ToxicGlow
ToxicGlow

Reputation: 71

An empty file with no contents

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&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>

Can it execute it simply as Microsoft(R) Windows (R) Server Enterprise Edition , 60 ?

UPDATE:

It also executes the "hi"

Upvotes: 0

Views: 201

Answers (3)

Manoj Shrestha
Manoj Shrestha

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:

  1. the array Servers[] has not been initialized
  2. it's reading the input file three times before setting it to Servers[]

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

Joshua Hutchison
Joshua Hutchison

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

Bal&#225;zs &#201;des
Bal&#225;zs &#201;des

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

Related Questions