1.把生成的随机数写入到文件中
1 public static void WriterFun(){ 2 //获得路径 3 String filepath = System.getProperty("user.dir"); 4 filepath += "\\file.txt"; 5 6 File file = new File(filepath); 7 if(!file.exists()){ 8 try { 9 file.createNewFile(); 10 } catch (IOException e) { 11 e.printStackTrace(); 12 } 13 } 14 try { 15 BufferedWriter bw = new BufferedWriter(new FileWriter(file)); 16 17 Random random = new Random(); 18 for(int i=0;i<10000;i++){ 19 int nums = Math.round(random.nextFloat()*1000.0f); 20 //将int 转化为 String类型 21 bw.write(Integer.toString(nums)); 22 bw.newLine(); 23 } 24 bw.close(); 25 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 }
读取文本数据
1 public static void fun1Reader(){ 2 3 String filepath = System.getProperty("user.dir"); 4 filepath += "\\123.txt"; 5 6 File file = new File(filepath); 7 BufferedReader br; 8 try { 9 br = new BufferedReader(new FileReader(file)); 10 11 String tmp; 12 while((tmp = br.readLine()) != null){ 13 System.out.println(tmp); 14 } 15 } catch (Exception e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } 19 }
贴出全部代码
1.生成10000整形的随机数存入文本file.txt中
2.从file.txt中读取数据并且放入Arraylist中
3.排序 选择排序方法进行排序
4.写入到files.txt文件中
1 package com.SortDao; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 import java.util.ArrayList; 11 import java.util.Random; 12 13 14 public class WriteAndReader { 15 16 17 public static void main(String[] args) { 18 // TODO Auto-generated method stub 19 WriterFun(); 20 21 // 关于为什么是Integer 不是int 大家自己网上查找一下 22 ArrayList<Integer> arrayList = ReaderFun(); 23 sorts(arrayList); 24 WriterFunc(arrayList); 25 arrayList.clear(); 26 } 27 28 //直接写入生成的随机数 29 public static void WriterFun(){ 30 //获得路径 31 String filepath = System.getProperty("user.dir"); 32 filepath += "\\file.txt"; 33 34 File file = new File(filepath); 35 if(!file.exists()){ 36 try { 37 file.createNewFile(); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 try { 43 BufferedWriter bw = new BufferedWriter(new FileWriter(file)); 44 45 Random random = new Random(); 46 for(int i=0;i<10000;i++){ 47 int nums = Math.round(random.nextFloat()*10000.0f); 48 //将int 转化为 String类型 49 bw.write(Integer.toString(nums)); 50 bw.newLine(); 51 } 52 bw.close(); 53 54 } catch (IOException e) { 55 e.printStackTrace(); 56 } 57 } 58 59 // 读入写入file.txt中的随机数 60 public static ArrayList<Integer> ReaderFun(){ 61 String filepath = System.getProperty("user.dir"); 62 filepath += "\\file.txt"; 63 File file = new File(filepath); 64 ArrayList<Integer> arrayList = new ArrayList<Integer>(); 65 if(file.exists()){ 66 try { 67 BufferedReader br = new BufferedReader(new FileReader(file)); 68 String tmp; 69 while((tmp = br.readLine()) != null){ 70 arrayList.add(Integer.valueOf(tmp)); 71 72 } 73 br.close(); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 78 }else{ 79 System.out.println("文件不存在"); 80 } 81 return arrayList; 82 } 83 84 // 进行排序(应该是选择排序) 85 public static void sorts(ArrayList< Integer> arr){ 86 for(int i = 0;i < arr.size();i++){ 87 Integer min = arr.get(i); 88 int position = i; 89 for(int j=i;j<arr.size();j++){ 90 if(min>arr.get(j)){ 91 min=arr.get(j); 92 position = j; 93 } 94 } 95 arr.set(position, arr.get(i)); 96 arr.set(i,min); 97 } 98 } 99 100 101 public static void WriterFunc(ArrayList<Integer> arr ){ 102 String filepath = System.getProperty("user.dir"); 103 filepath += "\\files.txt"; 104 File file = new File(filepath); 105 if(!file.exists()){ 106 try { 107 file.createNewFile(); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 } 111 } 112 try { 113 BufferedWriter bw = new BufferedWriter(new FileWriter(file)); 114 115 bw.write("After Sorted"); 116 bw.newLine(); 117 118 for(int i=0;i<arr.size();i++){ 119 Integer num = arr.get(i); 120 bw.write(Integer.toString(num)); 121 bw.newLine(); 122 } 123 124 bw.close(); 125 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } 129 } 130 }
时间: 2024-10-10 01:33:32