编写一个程序,分别使用字节流和字符流拷贝一个文本文件
时间: 2023-03-19 20:29:13 浏览: 242
以下是使用Java代码实现的示例:
使用字节流拷贝:
```
import java.io.*;
public class ByteStreamExample {
public static void main(String[] args) {
try (FileInputStream in = new FileInputStream("source.txt");
FileOutputStream out = new FileOutputStream("destination.txt")) {
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
}
```
使用字符流拷贝:
```
import java.io.*;
public class CharacterStreamExample {
public static void main(String[] args) {
try (BufferedReader in = new BufferedReader(new FileReader("source.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("destination.txt"))) {
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)