Java简单实现本地文件读写操作
问题:计算出大气环流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 }
字节流和字符流:
- 字节流:一般是带有Stream字样的,如InputStream,FileInputStream等等,字节流处理的最小单位是字节。
- 字符流:一般是带有Reader或Writer字样的,如InputStreamReader等等,字符流处理的最小单位是字符。
02:14:33
时间紧,任务重,上面写的太糙了...
修改如下
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 }
读取文件思路整理:(写入文件思路相反,请自行百度)