只是爲了您的信息:我有與此代碼有關的另一個問題,我已經問過它,我編輯了這個問題(完全)的問題,但我沒有得到任何答覆。我的FileWriter文件是完全空的
目的:我試圖讀取文件(包含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 = " ";
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&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>
輸出我想: 微軟(R)的Windows(R)Server 2003企業版, Service Pack 1,60
我讀的東西(供您參考): Java txt File from FileWriter is empty
UPDATE:
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 = " ";
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:
文件不輸出(噸愛情的那些誰幫助)
這是怎麼回事
<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>
任何提示/提示/技巧,可能使其可讀?
另外要注意,'String.contains()'是大小寫敏感的。您的搜索字符串是「窗口」,但您搜索的行包含「Windows」。 – csmckelvey
我嘗試了兩個提供的建議,但文件仍然是空的 – ToxicGlow
您現在搜索了「Windows」(大寫)_and_將while()語句移出while循環嗎? – csmckelvey