我正在創建一個應用程序,它將獲取表格中的所有rpms
,以及當我想將它附加到文本文件時出錯的情況,請參閱下面的代碼。將字符串追加到文件中
public class rpms(){
public static void main(String[] args) {
URLget rpms = new URLget();
try {
getTdSibling(sendGetRequest(URL).toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getTdSibling(String sourceTd) throws FileNotFoundException, UnsupportedEncodingException {
String fragment = sourceTd;
Document doc = Jsoup.parseBodyFragment(fragment);
for (Element table : doc.select("table")) {
for (Element row : table.select("tr")) {
Elements lines = row.select("td");
String linesToStr = lines.text();
String[] linestoStrArray = linesToStr.split("\n");
for (String line : linestoStrArray)
if (!line.contains("Outdated")){
//System.out.println(""+line);// display the rpms that do not have outdated
for (int i = 0; i < lines.size(); i++) {
if(!lines.eq(i).text().toString().equals(" ")){
splitStr(lines.eq(i).text().toString());
}
}
}
}
}
}
public static void splitStr(String str1) throws FileNotFoundException, UnsupportedEncodingException{
ArrayList<String> outputContent = new ArrayList<String>();
String[] split1 = str1.split(" ");
for (int i = 0; i < split1.length; i++) {
if(fileExplode(split1[i])){
System.out.println(split1[i]);
outputContent.add(split1[i]);
}
}
copyFile(outputContent);
}
public static void copyFile(ArrayList<String> fileCon1) throws FileNotFoundException, UnsupportedEncodingException{
PrintWriter writer1 = new PrintWriter("C:\\Users\\usersb\\Downloads\\rpms\\newrpms.txt", "UTF-8");
for(int i = 0 ; i < fileCon1.size() ; i++){
writer1.println(fileCon1.get(i));
}
System.out.println("updated newrpms.txt");
writer1.close();
}
public static boolean fileExplode(String str1) {
boolean hasRPM = false;
String[] split1 = str1.replace(".", " ").split(" ");
for (int i = 0; i < split1.length; i++) {
if ((i + 1) == split1.length) {
if (split1[i].endsWith("rpm")
|| (split1[i].length() > 2 && split1[i].charAt(0) == '.' && split1[i].charAt(1) == 'r'
&& split1[i].charAt(2) == 'p' && split1[i]
.charAt(3) == 'm')) {
hasRPM = true;
}
break;
}
}
return hasRPM;
}
}
執行完代碼後。該文件是空的。我該怎麼辦之前打個電話關閉的PrintWriter進入這個statemen顯示的輸出相同System.out.println(split1[i]);
你能解釋一下嗎我執行代碼後。該文件是空的。我該怎麼做才能得到相同的輸出顯示在這個狀態System.out.println(split1 [i]);? – 2014-09-24 07:15:40
你在哪裏定義了outputContent? – 2014-09-24 07:17:42
我的錯誤我忘了複製初始化。我做了編輯。 – user3278908 2014-09-24 07:23:25