Click here to Skip to main content
15,896,540 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How I read specific coloum from the text file and save the coloum in an array?
the file:
8

- 3,4 2 0

- 5 3 0

1 6 2 1

1 7,8 3 1

2 7 2 1

3 8 3 2

4,5 8 2 2

4,6,7 - 1 3


What I have tried:

I did not found tired for my quesion
Posted
Updated 3-Apr-18 7:49am
v2

1 solution

Steps to do:
1. Read text file: Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Classes > Basic I/O)[^]
2. Loop through the collection of lines and split single line into parts: String.split[^]
3. Use index of array to get proper "column":

Java
String[] lines = Files.readAllLines(Paths.get("FullFileName.txt"));
for (String line : lines) {
    String[] parts = line.split(" "); //space as column delimiter
    //parts[3] -  4. column
    System.out.println(parts[3]);
}


For further details, please see:
Different ways of Reading a text file in Java - GeeksforGeeks[^]
Java Read Text File Examples - JavaDevNotes[^]

Good luck!
 
Share this answer
 
Comments
Christiaan van Bergen 3-Apr-18 14:04pm    
That is of course when the text file is space delimited . Maybe splitting this one per character?
Maciej Los 3-Apr-18 14:31pm    
I was looking at file text content posted by OP. Seems it's space delimited ;)
Christiaan van Bergen 3-Apr-18 14:36pm    
Yes, it appears so, but I was confused by the '-' (minus characters) in the sample and the two commas in the last line. So, I thought the OP might mean columns per character. But hey, your example should help <<thumbsup>>
Maciej Los 3-Apr-18 14:40pm    
As same as this line is confusing: 4,6,7 - 1 3?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900