//从一个txt文件写到另一个文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestByteStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream in=null;
FileOutputStream out=null;
String path="D://JavaProject//练习//src//com//";
try{
in=new FileInputStream(path+"IO文本.txt");
out=new FileOutputStream(path+"IO文本复件.txt");
int c;
while((c=in.read())!=-1){
out.write(c);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if(in!=null)
in.close();
if(out!=null)
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
本文提供了一个使用Java进行文件复制的简单示例。通过FileInputStream和FileOutputStream读取和写入字节流的方式,实现了从一个文件到另一个文件的内容复制。此过程包括打开文件流、逐字节读取并写入目标文件,最后关闭流。

被折叠的 条评论
为什么被折叠?



