不要自卑,去提升实力
互联网行业谁技术牛谁是爹
如果文章可以带给你能量,那是最好的事!请相信自己
加油o~
题目描述:
编写一个程序,如果名为Exercise12_15.txt的文件不存在,则创建该文件。使用文本I/O将随机产生的100个整数写入文件,文件中的整数由空格分开。从文件中读回数据并以升序显示数据。
代码:
/**
*作者:魏宝航
*2020年12月5日,上午8:16
*/
import java.io.*;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String filename="Exercise12_15.txt";
File file=new File(filename);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileWriter fw=new FileWriter(file);
String s="";
for(int i=0;i<100;i++){
s+=(int)(Math.random()*100)+" ";
}
fw.write(s);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader rf=new BufferedReader(new InputStreamReader(new FileInputStream(file)) {
});
String[] arr=new String[100];
String s="";
String res="";
try {
while ((s=rf.readLine())!=null) {
res+=s;
res+=" ";
}
arr=res.split(" ");
System.out.println(res);
int[] a=new int[100];
for(int i=0;i<100;i++){
a[i]=Integer.parseInt(arr[i]);
}
Arrays.sort(a);
for(int i:a){
System.out.print(i+" ");
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}