关闭

java写文件

别叨叨 1年前 ⋅ 88 阅读
public class WriteFileUtils {
    
    /**
     * 将字节数组写入文件
     *
     * @param path 文件路径
     * @param data 文件内容
     */
    public static void writeFileByOutputStream(byte[] data, String path) {
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(path);
            outputStream.write(data, 0, data.length);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 将字节数组写入文件
     *
     * @param path 文件路径
     * @param data 文件内容
     */

    public static void writeFile(byte[] data, String path) {
        try {
            Files.write(Paths.get(path), data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入次数多时用
     *
     * @param path 文件路径
     * @param data 文件内容
     */
    public static void writeFileByBufferedWriter(String data, String path) {
        File file = new File(path);
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.flush();
                bufferedWriter.flush();
                bufferedWriter.close();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param path   文件路径
     * @param data   文件内容
     * @param append 是否是追加内容 true追加,false覆盖
     */
    public static void writeFileByFileWriter(String path, String data, boolean append) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(path, append);
            fileWriter.append(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fileWriter) {
                try {
                    fileWriter.flush();
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 写入次数少时用
     *
     * @param path 文件路径
     * @param data 文件内容
     */
    public static void writeFileByFileWriter(String data, String path) {
        File file = new File(path);
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file);
            fileWriter.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * @param path 文件路径
     * @param data 文件内容
     */
    public static void writeFileByPrintWriter(String data, String path) {
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(new FileWriter(path));
            printWriter.print(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != printWriter) {
                printWriter.flush();
                printWriter.close();
            }
        }
    }

    /**
     * @param path 文件路径
     * @param data 文件内容
     */
    public static void writeFileBufferedOutputStream(String data, String path) {
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedOutputStream = new BufferedOutputStream(
                    new FileOutputStream(path));
            bufferedOutputStream.write(data.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

全部评论: 0

    我有话说: