我现在想把
Bufferedreader br=new Bufferedreader();
string line="";
while((line=br.readline())!=null)
{
   //添加代码处
}
想把每次line的内容添加到一个文本文件里。

解决方案 »

  1.   

    if (newFile.canWrite()) {
            try {
              FileOutputStream out = new FileOutputStream(newFile, true);
              out.write(s.toString().getBytes());
              out.close();
            } catch (FileNotFoundException e) {
              log.error("文件找不到" + e.getMessage());
              e.printStackTrace();
              return false;
            } catch (IOException e) {
              log.error("写文件失败");
              e.printStackTrace();
              return false;
            }s为变量既要输入的内容
      

  2.   

    刚刚写了个测试、
    public class TestData {
    public static void ReadData() throws SQLException {
    try {
    FileReader read = new FileReader("E:/test1.txt");
    BufferedReader br = new BufferedReader(read);
    String row; File f = new File("E://test.txt");
    if (f.exists()) {
       f.delete();
    }
    f.createNewFile();
    FileOutputStream fos = new FileOutputStream(f);
    PrintWriter pw = new PrintWriter(fos);
    while ((row = br.readLine()) != null) {
    pw.write(row);
    pw.println();
    pw.flush();
    }
    fos.close();
    pw.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) throws Exception {
    ReadData();
    }
    }