文件复制
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 复制文本文件
* 数据源:数据从哪里来
* a.txt -- 读取数据
* 目的地:数据到哪里去
* b.txt -- 写数据
*/
public class CopyFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//封装数据源
FileInputStream fis = new FileInputStream("a.txt");
//封装目的地
FileOutputStream fos = new FileOutputStream("b.txt");
int by = 0;
while((by = fis.read()) != -1){
fos.write(by);
}
//释放资源(先关谁都行)
fos.close();
fis.close();
}
}
运行结果:
a.txt:
hello world
java
中国
b.txt:
hello world
java
中国
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 复制文本文件
* 数据源:数据从哪里来
* a.txt -- 读取数据
* 目的地:数据到哪里去
* b.txt -- 写数据
*/
public class CopyFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//封装数据源
FileInputStream fis = new FileInputStream("a.txt");
//封装目的地
FileOutputStream fos = new FileOutputStream("b.txt");
int by = 0;
while((by = fis.read()) != -1){
fos.write(by);
}
//释放资源(先关谁都行)
fos.close();
fis.close();
}
}
运行结果:
a.txt:
hello world
java
中国
b.txt:
hello world
java
中国

本文提供了一个使用Java进行文件复制的简单示例。通过FileInputStream和FileOutputStream类,该示例展示了如何将一个文本文件的内容复制到另一个文件中。代码包括了必要的异常处理,并在完成操作后正确关闭了所有打开的流。
1777

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



