[Q#18607418][A#18607545] How to split the strings in a file and read them?
https://stackoverflow.com/q/18607418
I have a file with information in it. It looks like: I want to split the lines and strings and read them. I want it to look like: i used a code like this: What this gave me was I think its probably an issue with my for loop but I'm not very advanced in programming and I'll appreciate help. Thanks.
Answer
https://stackoverflow.com/a/18607545
You're part way there which is great. When reading a file, the Reader will return null when it reaches the end of the stream, meaning nothing else is available to be read. Your current approach means that you want to read at least 100 lines, but no more…this will become problematic in the future if you file size increases…it's also somewhat wasteful Instead, we should use the fact a null value indicates the end of the file.. When you split a line, it will contain a number of elements. You are using the linenum variable to print these. The problem is, you've already read and split the line, the linenum is irrelevant for this task, as it represents the number of lines you've already read, not the part of the string you've just split. Instead, you need to use a inner loop to display the individual split elements for each line… For example… Also, don't forget, if you open it, you musty close it ;) You might want to take a little more time going through Basic I/O as well ;)
APIzation
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("fileeditor.txt"));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for (String part : splited) {
System.out.println(part);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
package com.stackoverflow.api;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Human18607545 {
public static void printStringsFromFile(String str1) {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(str1));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for (String part : splited) {
System.out.println(part);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {}
}
}
}
package com.stackoverflow.api;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* How to split the strings in a file and read them?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/18607545">https://stackoverflow.com/a/18607545</a>
*/
public class APIzator18607545 {
public static void splitString(String str1) throws Exception {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(str1));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for (String part : splited) {
System.out.println(part);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {}
}
}
}