Java创建TXT文件并写入 内容

    public static void main(String[] args)
    {
        String filePath = "E:/" + "1.txt";
        String content = "这是txt文件";
        FileWriter fw = null;
        try
        {
            File file = new File(filePath);
            if (!file.exists())
            {
                file.createNewFile();
            }
            fw = new FileWriter(filePath);
            fw.write(content);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                fw.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

    }

 

posted @ 2021-06-03 11:27  云村的王子  阅读(5211)  评论(0编辑  收藏  举报