本帖最后由 heheka123 于 2014-4-22 17:10 编辑
从键盘输入接收一个文件夹路径, 将该文件夹下的所有.java文件的文件名写入到a.txt文件中
- public class Demo12{
- public static void main(String[] args) throws IOException {
- System.out.println("请输路径:");
- File dir = getDir();
- String[] arr = dir.list();
- BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
- for (String name : arr)
- if (name.endsWith(".java")) {
- bw.write(name);
- bw.newLine();
- }
- bw.close();
- }
- public static File getDir() throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- while (true) {
- File file = new File(br.readLine());
- if (!file.exists())
- System.out.print("您输入的路径不存在");
- else if(!file.isDirectory())
- System.out.println("您输入的不是文件夹路径");
- else
- return file;
- }
- }
- }
复制代码
怎么出不来结果啊 大家看看哪里错了,高手救急啊!!!!! |
|