本帖最后由 Rain2692 于 2014-12-19 14:26 编辑
将d:\\下的所有java文件拷贝到d:\\jad目录下后缀名为.txt,将自己的思路展示给大家,互相交流,该题目不仅在笔试中出现过,而且在面试中也有出现。- [hide]public static void main(String[] args)throws Exception {
- // TODO Auto-generated method stub
- File src =new File("D:\\");
- File dec = new File("D:\\Jad");
- if(!dec.exists())
- dec.mkdirs();
- ArrayList<File> al = new ArrayList<>();
- fileTolist(src,al);
- for(int i=0;i<al.size();i++)
- {
- copyfiles(al.get(i));
- }
- System.out.println(al.size());
- }
- //分类统计添加
- public static void fileTolist(File dir,ArrayList<File> al)throws Exception
- {
- File[] files = dir.listFiles();
- if(files!=null)
- {
- for(File file : files)
- {
- if(file.isDirectory())
- {
- fileTolist(file,al);
- }
- else
- {
- if(file.getName().endsWith(".java"))
- {
- al.add(file);
- System.out.println(file.getName());
- }
- }
- }
- }
- }
- //拷贝文件
- public static void copyfiles(File file) throws Exception
- {
- String str = file.getName();
- BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
- BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Jad\\"+str.replace(".java", ".txt"))));
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- bufw.write(line);
- bufw.newLine();
- bufw.flush();
- }
- bufr.close();
- bufw.close();
- }[/hide]
复制代码
|