编写一个程序,分别使用字节流和字符流拷贝一个文本文件
时间: 2023-03-19 22:29:13 浏览: 228
以下是使用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);
}
}
}
```
阅读全文