问题:计算出大气环流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 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 }
标签:Java,String,读写操作,int,length,bufferedWriter,本地,new,line
来源: https://www.cnblogs.com/wq-code/p/11178572.html