Java创建TXT文件并写入 内容

public static void main(String[] args)
    {
        String filePath = "E:/" + "1.txt";
        FileWriter fw = null;
        try
        {
            File file = new File(filePath);
            if (!file.exists())
            {
                file.createNewFile();
            }
            fw = new FileWriter(filePath);
            BufferedWriter bw=new BufferedWriter(fw);
            bw.write("hello\n");
            bw.write("world\n");
            bw.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                fw.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

    }

 

posted @ 2022-05-11 17:13  南北12345678  阅读(3561)  评论(0编辑  收藏  举报