我的FileWriter文件完全为空

时间:2014-02-14 00:20:19

标签: java html

仅为了您的信息:我有另一个与此代码相关的问题,我已经问过它,我已经将问题编辑到了这个问题(完全),但我没有得到回复。

AIM:我正在尝试读取文件(包含HTML的.txt)并对内容进行排序。它确实创建了一个文本文件,它完全为空。 我读了一些类似的情况,但我看到他们的错误是没有添加out.flush()或out.close()文件。

这是现在的代码:

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");
        }
    }
}

我要排序的内容:

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

我想要的输出: Microsoft(R)Windows(R)Server 2003,Enterprise Edition,Service Pack 1,60

我读过的东西(供参考): Java txt File from FileWriter is empty

更新:

import java.io.*;

公共类文件{

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();
    }
}

}

更新:

文件DOES输出(对那些帮助过的人的爱很多)

这是怎么回事

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

任何可能使其可读的提示/提示/技巧?

2 个答案:

答案 0 :(得分:2)

您正在关闭while循环内的输出流,因此几乎立即抛出IOException,但不会显示异常的详细信息。这些陈述

in.close();
out.flush();
out.close();

应该在finally块中

try {
   ...
catch (IOException e) {
    e.printStackTrace(); // add me
} finally {
    in.close();
    out.flush();
    out.close();
}

只有这样才能检查字符串Windows(如上所述in the comments

String s = "Windows";

答案 1 :(得分:0)

inputLine从不包含windows,但它确实包含Windows,这会导致条件总是计算为false,从而阻止调用append方法。

更改以下行:

String inputLine = "", s="windows";

String inputLine = "", s="Windows";
比较前的

或小写s