java读写txt 文件-代码实例

java读写txt 文件-代码实例,第1张


源数据:5306行*15列

public class WindLoad01 {

    public static void main(String[] args) throws Exception {

        File file1 = new File("C:/Users/zzyuan/Desktop/test.txt");

        //存取数据
        double[][] map = new double[5306][15];

        int n = map.length;
        int m = map[0].length;

        BufferedReader br = new BufferedReader(new FileReader(file1));

        String[] line = null;
        String s = "";
        String str = "";
        int index = 0;

        while ((s = br.readLine()) != null){
            line = s.split("\s+");
            for(int j = 0 ; j < 15 ; j++){
                map[index][j] = Double.parseDouble(line[j]) - Double.parseDouble(line[j+15]);
                map[index][j] *= 31;
                str = String.format("%.2f",map[index][j]);
                map[index][j] = Double.parseDouble(str);
            }
            index++;
        }

        File file2 = new File("C:/Users/zzyuan/Desktop/out.txt");

        BufferedWriter wr = new BufferedWriter(new FileWriter(file2));
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                wr.write(map[i][j] + "\t");
            }
            wr.write("\r\n");
        }

        //关闭数据流
        br.close();
        wr.close();
    }
}

结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/876819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存