-1

I have a text file containing several columns (obtained using Python's np.savetxt). I would like to read it in Java, store it as a multidimensional array, loop over the rows of the array and do some operations with the values retrieved at every iteration. I have no previous experience in Java, so would be very grateful if you could help me solve this. In Python, this would be something like

a, b, c = np.loadtxt(myfile, unpack=True)
for i in range(len(a)):
    # do stuff with a[i], b[i], c[i]

I have found some implementations online that state that to read the file in Java I could use the following code. Unfortunately, it does not seem to work for me

    public static void ReadFile(String[] args) {
        BufferedReader abc = new BufferedReader(new FileReader(myfile));
        List<String> lines = new ArrayList<String>();

        while ((String line = abc.readLine()) != null) {
            lines.add(line);
            System.out.println(data);
        }
        abc.close();
    }
user36390
  • 11
  • 2
  • This is nothing to do with Python. You just need to look at the format that `numpy` saves the file in – roganjosh Nov 20 '19 at 22:07
  • 3
    Welcome to Stack Overflow! StackOverflow is not a free coding service. You're expected to try to [solve the problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Please update your question to show what you have already tried in a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). For further information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask), and take the [tour](https://stackoverflow.com/tour) :) – Michele Dorigatti Nov 20 '19 at 22:08
  • Thanks, @MicheleDorigatti. I have updated the question accordingly. – user36390 Nov 20 '19 at 22:17
  • user36390, it is not enough to find some code online and copy paste it. You cannot expect to learn Java by asking a question. Work on the code and come back with more specific questions. If you want help with the code posted, you need to report exactly what is the error that you get, what have you tried to fix it and what specifically you don't understand. – Michele Dorigatti Nov 20 '19 at 22:20
  • @roganjosh The question was never about Python. – csabinho Nov 20 '19 at 22:48
  • @csabinho why don't you check the edit history? – roganjosh Nov 20 '19 at 23:37
  • @roganjosh Of course I did! That's why I wrote the "never" in "The question was never about Python." – csabinho Nov 20 '19 at 23:40
  • 1
    @csabinho I don't know what the point of the comment is, then. I already dealt with it and left a comment to the OP as to why I did. What am I supposed to be answering here? – roganjosh Nov 20 '19 at 23:42

2 Answers2

0

Have you considered using a TSV file parser library such as Univocity ? Here is a previous post on StackExchange with some examples: Good and effective CSV/TSV Reader for Java

Kenneth S
  • 66
  • 5
0
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.nio.file.Path;

import static java.util.stream.Collectors.toList;

public class ReadFileInManyColumns {

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

    Path path = Paths.get("build/asd.txt");

    List<String> fileLines = Files.readAllLines(path, StandardCharsets.UTF_8);

    for (String fileLine : fileLines) {

      String[] columnArray = fileLine.split("\\s+");

      List<String> lineColumns = Arrays.stream(columnArray).collect(toList());

      doSomethingWithFileLineColumns(lineColumns);

    }
  }

  private static void doSomethingWithFileLineColumns(List<String> lineColumns) {
    System.out.println(lineColumns);
  }

}
  • Thanks for the detailed answer. The code as it is does not seem to work though. By adding `import java.nio.file.Path;` and replacing `var path = Paths.get("build/asd.txt");` with `Path path = Paths.get("build/asd.txt");` should compile instead. – user36390 Nov 21 '19 at 15:55
  • Sorry, i use Java 9+. Java 8 does not have keyword 'var'. – Евгений Колпаков Jan 24 '20 at 11:15