java读取文件操作

public class FileUtil {
    public static String readFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            LogUtil.info("读取文件失败:文件不存在," + filePath);
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        InputStream is = null;
        BufferedReader reader = null;
        try {
            is = new FileInputStream(filePath);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = reader.readLine(); // 读取第一行
            buffer.append(line); // 将读到的内容添加到 buffer 中
            while ((line = reader.readLine()) != null) { // 如果 line 为空说明读完了
                buffer.append(System.getProperty("line.separator")); // 添加换行符
                buffer.append(line); // 将读到的内容添加到 buffer 中
            }
        } catch (IOException ex) {
            LogUtil.error("读取文件失败:" + ex.getStackTrace());
        } finally {
            try {
                reader.close();
                is.close();
            } catch (Exception ex) {

                LogUtil.error("读取文件关闭时失败:" + ex.getStackTrace());
            }
        }
        return buffer.toString();
    }

    public static void writeFile(String filePath, String content) {
        createOrCleanFile(filePath);
        OutputStream os = null;
        BufferedWriter writer = null;
        try {
            os = new FileOutputStream(filePath);

            writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(content);
            writer.flush();
        } catch (IOException ex) {
            LogUtil.error("写文件失败:" + ex.getStackTrace());
        } finally {
            try {
                writer.close();
                os.close();
            } catch (Exception ex) {

                LogUtil.error("写文件关闭时失败:" + ex.getStackTrace());
            }
        }
    }

    public static void setFileLength(String filePath, Integer length) throws Exception {
        try {
            RandomAccessFile raf = new RandomAccessFile(filePath, "rw");
            raf.setLength(0);
            raf.close();
        } catch (Exception ex) {
            LogUtil.error("写文件失败:" + ex.getStackTrace());
            throw ex;
        }
    }

    public static boolean createOrCleanFile(String filePath) {
        File file = new File(filePath);
        File parent =new File(file.getParent()) ;
        parent.mkdirs();
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception ex) {
                LogUtil.info("写文件失败:创建文件" + filePath + "失败。信息为:" + ex);
                return false;
            }
            return true;
        } else {
            try {
                    setFileLength(filePath, 0);
            } catch (Exception ex) {
                return false;
            }
        }
        return true;
    }
    public static void deleteFile(String fileName)
    {
        File file = new File(fileName);
        file.deleteOnExit();
    }

你可能感兴趣的:(java读取文件)