有没有那么多.... 给你个少的:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class CopyFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
Copy("D://a.java","D://text.txt");//第一个是目标文件的目录,第二个是你想拷贝的目录
}
public static void Copy(String F,String D) throws IOException, FileNotFoundException {
String file = F;
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "GBK"));
PrintWriter w = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(D), "utf-8"));
// .readLine(); 每次读取一行, 到 \n,\r为止
// 如果到文件末尾, 返回null
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
w.write(str + "\n");
}
in.close();
w.close();
}
}
正好 前两天写了个差不多的。直接贴给你了。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* 用java IO实现:将一个目录下的文件copy到另一个目录下。
* a) 输入:源目录,目标目录
* b) 输出:文件及目录结构复制成功并打印复制的文件个数
*/
public class FileCopy {
private File file = null;
private String sourceDir = null;
private String signDir = null;
private boolean flag = false;
public static void main(String[] args) {
new FileCopy();
}
public FileCopy() {
inputSourceDir();
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile()) System.out.println(f + "\t这是一个文件。");
else if (f.isDirectory()) System.out.println(f + "\t这是一个目录。");
}
inputSignDir();
copyFile(files);
}
private void inputSourceDir(){
flag = false;
do {
System.out.println("请输入源目录:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
sourceDir = br.readLine();
file = new File(sourceDir);
if (file.isDirectory()) {
flag = true;
} else {
flag = false;
}
} catch (IOException e) {
System.out.println("输入错误,请重新输入。");
}
} while (false == flag);
}
private void inputSignDir() {
flag = false;
do {
System.out.println("请输入目标目录:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
signDir = br.readLine();
file = new File(signDir);
if (file.isDirectory()) {
flag = true;
} else {
flag = false;
}
} catch (IOException e) {
System.out.println("输入错误,请重新输入。");
}
} while (false == flag);
}
private void copyFile(File[] files) {
System.out.println("拷贝的源文件夹路径:" + sourceDir);
System.out.println("拷贝的目标文件夹路径:" + signDir);
for (File f : files) {
try {
File ok = new File(signDir, f.getName());
if (false == ok.exists()) {
ok.createNewFile();
}
FileInputStream fis = new FileInputStream(f);
byte[] size = new byte[(int) f.length()];
fis.read(size);
FileOutputStream fos = new FileOutputStream(ok);
fos.write(size);
if ((f.getName().equals(ok.getName())) && (f.length() == ok.length())) {
System.out.println(f + "\t文件拷贝成功!");
} else {
System.out.println(f + "\t文件拷贝失败!");
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
System.out.println("文件(夹)没有找到!");
} catch (IOException e) {
System.out.println("文件读取出错!");
}
}
}
}