练习-Java输入输出之数据IO流之把文件中内容转为大写后写入另一个文件
时间: 2024-06-13 20:09:41 浏览: 12
以下是Java将文件内容转为大写并写入另一个文件的示例代码:
```java
import java.io.*;
public class FileToUpper {
public static void main(String[] args) {
try {
// 创建输入流和输出流
FileInputStream in = new FileInputStream("input.txt");
FileOutputStream out = new FileOutputStream("output.txt");
// 创建数据输入流和输出流
DataInputStream dataIn = new DataInputStream(in);
DataOutputStream dataOut = new DataOutputStream(out);
// 读取文件内容并转为大写后写入输出文件
while (dataIn.available() > 0) {
String str = dataIn.readUTF();
String upperStr = str.toUpperCase();
dataOut.writeUTF(upperStr);
}
// 关闭流
dataIn.close();
dataOut.close();
in.close();
out.close();
System.out.println("文件内容已转为大写并写入输出文件!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
相关推荐
![](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://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)