将字符串附加到文件

时间:2014-09-24 07:09:29

标签: java eclipse append jsoup add

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

 }

  }

执行代码后。该文件为空。我该怎么做才能在此州议员System.out.println(split1[i]);

中显示相同的输出

1 个答案:

答案 0 :(得分:0)

在关闭PrintWriter writer1.flush();

之前添加writer1.close();

这是一个用于将String值写入文本文件的工作代码,请在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();