2014-09-24 104 views
-2

我正在创建一个应用程序,它将获取表格中的所有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]);

+0

你能解释一下吗我执行代码后。该文件是空的。我该怎么做才能得到相同的输出显示在这个状态System.out.println(split1 [i]);? – 2014-09-24 07:15:40

+0

你在哪里定义了outputContent? – 2014-09-24 07:17:42

+0

我的错误我忘了复制初始化。我做了编辑。 – user3278908 2014-09-24 07:23:25

回答

0

添加writer1.flush();writer1.close();

这是写一个字符串值,以文本文件中的工作代码,尝试这在你的copyFile方法中

PrintWriter out = null; 
    try { 
     out = new PrintWriter("C:\\45.txt"); //path where you want to create your file 
    } catch (FileNotFoundException ex) { 

    } 

    StringBuffer writesb = null; 
    writesb = new StringBuffer(); 

    //append text 
    writesb.append("w"); 
    //Line Spacer.writes to a new line 
    writesb.append(System.getProperty("line.separator")); 
    writesb.append("e"); 

    out.print(writesb.toString()); 
    out.flush(); 
    out.close(); 
+0

是的,它的工作,但它不能得到我想要在我的文件中显示的价值 – user3278908 2014-09-24 08:16:03

+0

根据你的代码你没有冲洗你的PrintWriter .....没有冲洗它会只需创建文本文件而不写内容。所以添加一个writer1.flush();在writer1.close()之前;以复制文件()方法..(希望你还没有检查它) – Kandy 2014-09-24 08:59:11

+0

让我知道如果你有你的代码... – Kandy 2014-09-24 09:04:28