FileInputStream和FileOutputStream可以拷贝文本文件吗
时间: 2024-05-02 20:15:23 浏览: 10
是的,FileInputStream和FileOutputStream可以用于拷贝文本文件。可以通过以下方式实现:
```java
try {
FileInputStream input = new FileInputStream("source.txt");
FileOutputStream output = new FileOutputStream("destination.txt");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
System.out.println("文件拷贝成功!");
} catch (IOException e) {
e.printStackTrace();
}
```
其中,source.txt是源文件的名称,destination.txt是目标文件的名称。拷贝过程中,我们使用了一个缓存区,每次读取和写入1024个字节的数据。如果想要拷贝其他类型的文件(如图片、音频等),也可以使用同样的方式,只需要将源文件和目标文件的名称修改即可。
相关问题
编写一个程序,分别使用字节流和字符流拷贝一个文本文件。要求如下: (1)使用FileInputStream、FileOutputStream和FileReader、FileWriter分别进行拷贝。 (2)使用字节流拷贝时,定义一个1024长度的字节数组作为缓冲区,使用字符流拷贝,使用BufferedReader和BufferedWriter包装流进行包装。
好的,以下是使用字节流和字符流分别拷贝文本文件的示例代码:
1. 使用字节流拷贝文件:
```java
import java.io.*;
public class ByteStreamCopy {
public static void main(String[] args) {
String srcPath = "source.txt";
String destPath = "dest.txt";
byte[] buffer = new byte[1024];
try {
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
System.out.println("File copied using byte stream!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 使用字符流拷贝文件:
```java
import java.io.*;
public class CharStreamCopy {
public static void main(String[] args) {
String srcPath = "source.txt";
String destPath = "dest.txt";
try {
FileReader fr = new FileReader(srcPath);
FileWriter fw = new FileWriter(destPath);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
System.out.println("File copied using character stream!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
注意,以上的示例代码只是演示了如何使用字节流和字符流分别拷贝文本文件,并没有进行异常处理等完善的操作。在实际使用中,需要根据具体情况进行适当的改进和完善。
1.编写一个程序,分别使用字节流和字符流拷贝一个文本文件(注意 必须是.txt为后缀的文本文件),要求如下: (1)使用FileInputStream、FileOutputStream和FileReader、FileWriter 分别进行复制; (2)使用字节流拷贝时,定义一个1024字节的字节数组作为缓冲区, 使用字符流拷贝时,使用BufferReader包装流作为缓冲。
以下是使用字节流拷贝的代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileByte {
public static void main(String[] args) {
String sourceFile = "source.txt";
String targetFile = "target.txt";
// 使用FileInputStream和FileOutputStream复制文本文件
try (FileInputStream fis = new FileInputStream(new File(sourceFile));
FileOutputStream fos = new FileOutputStream(new File(targetFile))) {
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
System.out.println("使用字节流拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以下是使用字符流拷贝的代码:
```java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFileChar {
public static void main(String[] args) {
String sourceFile = "source.txt";
String targetFile = "target.txt";
// 使用FileReader和FileWriter复制文本文件
try (BufferedReader br = new BufferedReader(new FileReader(new File(sourceFile)));
FileWriter fw = new FileWriter(new File(targetFile))) {
char[] buffer = new char[1024];
int len;
while ((len = br.read(buffer)) != -1) {
fw.write(buffer, 0, len);
}
System.out.println("使用字符流拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
需要注意的是,以上代码中的文件名和路径需要根据实际情况进行修改。