- package com.itheima;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /*
- * 9、 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
- *
- * */
- public class Test9 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
-
- copyJava("C:\\Users\\lenovo\\Desktop\\abc","C:\\Users\\lenovo\\Desktop\\eee" );
- }
-
- public static void copyJava(String path,String direction) throws IOException{
-
- File file = new File(direction);
- if(!file.exists()){
- file.mkdirs();
- }
-
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- File f = new File(path);
- File[] files = f.listFiles();
- for(int i=0;i<files.length;i++){
- String filename = files[i].getName();
- if(filename.endsWith(".java")){
- int len = 0;
- byte[] buffer = new byte[1024];
- bis = new BufferedInputStream(new FileInputStream(files[i]));
- bos = new BufferedOutputStream(new FileOutputStream(direction+"\\"+filename.replace(".java", ".txt")));
- while((len=bis.read(buffer))!=-1){
- bos.write(buffer, 0, len);
- }
- bis.close();
- bos.close();
- }
- }
- }
- }
复制代码 |