Reputation: 71
Just for your info: I had another question relating to this code, and I had already asked it, I edited the question to this question (completely) but I got no responses.
AIM: I am trying to read in a file(.txt containing HTML) and sort the content. It does create a text file and it completely empty. I read some similar situations but I have seen their mistake was not adding a out.flush() or out.close() file.
Here is the code till now:
import java.io.*;
public class File {
public static void main(String[] args) throws IOException {
try {
String input = "SCCM.txt";
BufferedReader in = new BufferedReader(new FileReader(input));
String output = "output.txt";
BufferedWriter out = new BufferedWriter( new FileWriter( output ) );
String inputLine = "", s="windows";
String regex = " ";
while ((inputLine = in.readLine()) != null) {
if ( inputLine.contains(s) ) {
inputLine.split(regex);
out.append( inputLine );
out.newLine( );
}
in.close();
out.flush();
out.close();
}
}
catch(IOException e) {
System.out.println("Hi");
}
}
}
Content I want to sort :
<TR class="RowDark">
<TD width=0><A href="Report.asp?ReportID=100&sp=Service+Pack+1&os=Microsoft%28R%29+Windows%28R%29+Server+2003%2C+Enterprise+Edition"><IMG border=0 src="images/icolink3.gif" alt="Open the target" width=11 height=11></A></TD>
<TD class=SimpleTextSmall> Microsoft(R) Windows(R) Server 2003, Enterprise Edition </TD>
<TD class=SimpleTextSmall> Service Pack 1 </TD>
<TD class=SimpleTextSmall> 60 </TD>
</TR>
Output I want: Microsoft (R) Windows (R) Server 2003 , Enterprise Edition , Service Pack 1 , 60
Stuff I read (for your information): Java txt File from FileWriter is empty
UPDATE:
import java.io.*;
public class File {
public static void main(String[] args) throws IOException {
String input = "SCCM.txt";
BufferedReader in = new BufferedReader(new FileReader(input));
String output = "output.txt";
BufferedWriter out = new BufferedWriter( new FileWriter( output ) );
try {
String inputLine = "", s="Windows";
String regex = " ";
while ((inputLine = in.readLine()) != null) {
if ( inputLine.contains(s) ) {
inputLine.split(regex);
out.write(inputLine);
out.newLine( );
}
}
}
catch(IOException e) {
System.out.println("Hi");
in.close();
out.flush();
out.close();
}
}
}
UPDATE:
The file DOES output (tons of love to those who helped)
Here is how it is
<TD width=0><A href="Report.asp?ReportID=100&sp=Service+Pack+1&os=Microsoft%28R%29+Windows%28R%29+Server+2003%2C+Enterprise+Edition"><IMG border=0 src="images/icolink3.gif" alt="Open the target" width=11 height=11></A></TD>
<TD class=SimpleTextSmall> Microsoft(R) Windows(R) Server 2003, Enterprise Edition </TD>
Any hints/tips/tricks to perhaps make it readable?
Upvotes: 0
Views: 1237
Reputation: 159784
You are closing the output stream inside the while
loop so an IOException
is thrown almost immediately but the details of the exception are not displayed. These statements
in.close();
out.flush();
out.close();
should be in a finally block
try {
...
catch (IOException e) {
e.printStackTrace(); // add me
} finally {
in.close();
out.flush();
out.close();
}
Only then can you check for the String Windows
(as mentioned in the comments)
String s = "Windows";
Upvotes: 2
Reputation: 94469
inputLine
never contains windows
however it does contain Windows
, which causes the the conditional to always evaluate to false, preventing the append method from being called.
Change the following line:
String inputLine = "", s="windows";
to
String inputLine = "", s="Windows";
or lowercase s
before the comparison:
Upvotes: 0