简单的使用代码将字符串输入到txt文本中

将字符串hello world写入到文本中,并且保存到指定的磁盘中,在这里使用到了字符输入流FileWriter指定路径还使用了writer的方法写入字符串,使用完之后记得close()关闭字符输入流。

public static void main(String[] args) {


        String str="hello world";
        FileWriter fw=null;
        try {
            fw=new FileWriter("E:\\hello.txt");
            fw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     }
发布了152 篇原创文章 · 获赞 141 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_44739706/article/details/105264268