BufferedReader not reading the entire file and not exiting the loop - java

I have ini file that to be read in my application but the problem is it is not reading the entire file and it stucks in the while loop.
My code:
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
Properties section = null;
while(line!=null){
if(line.startsWith("[") && line.endsWith("]")){
section = new Properties();
this.config.put(line.substring(1, line.length() - 1), section);
}else{
String key = line.split("=")[0];
String value = line.split("=")[1];
section.setProperty(key, value);
}
line = br.readLine();
System.out.println(line);
// To continue reading newline.
//if i remove this, it will not continue reading the second header
if(line.equals("")){
line = br.readLine();
}
}
System.out.println("Done"); // Not printing this.
This is what inside the ini file. The newlines are included so I add if the line.equals("").
[header]
key=value
[header2]
key1=value1
key2=value2
[header3]
key=value
// -- stops here
//this newlines are included.
#Some text // stops here when I remove all the newlines in the ini file.
#Some text
Output:
[header]
key=value
[header2]
key1=value1
key2=value2
[header3]
key=value
//whitespace
//whitespace
UPDATE:
I remove all the newlines in the ini file but still not reading the entire file.

Unless there's something you've not included in this post the logic won't get stuck in the loop... If the file you're using looks exactly like what you've posted, it'll hit either a blank line(because you're only skipping 1 blank) or one of the lines starting "#" and get an ArrayIndexOutOfBoundsException because those lines don't contain an "="... Simplify your while loop to this and the ArrayIndexOutOfBoundsExceptions wont occur and it'll process the full file:
Properties section = null;
String line = null;
while ((line = br.readLine()) != null) {
if (line.startsWith("[") && line.endsWith("]")) {
section = new Properties();
this.config.put(line.substring(1, line.length() - 1), section);
} else if (line.contains("=") && !line.startsWith("#")) {
String[] keyValue = line.split("=");
section.setProperty(keyValue[0], keyValue[1]);
}
}
Notice that I'm doing a line.contains("=") so that blank lines and lines beginning # are skipped over...

Related

How to read every line of a text file including empty lines

I want to be able to read an entire text file that has empty lines between text. Every solution I try to implement seems to stop reading after it reaches an empty line. I want to be able to read an entire text file, including empty lines, and store the contents in a String. This is what I have now. I included two implementations. How can I alter either of the implementations to continue reading after an empty line? Also, I want the empty lines in the text file to be included in the String that it is being stored in.
File templateFile = new File(templatePath);
String oldContent = "";
BufferedReader reader = new BufferedReader(new FileReader(templateFile));
//Implementation 1
String line = reader.readLine();
while(line != null) {
oldContent = oldContent + line + System.lineSeparator();
line = reader.readLine();
}
/* Implementation 2
Scanner sc = new Scanner(templateFile);
while(sc.hasNext()) {
oldContent = sc.nextLine();
} */
Using java 11 java.nio.file.Files.readString()
oldContent = Files.readString(Paths.get(templatePath));

Remove stop words from file - going over it multiple times causes content duplication and does not remove the words

I am trying to go over a bunch of files, read each of them, and remove all stopwords from a specified list with such words. The result is a disaster - the content of the whole file copied over and over again.
What I tried:
- Saving the file as String and trying to look with regex
- Saving the file as String and going over line by line and comparing tokens to the stopwords that are stored in a LinkedHashSet, I can also store them in a file
- tried to twist the logic below in multiple ways, getting more and more ridiculous output.
- tried looking into text / line with the .contains() method, but no luck
My general logic is as follows:
for every word in the stopwords set:
while(file has more lines):
save current line into String
while (current line has more tokens):
assign current token into String
compare token with current stopword:
if(token equals stopword):
write in the output file "" + " "
else: write in the output file the token as is
Tried what's in this question and many other SO questions, but just can't achieve what I need.
Real code below:
private static void removeStopWords(File fileIn) throws IOException {
File stopWordsTXT = new File("stopwords.txt");
System.out.println("[Removing StopWords...] FILE: " + fileIn.getName() + "\n");
// create file reader and go over it to save the stopwords into the Set data structure
BufferedReader readerSW = new BufferedReader(new FileReader(stopWordsTXT));
Set<String> stopWords = new LinkedHashSet<String>();
for (String line; (line = readerSW.readLine()) != null; readerSW.readLine()) {
// trim() eliminates leading and trailing spaces
stopWords.add(line.trim());
}
File outp = new File(fileIn.getPath().substring(0, fileIn.getPath().lastIndexOf('.')) + "_NoStopWords.txt");
FileWriter fOut = new FileWriter(outp);
Scanner readerTxt = new Scanner(new FileInputStream(fileIn), "UTF-8");
while(readerTxt.hasNextLine()) {
String line = readerTxt.nextLine();
System.out.println(line);
Scanner lineReader = new Scanner(line);
for (String curSW : stopWords) {
while(lineReader.hasNext()) {
String token = lineReader.next();
if(token.equals(curSW)) {
System.out.println("---> Removing SW: " + curSW);
fOut.write("" + " ");
} else {
fOut.write(token + " ");
}
}
}
fOut.write("\n");
}
fOut.close();
}
What happens most often is that it looks for the first word from the stopWords set and that's it. The output contains all the other words even if I manage to remove the first one. And the first will be there in the next appended output in the end.
Part of my stopword list
about
above
after
again
against
all
am
and
any
are
as
at
With tokens I mean words, i.e. getting every word from the line and comparing it to the current stopword
After awhile of debugging I believe I have found the solution. This problem is very tricky as you have to use several different scanners and file readers etc. Here is what I did:
I changed how you added to your StopWords set, as it wasn't adding them correctly. I used a buffered reader to read each line, then a scanner to read each word, then added it to the set.
Then when you compared them I got rid of one of your loops as you can easily use the .contains() method to check if the word was a stopWord.
I left you to do the part of writing to the file to take out the stop words, as I'm sure you can figure that out now that everything else is working.
-My sample stop words txt file:
Stop words
Words
-My samples input file was the exact same, so it should catch all three words.
The code:
// create file reader and go over it to save the stopwords into the Set data structure
BufferedReader readerSW = new BufferedReader(new FileReader("stopWords.txt"));
Set<String> stopWords = new LinkedHashSet<String>();
String stopWordsLine = readerSW.readLine();
while (stopWordsLine != null) {
// trim() eliminates leading and trailing spaces
Scanner words = new Scanner(stopWordsLine);
String word = words.next();
while(word != null) {
stopWords.add(word.trim()); //Add the stop words to the set
if(words.hasNext()) {
word = words.next(); //If theres another line, read it
}
else {
break; //else break the inner while loop
}
}
stopWordsLine = readerSW.readLine();
}
BufferedReader outp = new BufferedReader(new FileReader("Words.txt"));
String line = outp.readLine();
while(line != null) {
Scanner lineReader = new Scanner(line);
String line2 = lineReader.next();
while(line2 != null) {
if(stopWords.contains(line2)) {
System.out.println("removing " + line2);
}
if(lineReader.hasNext()) { //If theres another line, read it
line2 = lineReader.next();
}
else {
break; //else break the first while loop
}
}
lineReader.close();
line = outp.readLine();
}
OutPut:
removing Stop
removing words
removing Words
Let me know if I can elaborate any more on my code or why I did something!

reading input from file until specific word is read

i am writing a java program to read a file and print output to another string variable.which is working perfectly as intended using is code.
{
String key = "";
FileReader file = new FileReader("C:/Users/raju/Desktop/input.txt");
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
while (line != null) {
key += line;
line = reader.readLine();
}
System.out.println(key); //this prints contents of .txt file
}
this prints whole text in the file.But i want to only print the lines till word END is encountered in file.
example: if input.txt file contains following text : this test file END extra in
it should print only :
this test file
Just do a simple indexOf to see where it is and if it exists in the line. If the instance is found one option would be using substring to cut off everything up until the index of the keyword. For a bit more control though try using java regular expressions.
String key = "";
FileReader file = new FileReader("C:/Users/raju/Desktop/input.txt");
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
while ((line = reader.readLine()) != null && line.indexOf("Keyword to look for") == -1)
key += line;
System.out.println(key);
I am not sure why it needs to be any more complicated than this:
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String str = re.readLine();
if (str.equals("exit")) break;
// whatever other code.
}
You can do it in many ways. one of them is using indexOf method to specify the start index of "END" in input and then using subString method.
for more information, read documentation of String calss. HERE
This will work for your issue.
public static void main(String[] args) throws IOException {
String key = "";
FileReader file = new FileReader("/home/halil/khalil.txt");
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
while (line != null) {
key += line;
line = reader.readLine();
} String output = "";
if(key.contains("END")) {
output = key.split("END")[0];
System.out.println(output);
}
}
You have to change your logic to check if the line contains "END".
If END not found in a line, add the line to key stringin your program
If yes, split that line into word array, read the line till you encounter the word "END" and append it to your key string. Consider using Stringbuilder for key.
while (line != null) {
line = reader.readLine();
if(!line.contains("END")){
key += line;
}else{
//Note that you can use split logic like below, or use java substring
String[] words = line.split("");
for(String s : words){
if(s.equals("END")){
return key;
}
key += s;
}
}
}

Java Read numbers from txt file

Let's assume we have a text file like this one:
#this is a config file for John's server
MAX_CLIENT=100
# changed by Mary
TCP_PORT = 9000
And I have written the following code :
this.reader = new BufferedReader(new FileReader(filename));
String line;
line = reader.readLine();
while (line.length() != 0) {
line = line.trim();
if (line.contains("#") || line.contains("")) {
line = reader.readLine();
} else {
if (line.contains("MAX_CLIENT=")) {
line = line.replace("MAX_CLIENT=", "");
this.clientmax = Integer.parseInt(line);
}
if (line.contains("TCP_PORT=")) {
line = line.replace("TCP_Port=", "");
tcp_port = Integer.parseInt(line);
}
}
}
where clientmax and tcp_port are of type int.
Will clientmax and tcp_port receive their value for this code?
What should I do if I the text file changes a little bit to:
MAX_CLIENT=100# changed by Mary
containing a comment after the number.
ow,btw # represents the start of a comment.
Thank you.
You should use a class designed for this purpose: Properties
This class handles comments for you so you don't have to worry about them.
You use it like this:
Properties prop = new Properties();
prop.load(new FileInputStream(filename));
Then you can extract properties form it using:
tcpPort = Integer.parseInt(prop.getProperty("tcpPort"));
Or save using:
prop.setProperty("tcpPort", String.valueOf(tcpPort));
Use line.startsWith("#") instead of line.contains("#")
When you do this, keep in mind that you need to stop reading when you reach a comment character.

want to start reading a file from a specific point

I have a file that contains log print out and I want to read certain lines and from certain point from the middle to the end of the line and move to the next line that match the criteria.
I want to start reading from RULE EXECUTING to the end of the line and then check the next line of it has RULE EXECUTING if not skip it to the following line if that does have RULE EXECUTING then copy from that point to the end of the line.
FILE SAMPLE
2013-02-14 09:26:20:078 [main] DEBUG sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd - RULE EXECUTING --> CMNETSL.hdjjjdlskdnlskd.jgfkdflkdfl_Translation
2013-02-14 09:28:00:312 [main] DEBUG moc.uty.lweifoisd.sfsd.kjfdnkjs.RulesetInvoker - Rudejgfjkgjf: After invoking: CMNETSLO
2013-02-14 09:26:20:421 [main] DEBUG sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd - RULE EXECUTING --> sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd
what I want to get from the line would look like this
RULE EXECUTING --> CMNETSL.hdjjjdlskdnlskd.jgfkdflkdfl_Translation
RULE EXECUTING --> sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd
So, something like:
BufferedReader br = new BufferedReader(new FileReader("mylog.log"));
String line;
int idx;
while((line = br.getLine()) != null)
if((idx = line.indexOf("RULE EXECUTING --> ")) != -1)
System.out.println(line.substring(idx));
I've not tried that in an ide or compiled it, but I think that you probably get the idea.
Scanner s = new Scanner(new File("log.txt")); // read file using scanner line by line
while(s.hasNextLine())
{
String nextLine = s.nextLine();
if(nextLine.startsWith("RULE EXECUTING")) //check if line starts with the key word
{
// do whatever you want to do
}
}
You have to read the entire file for that, use a FileReader and read the file line for line, searching for the substring "RULE EXECUTING" in each. If it is found, use the substring method to output or otherwise process the substring. Repeat until the file is done and - most importantly - close the file.
Here is a complete codesample:
BufferedReader reader = null;
try {
//open file
reader = new BufferedReader(new FileReader("example.log"));
int index;
//read first line
String line = reader.readLine();
//go through the entire file line for line
while (line != null) {
//look for substring in line
index = line.indexOf("RULE EXECUTING");
if(index >= 0 ) {
//substring found, write result to stdout
System.out.println(line.substring(index));
}
//read next line
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//always close file readers!
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Categories

Resources