0

i have a 3 lines text of int that are data i need to put in some variables, i want to access one by one to all like an array, i can read the firt line but don't know how to go to next line, i know it 's a stupid thing but i'm blocked

public void Load () throws IOException {
    BufferedReader   in = new BufferedReader(new FileReader("prova.txt"));

    String inputLine = in.readLine();
    String [] fields = inputLine.split(" "); // Splits at the space
    int i=0;
    while(inputLine!=null) {
    System.out.println(fields[i]); //prints out name
    i++;
    }

}

i wanto to access to a single int for any line, any tips?

BackSlash
  • 21,927
  • 22
  • 96
  • 136
Fabio
  • 1
  • 2

4 Answers4

3

You can get all lines from file using Files.readAllLines() from java8:

List<String> lines = Files.readAllLines(Paths.get("prova.txt"));

for (String line : lines) {
    String[] split = line.split(" "));
    // use element access by index to do what you want
}

Also if you are familiar with stream api:

Files.lines(Paths.get("prova.txt"))
        .flatMap(s -> Arrays.stream(s.split(" ")))
        .forEach(System.out::println);
Ruslan
  • 6,090
  • 1
  • 21
  • 36
0

Use the Java NIO API.

Path myPath = Paths.get("prova.txt")
List<String> contents = Files.readAllLines(myPath)
for(String line : contents) {
    System.out.println(line);
}
jseashell
  • 745
  • 9
  • 19
  • thx , i know about this, but don't know how to acess to a single int, like i want to do something with firts elemnt of firt line , something with elemnt 4 o 3rd line :D – Fabio Mar 01 '19 at 15:52
0

You have to iterate twice : once over the lines in the files (for example using Files.lines(...)) and then over the fields in the line (with say a for loop). Something like so :

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class Snippet {
    public static void main(String[] args) throws IOException {
        new Snippet().Load();
    }

    public void Load() throws IOException {
        Files.lines(new File("prova.txt").toPath()).forEach(line -> {
            String[] fields = line.split("\\s"); // Splits at the space
            for (String field : fields) {
                System.out.println(field);
            }
            System.out.println();
        });

    }
}

HTH!

Highbrainer
  • 750
  • 4
  • 15
0

What wrong you are doing is that you have read only first line and trying to print all that is there in first line by continuously increasing value of i which will end up with null pointer exception.I have tried the same way you are doing it. Let me know if you have any concern.

public class Snippet {
public static void Load() throws IOException {
    BufferedReader in = new BufferedReader(new FileReader("prova.txt"));

    String inputLine = in.readLine();
    // Splits at the space

    while (inputLine != null) {
        int i = 0;
        String[] fields = inputLine.split(" ");
        while (i < fields.length) {
            System.out.println(fields[i]); // prints out name
            i++;
        }
        inputLine = in.readLine();
    }

}

}

What i am doing here is reading each line splitting it based on space and print everything that came on first line and and then read next line at the end of loop.

Nawnit Sen
  • 973
  • 2
  • 7
  • 14