Reputation: 71
Here is the edited version of my previous question:
So here is my AIM : To read in a .txt file (HTML file) and put the required contents into a .txt file.
Now, this HTML file contains tons of tables and formatting which I don't need, I just need the contents
import java.io.*;
public class File {
public static void main(String[] args) throws IOException
{
try{ String input = "out.txt";
BufferedReader in = new BufferedReader(new FileReader(input));
String output = "output.txt";
BufferedWriter out = new BufferedWriter( new FileWriter( output ) );
String inputLine = "";
int i=0;
while ( ( inputLine = in.readLine( ) ) != null ) {
if ( inputLine.contains( "Windows" ) ) {
out.append( inputLine );
out.newLine( );
}
in.close();
out.close();
}
}
It DOES make a file named "output.txt" but it is COMPLETELY empty.
How does it exactly sort the string? Is it word-by-word or sentence by sentence?
Here is a sample of the file. (A bit of it)
<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>
Can it execute it simply as Microsoft(R) Windows (R) Server Enterprise Edition , 60 ?
UPDATE:
It also executes the "hi"
Upvotes: 0
Views: 201
Reputation: 4694
BufferedWriter
can not be instantiated with a string argument. Use this instead:
BufferedWriter out = new BufferedWriter(new FileWriter( output ));
Also, there are some errors in the code:
It can be simplified as this :
try {
String input= "D:\\input.txt";
BufferedReader in = new BufferedReader( new FileReader( input ) );
String output = "D:\\output.txt";
BufferedWriter out = new BufferedWriter( new FileWriter( output ) );
String inputLine = "";
while ( ( inputLine = in.readLine( ) ) != null ) {
if ( inputLine.contains( "Windows" ) ) {
out.append( inputLine );
out.newLine( );
}
}
in.close( );
out.flush( );
out.close( );
} catch ( Exception e ) {
}
Upvotes: 2
Reputation: 587
Is it a compile time error and not a runtime error you're getting? if so, it could be because BufferedWriter does not have a constructor that takes a String, only another Writer. Instead, try:
String output="output.txt";
BufferedWriter out = new BufferedWriter(new FileWriter(output));
BufferedWriter is designed to wrap around other Writer classes to prevent each inputted byte from being written directly to the underlying output stream.
Upvotes: 0
Reputation: 13807
Also try to instantiate your arrays! You have String Workstation[];
and String Server[];
but in this state they are empty references pointing nowhere, and will give you nullpointer exceptions, after you fix the compile errors.
Try initializing them like String Server[] = new String[sizeOfArray];
or consider using a List<String>
instead.
Upvotes: 0