思路:
1.根据数据源创建字节输入流对象
2.根据目的地创建字节输出流对象
3.读写数据,复制文本文件(一次读一个字节,一次写一个字节)
public class CopyTxtDemo {
public static void main(String[] args) throws IOException {
// 1.根据数据源创建字节输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\map.txt");
// 2.根据目的地创建字节输出流对象
FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\map1.txt");
// 3.读写数据,复制文本文件(一次读一个字节,一次写一个字节)
int by ;
while ((by = fis.read()) != -1){
fos.write(by);
}
fis.close();
fos.close();
}
public void add() throws IOException {
FileInputStream fis =new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\map.txt");
// 2.调用字节输入流对象的读数据方法
// int read() 从该输入流读取一个字节的数据。
// int by = fis.read();
// System.out.println(by);
// System.out.println((char)by);
//如果达到文本末尾 -1
// while (by != -1){
// System.out.print((char)by);
// by =fis.read();
// }
//优化
int by;
while ((by = fis.read()) != -1){
System.out.print((char)by);
}
// 3.释放资源
fis.close();
}
}