从txt文件读取矩阵 [英] Read a matrix from a txt file

查看:80
本文介绍了从txt文件读取矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个txt文件,上面有一个矩阵列表.例如:

I have a txt file that has a list of matrices on it. For ex:

[6]

5 3        
6 2

7 8 -1     
4 3 1      
-2 0 3    

,我需要通读此txt文件并确定每个矩阵的行列式,然后将矩阵及其行列式输出到输出txt文件.我一直坚持要读取每个矩阵,并在我的确定的类中运行它们,然后将其输出到输出txt文件.如何分别获得每个矩阵和一个矩阵,以便可以通过代码运行它?

and I need to read through this txt file and determine the determinant of each matrix and then output the matrix and its determinant to an output txt file. I am stuck with getting each matrix to be read and run through my determinate class and then outputted to the output txt file. How do I get each matrix separately and a matrix so that I can run it through my code?

这是我到目前为止的内容,但是它只是将每一行输出到输出文件,而没有将每个矩阵都作为矩阵,因此我可以找到其行列式.

This is what I have so far, but it just outputs each line to the output file and does not get each matrix as a matrix so I can find its determinant.

    FileReader fileReader = new FileReader (inputFilePath);                                   
    FileWriter fileWriter = new FileWriter (outputFilePath);                                             
    BR = new BufferedReader(fileReader);                                                                           
    try {                                                                                          
        String line = BR.readLine();                                                                            
        while (line != null) {                                                                           
            fileWriter.write(line + '\n');                                                                                             
            fileWriter.write("determinant= " + '\n');                                                                                                           
            line = BR.readLine();                                                                                              
        } 
        BR.close();                                                                                          
    } catch (IOException e) {                                                                                              
        e.printStackTrace();                                                                                  
    }

任何帮助将不胜感激!

推荐答案

假定矩阵是由整数值组成的,则可以读取所有行,然后对于由空格分隔的每一行,将其作为数组返回.

Assuming the matrices are made of integer values, you can read all lines and then for each line split by the whitespace and return as an array.

       List<String> lines = Files.readAllLines(Paths.get(inputFilePath));

       List<int[]> listOfMatrices = lines.stream()
                .filter(line -> !line.isEmpty())
                .map(line -> Arrays.stream(line.split(" "))
                        .mapToInt(Integer::parseInt)
                        .collect(Collectors.toList())
                .collect(Collectors.toList());

输出

[5, 3]
[6, 2]
[7, 8, -1]
[4, 3, 1]
[-2, 0, 3]

这篇关于从txt文件读取矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆