【问题标题】:My FileWriter file is completely empty我的 FileWriter 文件完全为空
【发布时间】:2014-02-14 09:06:59
【问题描述】:

仅供参考:我有另一个与此代码有关的问题,我已经问过了,我(完全)编辑了这个问题的问题,但没有得到任何答复。

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

}

更新:

文件确实输出了(对帮助过的人表示爱意)

是这样的

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

有什么提示/提示/技巧可以让它变得可读吗?

【问题讨论】:

  • 还要注意String.contains() 区分大小写。您的搜索字符串是“windows”,但您搜索的行包含“Windows”。
  • 我尝试了提供的两个建议,但文件仍然是空的
  • 您现在已搜索“Windows”(大写)并将您的 close() 语句移出 while 循环?
  • 我给了代码的更新版本!是的,我遵循了这两个步骤! :)
  • @ToxicGlow 您需要将关闭语句放在 finally 块而不是异常块中,如下所示:)

标签: java html


【解决方案1】:

您正在关闭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";

【讨论】:

  • 好奇,你是如何链接到这样的特定评论的?
  • @Takendarkk 链接可以在发布时间找到,例如 8 分钟前
【解决方案2】:

inputLine 从不包含windows,但它确实包含Windows,这会导致条件始终评估为假,从而阻止调用 append 方法。

更改以下行:

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

到

String inputLine = "", s="Windows";

或者比较前的小写s:

【讨论】:

  • 我尝试了你的建议,谢谢 :) 但我的文件仍然是空的
猜你喜欢
  • 2017-09-22
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
相关资源
最近更新 更多