如何用java编写 从c盘的test.txt读取文件内容,然后拷贝到c盘得test2.txt文件中,并且覆盖原有的内容
2个回答
展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class test {
public static void main(String[] args ) throws Exception {
File source = new File("c:/test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
File desc = new File("c:/test2.txt");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(desc)));
String str = null;
while ((str = in.readLine()) != null) {
out.write(str + "\r\n");
}
if(desc.exists())
{
System.out.print("ok!");
}
out.close();
in.close();
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class test {
public static void main(String[] args ) throws Exception {
File source = new File("c:/test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
File desc = new File("c:/test2.txt");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(desc)));
String str = null;
while ((str = in.readLine()) != null) {
out.write(str + "\r\n");
}
if(desc.exists())
{
System.out.print("ok!");
}
out.close();
in.close();
}
}
参考资料: http://zhidao.baidu.com/question/97922158.html
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询