問題:計算出大氣環流1到142項指數的所有年份的逐月平均值並輸出成文本,計算2017年每一個指數的方差並輸出成文本。(無效值 -999 不計算在內)數組
1 public class Test { 2 3 public static void main(String[] args) { 4 boolean isFirst = true; 5 try { 6 File file = new File("E:\\index_all.txt"); 7 File outFile = new File("E:\\index.txt"); 8 if (file.isFile() && file.exists()) { //判斷文件或目錄是否存在 9 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "utf-8"); 10 BufferedReader bufferedReader = new BufferedReader(reader); 11 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile), "gbk"); 12 BufferedWriter bufferedWriter = new BufferedWriter(writer); 13 OutputStreamWriter writer2017 = new OutputStreamWriter(new FileOutputStream(outFile), "gbk"); 14 BufferedWriter bufferedWriter2017 = new BufferedWriter(writer); 15 String lineTxt = null; 16 List<String> list = new ArrayList<>(); 17 while ((lineTxt = bufferedReader.readLine()) != null) { 18 System.out.println(lineTxt); 19 list.add(lineTxt.replace(" ", ",")); 20 // bufferedWriter.write(lineTxt+"\r\n"); 21 } 22 bufferedWriter.write(list.get(0).replace(",", " ") + "\tavg\r\n"); 23 24 String[] arr = new String[list.size()]; 25 arr = list.toArray(arr); 26 for (int i = 1; i < arr.length; i++) { 27 String[] line = arr[i].split(","); 28 String[] line2017 = arr[0].split(","); 29 double avg = 0, sum = 0; 30 /*int a = 0; 31 for (int j = 9; j < line.length; j++) { 32 if (line[j].length() > 0 && !line[j].startsWith("-999")) { 33 sum += Double.valueOf(line[j]); 34 a += 1; 35 } 36 } 37 avg = sum / a;*/ 38 avg = getAvg(line); 39 bufferedWriter.write(list.get(i).replace(",", " ") + "\t" + String.format("%.3f", avg) + "\r\n"); //保留3位小數 40 bufferedWriter.flush(); //將緩衝區的內容輸出 41 42 // 計算2017年每一個指數的方差並輸出成文本 43 double powAvg = 0, powSum = 0; 44 if (line[4].startsWith("2017")) { 45 if (isFirst) { 46 bufferedWriter2017.write("\r\n\r\n" + line2017[3] + "\t平均值" + "\t方差\r\n"); 47 isFirst = false; 48 } 49 double avg2017 = getAvg(line); 50 int p = 0; 51 for (int j = 9; j < line.length; j++) { 52 if (line[j].length() > 0 && !line[j].startsWith("-999")) { 53 powSum += Math.pow((Double.valueOf(line[j]) - avg2017), 2); //計算平方和 54 p += 1; 55 } 56 } 57 powAvg = powSum / p; //計算方差 58 bufferedWriter2017.write(line[4] + "\t" + String.format("%.3f", avg) + "\t" + String.format("%.3f", powAvg) + "\r\n"); 59 } 60 } 61 bufferedWriter.flush(); //將緩衝區的內容輸出 62 bufferedWriter2017.flush(); 63 bufferedWriter.close(); //①先關BufferedWriter 64 bufferedWriter2017.close(); 65 writer.close(); //②後關OutputStreamWriter 66 writer2017.close(); 67 bufferedReader.close(); 68 reader.close(); 69 } else { 70 System.out.println("找不到指定的文件"); 71 } 72 } catch (Exception e) { 73 System.out.println("讀取文件內容出錯"); 74 e.printStackTrace(); 75 } 76 } 77 78 private static double getAvg(String[] line) { 79 double sum = 0; 80 int a = 0; 81 for (int j = 9; j < line.length; j++) { 82 if (line[j].length() > 0 && !line[j].startsWith("-999")) { 83 sum += Double.valueOf(line[j]); 84 a += 1; 85 } 86 } 87 return sum / a; 88 } 89 }
字節流和字符流:編碼
02:14:33spa
時間緊,任務重,上面寫的太糙了...code
修改以下orm
1 public class Test { 2 public static void main(String[] args) { 3 boolean isFirst = true; 4 File file = new File("E:\\index_all.txt"); 5 if (file.isFile() && file.exists()) { //判斷文件或目錄是否存在 6 try { 7 InputStream in = new FileInputStream(file); 8 File outFile = new File("E:\\index.txt"); 9 OutputStream out = new FileOutputStream(outFile); 10 try { 11 Reader reader = new InputStreamReader(in, "utf-8"); 12 BufferedReader bufferedReader = new BufferedReader(reader); 13 Writer writer = new OutputStreamWriter(out, "gbk"); 14 BufferedWriter bufferedWriter = new BufferedWriter(writer); 15 BufferedWriter bufferedWriter2017 = new BufferedWriter(writer); 16 String lineTxt = null; 17 List<String> list = new ArrayList<>(); 18 try { 19 while ((lineTxt = bufferedReader.readLine()) != null) { 20 System.out.println(lineTxt); 21 list.add(lineTxt.replace(" ", ",")); 22 // bufferedWriter.write(lineTxt + "\r\n"); 23 } 24 } catch (IOException e) { 25 System.out.println("異常:讀取文件出錯"); 26 e.printStackTrace(); 27 } 28 29 try { 30 bufferedWriter.write(list.get(0).replace(",", " ") + "\tavg\r\n"); 31 String[] arr = new String[list.size()]; 32 arr = list.toArray(arr); // 將List集合轉成String[]數組 33 for (int i = 1; i < arr.length; i++) { 34 String[] line = arr[i].split(","); 35 String[] line2017 = arr[0].split(","); 36 double avg = 0, sum = 0; 37 avg = getAvg(line); 38 bufferedWriter.write(list.get(i).replace(",", " ") + "\t" + String.format("%.3f", avg) + "\r\n"); // 保留3位小數 39 bufferedWriter.flush(); //將緩衝區的內容輸出[沖刷出流(刷新此輸出流),將全部緩衝的數據強制發送到目的地] 40 41 // 計算2017年每一個指數的方差並輸出成文本 42 double powAvg = 0, powSum = 0; 43 if (line[4].startsWith("2017")) { 44 if (isFirst) { 45 bufferedWriter2017.write("\r\n\r\n" + line2017[3] + "\t平均值" + "\t方差\r\n"); 46 isFirst = false; 47 } 48 double avg2017 = getAvg(line); 49 int p = 0; 50 for (int j = 9; j < line.length; j++) { 51 if (line[j].length() > 0 && !line[j].startsWith("-999")) { 52 powSum += Math.pow((Double.valueOf(line[j]) - avg2017), 2); 53 p += 1; 54 } 55 } 56 powAvg = powSum / p; 57 bufferedWriter2017.write(line[4] + "\t" + String.format("%.3f", avg) + "\t" + String.format("%.3f", powAvg) + "\r\n"); 58 } 59 } 60 bufferedWriter.flush(); //將緩衝區的內容輸出[沖刷出流(刷新此輸出流),將全部緩衝的數據強制發送到目的地] 61 bufferedWriter2017.flush(); 62 } catch (IOException e) { 63 System.out.println("異常:寫入文件出錯"); 64 e.printStackTrace(); 65 } finally { 66 try { 67 out.close(); // 直接調用最外層的close()就能夠,相似鏈條 你關閉最外面的 最外面的關閉裏面的 裏面的關閉更裏面的; 68 in.close(); 69 } catch (IOException e) { 70 System.out.println("異常:不支持編碼"); 71 e.printStackTrace(); 72 } 73 } 74 } catch (UnsupportedEncodingException e) { 75 System.out.println("異常:不支持編碼"); 76 e.printStackTrace(); 77 } 78 } catch (FileNotFoundException e) { 79 System.out.println("異常:文件未找到"); 80 e.printStackTrace(); 81 } 82 } else { 83 System.out.println("文件或目錄不存在"); 84 } 85 } 86 87 private static double getAvg(String[] line) { 88 double sum = 0; 89 int a = 0; 90 for (int j = 9; j < line.length; j++) { 91 if (line[j].length() > 0 && !line[j].startsWith("-999")) { 92 sum += Double.valueOf(line[j]); 93 a += 1; 94 } 95 } 96 return sum / a; 97 } 98 }
讀取文件思路整理:(寫入文件思路相反,請自行百度)blog