码迷,mamicode.com
首页 > 编程语言 > 详细

java实现文本文件的拷贝

时间:2015-08-04 00:23:46      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

package com.starain.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class CopyFile {

public static void main(String[] args) {

try {
BufferedWriter writer11 = new BufferedWriter(new FileWriter("D:/t.txt"));
writer11.write("1");
writer11.newLine();
writer11.write("2");
writer11.newLine();
writer11.write("3");
writer11.newLine();
writer11.write("4");
writer11.newLine();
writer11.write("5");
writer11.newLine();
writer11.write("6");
writer11.newLine();
writer11.write("7");
writer11.newLine();
writer11.write("8");
writer11.newLine();
writer11.write("9");
writer11.newLine();
writer11.write("0");
writer11.newLine();
writer11.close();
} catch (Exception e) {
System.out.println("创建文件失败");
}

System.out.println("请输入源文件名(例如D:/t.txt),但需要保证文件存在");
Scanner scanner = new Scanner(System.in);
String sfilePathName = scanner.next();
System.out.println("请输入目的文件名(例如D:/tester2.txt)");
String dfilePathName = scanner.next();
CopyAndPaste copy = new CopyAndPaste();
copy.setSfilePathName(sfilePathName);
copy.setDfilePathName(dfilePathName);
copy.copyAndPaste();
}

}
class CopyAndPaste{
private String sfilePathName;//源文件地址
private String dfilePathName;//目的文件地址
BufferedReader bufferedRead;
BufferedWriter bufferedWrite;
String line;
public CopyAndPaste(){

}
public CopyAndPaste(String sfilePathName,String dfilePathName){
this.setSfilePathName(sfilePathName);
this.setDfilePathName(dfilePathName);
}
public void setSfilePathName(String sfilePathName) {
this.sfilePathName = sfilePathName;
}
public String getSfilePathName() {
return sfilePathName;
}
public void setDfilePathName(String dfilePathName) {
this.dfilePathName = dfilePathName;
}
public String getDfilePathName() {
return dfilePathName;
}

public void copyAndPaste(){



try {
bufferedRead = new BufferedReader(new FileReader(sfilePathName));
} catch (FileNotFoundException e) {
System.out.println("file read false");
}
try {
bufferedWrite = new BufferedWriter(new FileWriter(dfilePathName));
} catch (Exception e) {
System.out.println("file write false");
}
try {
line = bufferedRead.readLine();
System.out.println("文本内容:");
while(line!=null){
System.out.println(line);
bufferedWrite.write(line);
bufferedWrite.newLine();
line = bufferedRead.readLine();
}
System.out.println("拷贝完成\nsuccess");
} catch (Exception e) {
System.out.println("copy and paste false");
}
try {
bufferedRead.close();
} catch (Exception e) {
System.out.println("read close false");
}
try {
bufferedWrite.close();
} catch (Exception e) {
System.out.println("write close false");
}
}
}

java实现文本文件的拷贝

标签:

原文地址:http://www.cnblogs.com/starainDou/p/4700793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!