java写文件到本地文件_Java简单实现本地文件读写操作

这段Java代码展示了如何从文件中读取数据,计算大气环流指数的逐月平均值,并将结果写入新文件。同时,它还计算2017年的指数方差。程序使用BufferedReader和BufferedWriter进行文件操作,处理的数据包含无效值(-999)。计算过程中,无效值不被纳入统计。
摘要由CSDN通过智能技术生成

问题:计算出大气环流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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值