本帖最后由 air 于 2013-12-29 19:00 编辑
- import java.io.*;
- import java.util.*;
- class JavaFileList
- {
- public static void main(String[] args) throws IOException
- {
-
- File dir = new File("d:\\java1223");
- List<File> list = new ArrayList<File>();
- fileToList(dir,list);
- //System.out.println(list.size());
- File file = new File(dir,"javalist.txt");
- writeToFile(list,file.toString());
-
- }
- public static void fileToList(File dir,List<File> list)
- {
- File[] files = dir.listFiles();
- for(File file : files)
- {
- if(file.isDirectory())
- fileToList(file,list);
- else
- {
- if(file.getName().endsWith(".java"))
- list.add(file);
- }
- }
- }
- public static void writeToFile(List<File> list,String javaListFile)throws IOException
- {
- BufferedWriter bufw = null;
- try
- {
- bufw = new BufferedWriter(new FileWriter(javaListFile));
-
- for(File f : list)
- {
- String path = f.getAbsolutePath();
- bufw.write(path);
- bufw.newLine();
- bufw.flush();
- }
- }
- catch (IOException e)
- {
- throw e;
- }
- finally
- {
- try
- {
- if(bufw!=null)
- bufw.close();
- }
- catch (IOException e)
- {
- throw e;
- }
- }
- }
- }
复制代码 我的问题是:
WriteToFile(这里写的是文件名,是file.toString)
可是这个文件名已经是别的地方 File file = new File(dir,"javalist.txt");
file.toString 用System.out打印出来的就是"javalist.txt",不包含上面一行里的dir---->File dir = new File("d:\\java1223");
为什么这个FileWriter 写出的javaList.txt文件会在("d:\\java1223","javalist.txt")
这个目录诞生文件而不是我写代码编译的class文件的路径诞生呢?
|