用Java编写TextFile [英] Writing to a TextFile in Java

查看:158
本文介绍了用Java编写TextFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将字符串写入文本文件,但有一点问题。我在此站点的其他问题的帮助下完成了我的代码,但是当我尝试将字符串添加到文本文件时,它会删除该文本文件中的所有内容并写入输入。但是我想让它去下一行并写下来。我解决不了。我将不胜感激任何帮助。谢谢..

Hi there I'm trying to write strings to a textfile but there is a little problem. I completed my code with the help of the other questions at this site but when i try to add strings to a text file it erases everything in that text file and writes the input. But I want it to go to the nextline and write it. I couldn't solve it. I would appreciate any help. Thank you..

public static void addCar() throws IOException{

        String string = transferBrand;
        String string2 = ":"+transferModel;
        System.out.println(string+string2);
        File file = new File("HatchBack.txt");
        try {
            StringReader stringReader = new StringReader(string+string2);
            BufferedReader bufferedReader = new BufferedReader(stringReader);
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            for(String line = bufferedReader.readLine(); line != null; line =bufferedReader.readLine()) {               
                bufferedWriter.write(line);
                bufferedWriter.newLine();                   
            }
            bufferedReader.close();
            bufferedWriter.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }       
}


推荐答案

(你是否尝试过JavaDoc中提到的这个?

(untested) Did you try this as mentioned in JavaDoc?

FileWriter fileWriter = new FileWriter(file, true);

这篇关于用Java编写TextFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆