Java中从键盘输入10个整数,创建cl.txt,然后将它们从小到大排序并写入cl.txt,然后再将文件的内容读出来,输出到屏幕上
时间: 2023-10-05 21:06:25 浏览: 51
以下是Java代码实现该功能:
```java
import java.io.*;
import java.util.*;
public class SortIntegers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] nums = new int[10];
System.out.println("请输入10个整数:");
for (int i = 0; i < nums.length; i++) {
nums[i] = scanner.nextInt();
}
Arrays.sort(nums);
try {
FileWriter writer = new FileWriter("cl.txt");
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + "\n");
}
writer.close();
System.out.println("已将排序结果写入cl.txt文件。");
FileReader reader = new FileReader("cl.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
System.out.println("排序结果为:");
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
运行程序后,依次输入10个整数,程序会将它们从小到大排序并写入名为cl.txt的文件,并将文件的内容读出来输出到屏幕上。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)