将java的文件改成txt然后复制到制定目录下
- package chart8;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /*
- * 思路:
- * 1,输入路径
- * 2,判断是否是存在以及是否是文件夹
- * 3,将文件夹传入copyDeom方法中
- * */
- public class JavaFileCopy
- {
- public static void main(String[] args)
- {
- System.out.println("请输入需要复制的文件夹路径");
- BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
- String path = null;
- try
- {
- path = bufr.readLine();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- File dir = new File(path);
- if((dir.exists()&&dir.isFile()))
- {
- System.out.println("路径不对");
- }
- copyAndRename(dir);
- System.out.println("复制成功");
-
- }
- public static void copyAndRename(File dir)
- {
- File[] files = dir.listFiles();
-
- for(File file:files)
- {
- if(file.isDirectory())
- {
- copyAndRename(file);
- }
- else
- {
- /*File[] filess = file.listFiles(new FilenameFilter() {
-
- @Override
- public boolean accept(File file, String name) {
- // TODO Auto-generated method stub
- return name.endsWith(".java");
- }
-
- });
- for(File file1 :filess)*/
-
- //本想用过滤器,但是发现,过滤器用上了,总是空指针异常为啥呢?
- if(file.getName().endsWith(".java"))
- {
-
- File newDir = new File("D:\\IO8\\TEXT");
- if(!(newDir.exists()))
- {
- newDir.mkdirs();
- }
- String newName = file.getName().replaceAll(".java", ".txt");
-
- FileInputStream fis=new FileInputStream(file);
- FileOutputStream fos = new FileOutputStream(new File(newDir,newName));
- byte[] buf = new byte[1024];
- int len = -1;
- while((len= fis.read(buf))!=-1)
- {
- fos.write(buf, 0, buf.length);
- }
- fis.close();
- fos.close();
-
-
- }
-
- }
- }
-
- }
复制代码 |