62,615
社区成员
发帖
与我相关
我的任务
分享
//比如没关闭流之类的问题暂时不管.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class FileCopy {
public static void main(String[] args) {
// TODO Auto-generated method stub
String oldPath = "F:\\test\\test1.txt";
String newPath = "F:\\test2\\";
Copy(oldPath,newPath);
}
private static void Copy(String oldPath, String newPath) {
// TODO Auto-generated method stub
BufferedReader br = null;
BufferedWriter bw = null;
String str ="";
String fileName = "";
File oldFile = new File(oldPath);
fileName = oldFile.getName();
if(oldFile.exists()){
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(oldPath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("地址" + oldPath + "不存在文件");
e.printStackTrace();
}
}
File newFile = new File(newPath);
if(!newFile.exists()){
newFile.mkdirs();
}
try {
File newFilePath = new File(newPath +File.separator+ fileName);
newFilePath.createNewFile();
bw = new BufferedWriter(new OutputStreamWriter((new FileOutputStream(newPath +File.separator+ fileName)),"UTF-8"));
while((str = br.readLine()) != null){
System.out.println(str);
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
public class FileTest {
public static void main(String[] args) {
String oldPath = "f:\\test\\test1.txt";
String newPath = "f:\\test\\12.txt"; //加上你要创建的文件名
Copy(oldPath, newPath);
}
private static void Copy(String oldPath, String newPath) {
BufferedReader br = null;
BufferedWriter bw = null;
String str = "";
String fileName = "";
File oldFile = new File(oldPath);
fileName = oldFile.getName();
if (oldFile.exists()) {
try {
//用BufferedReader作为读取文件内容的外管道
br = new BufferedReader(new FileReader(oldFile));
} catch (FileNotFoundException e) {
System.out.println("地址" + oldPath + "不存在文件");
e.printStackTrace();
}
}
File newFile = new File(newPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
try {
File newFilePath = new File(newPath + File.separator + fileName);
newFilePath.createNewFile();
//用BufferedWriter作为向新文件写内容的外管道
bw = new BufferedWriter(new FileWriter(newFilePath));
while ((str = br.readLine()) != null) {
System.out.println(str);
bw.write(str);
bw.newLine();
}
br.close(); //用完后关闭流
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}