文件复制
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
中国