ToxicGlow
ToxicGlow

Reputation: 71

My FileWriter file is completely empty

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 = "&nbsp";
            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&amp;sp=Service+Pack+1&amp;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>&nbsp;Microsoft(R)&nbspWindows(R)&nbspServer&nbsp2003,&nbspEnterprise&nbspEdition&nbsp;</TD>
    <TD class=SimpleTextSmall>&nbsp;Service&nbspPack&nbsp1&nbsp;</TD>
    <TD class=SimpleTextSmall>&nbsp;60&nbsp;</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 = "&nbsp";
        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&amp;sp=Service+Pack+1&amp;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>&nbsp;Microsoft(R)&nbspWindows(R)&nbspServer&nbsp2003,&nbspEnterprise&nbspEdition&nbsp;</TD>

Any hints/tips/tricks to perhaps make it readable?

Upvotes: 0

Views: 1237

Answers (2)

Reimeus
Reimeus

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

Kevin Bowersox
Kevin Bowersox

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

Related Questions