Java文件(txt)工具类

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


/**
 * 文件工具类
 */
public class FileUtil {

    /**
     * 读取文件内容
     * @param fileName 文件地址
     * @param valueType 读出的类型
     * @return
     */
    public static List<Object> readFile(String fileName, Class valueType){
        //如果不存在就创建一个
        createFileIfNotExists(fileName);
        List<Object> list=new ArrayList();
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            //一行一行读取
            while ((line = reader.readLine()) != null && reader.readLine().length()>0) {
                ObjectMapper objectMapper = new ObjectMapper();
                Object object = objectMapper.readValue(line, valueType);
                list.add(object);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 追加文件内容
     * @param fileName
     * @param data
     */
    public static void writeFile(String fileName,String data) {
        File file=new File(fileName);
        //如果不存在就创建一个
        createFileIfNotExists(fileName);
        try (FileWriter writer = new FileWriter(fileName, true)) {
            writer.write(data);
            writer.write("\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 如果文件以及相关文件夹不存在就创建他
     * @param filePath
     */
    public static void createFileIfNotExists(String filePath){
        // 创建一个 File 对象
        File file = new File(filePath);
        // 检查文件是否存在
        if (!file.exists()) {
            // 文件不存在,检查父文件夹是否存在,如果不存在则创建
            File parentFolder = file.getParentFile();
            if (parentFolder != null && !parentFolder.exists()) {
                boolean flag = parentFolder.mkdirs();
                if (flag) {
                    System.out.println("文件夹已创建: " + parentFolder);
                }
            }
            // 创建文件
            try {
                boolean flag = file.createNewFile();
                if (flag) {
                    System.out.println("文件已创建: " + file);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值