Java文件 *** 作:以追加的方式输出文本

Java文件 *** 作:以追加的方式输出文本,第1张

Java文件 *** 作:以追加的方式输出文本
    // 以追加的方式将文本输出到本地文件中
    public static void writeTextAppendLocalFile(String localFilePath, String content) {
        File file = new File(localFilePath);
        file.mkdir();
        file = new File(localFilePath + "\java-input-file.txt");
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;

        try {
            if (!file.exists()) {
                boolean hasFile = file.createNewFile();
                if (hasFile) {
                    // System.out.println("file not exists, create new file");
                }
                fos = new FileOutputStream(file);
            } else {
                // System.out.println("file exists");
                fos = new FileOutputStream(file, true);
            }

            osw = new OutputStreamWriter(fos, "utf-8");
            osw.write(content);
            osw.write("rn");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (osw != null) {
                    osw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存