在E盘根目录下创建文件名为iotest3.txt,其内容为outputstream.创建文件输出流将iotest1.txt文件内容写入iotest3.txt,并显示iotest3.txt内容。
时间: 2023-12-21 17:07:32 浏览: 27
好的,以下是Java代码实现:
```java
import java.io.*;
public class FileIOExample {
public static void main(String[] args) {
try {
// 在E盘根目录下创建文件名为iotest3.txt
File file = new File("E:/iotest3.txt");
file.createNewFile();
// 获取iotest1.txt文件内容
String content = getFileContent("E:/iotest1.txt");
// 创建文件输出流将iotest1.txt文件内容写入iotest3.txt
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
// 显示iotest3.txt内容
String fileContent = getFileContent("E:/iotest3.txt");
System.out.println(fileContent);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getFileContent(String filePath) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line + "\n");
}
reader.close();
return content.toString();
}
}
```
注意事项:
1. 代码中的文件路径是根据题目中给出的路径进行的,如果实际情况不同,请自行修改。
2. 程序运行前请确保E盘根目录下存在iotest1.txt文件。