//IO_001.java
import java.io.*;
public class IO_001{
public staic void main(String[] args)throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt",true));
bw.write("文件已被创建成功!");
bw.newLine();
bw.write("又添加了一行文字");
bw.close();
}
}
//IO_002.java
import java.io.*;
import java.util.*;
public class IO_002{
public static void main(String[] args)throws IOException{
IO_002 io = new IO_002();
io.getWriter();
Integer[] arr = io.getReader_Sort();
for(Integer s : arr)
System.out.println(s);
}
//写入方法
private void getWriter()throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("file2.txt"));
for(int i = 0; i < 15; i++){
bw.write(String.valueOf(new Random().nextInt(99999)+20));
bw.newLine();
}
bw.close();
}
//读取、排序方法
private Integer[] getReader_Sort()throws IOException{
BufferedReader br = new BufferedReader(new FileReader("file2.txt"));
List list =new ArrayList();
String line = "";
while((line=br.readLine())!=null){
list.add(Integer.parseInt(line));
}
Integer[] arr = new Integer[list.size()];
arr = list.toArray(arr);
//直接调用排序工具类
Arrays.sort(arr);
return arr;
}
}