Java 文件工具类 YsFile

package my;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by 云先生 on 2018/12/25.
 */

public class YsFile
{
    public String encoding = "utf-8";// 字符集 "gbk" "utf-8"

    private String filename;
    private File f;
    private BufferedWriter out = null;
    private ArrayList<String> readLines = null;
    private Map<String, String> strModel = null;

    public YsFile(String filename, String encoding)
    {

        this.filename = filename;
        this.encoding = encoding;
        f = new File(filename);
        if (!f.exists())
        {
            System.out.println("没有找到路径  " + filename + " 的文件.");
            System.out.println("  开始创建文件  " + filename);
            try
            {
                f.createNewFile();
            } catch (IOException e)
            {
                System.out.println("    文件创建失败,原因:  " + e.getMessage());
                return;
            }
            System.out.println("      文件: " + filename + " 创建成功");
        }
    }

    // 读取 不限大小
    public String read() throws Exception
    {
        System.out.println("*读取文件(无限制): " + filename + " ..");
        FileInputStream fstream = new FileInputStream(f);
        try
        {
            int fileSize = (int) f.length();
            byte[] buffer = new byte[fileSize];
            fstream.read(buffer);
            return new String(buffer, encoding);
        } finally
        {
            try
            {
                fstream.close();
            } catch (Exception e)
            {
            }
        }

    }

    // 读取 可以设定文件大小
    public String read_max_size(Double max_size_M) throws Exception
    {
        System.out.println("*读取文件(有文件大小限制): " + filename + " ..");
        FileInputStream fstream = new FileInputStream(f);
        try
        {
            int fileSize = (int) f.length();
            if (fileSize > 1024 * 1024 * max_size_M)
                throw new Exception("File too large to read! size=" + fileSize / (1024 * 1024) + "M");

            byte[] buffer = new byte[fileSize];
            fstream.read(buffer);
            return new String(buffer, encoding);
        } finally
        {
            try
            {
                fstream.close();
            } catch (Exception e)
            {
            }
        }

    }

    // 读取 按行读取 返回一个 list
    public ArrayList<String> readLines() throws Exception
    {
        System.out.println("*读取文件(按行读取): " + filename + " ..");
        // 使用ArrayList来存储每行读取到的字符串
        ArrayList<String> arrayList = new ArrayList<>();
        InputStreamReader inputReader = null;
        BufferedReader bf = null;
        try
        {
            inputReader = new InputStreamReader(new FileInputStream(f), encoding);
            bf = new BufferedReader(inputReader);
            // 按行读取字符串
            String str;
            while ((str = bf.readLine()) != null)
            {
                if (str.trim().equals(""))
                    continue; // 如果该行没有内容 跳过

                arrayList.add(str.trim());
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            bf.close();
            inputReader.close();
        }

        return arrayList;
    }

    // 写文件 不追加读写 每次都覆盖
    public void write_cover(String text) throws Exception
    {
        System.out.println("*写入文件: " + filename + " ..");
        FileOutputStream fstream = new FileOutputStream(f);
        try
        {
            fstream.write(text.getBytes(encoding));
        } finally
        {
            fstream.close();
        }
    }

    // 写文件 在末尾行追加读写 不覆盖
    public void write_add(String text) throws Exception
    {
        System.out.println("*写入文件: " + filename + " ..");
        OutputStreamWriter writerStream = new OutputStreamWriter(new FileOutputStream(f, true), "UTF-8");
        out = new BufferedWriter(writerStream);
        try
        {
            out.write(text + "\r\n");
        } finally
        {
            out.close();
        }
    }

    //
    public String getString(String key)
    {
        // 获取 key=Value 举例 ip=192.168.01.1
        // f.getString("ip") 返回 "192.168.01.1"

        try
        {
            if (readLines == null)
            {
                readLines = readLines();
            }

        } catch (Exception e)
        {
            System.out.println("获取失败,原因:" + e.getMessage());
            return null;
        }

        Iterator<String> it = readLines.iterator();

        if (strModel == null)
        {
            strModel = new HashMap<String, String>();
        }

        while (it.hasNext())
        {
            String text = (String) it.next();
            if (text.indexOf("=") > 0)
            {
                String[] str = text.split("=");
                strModel.put(str[0], str[1]);
            }
        }

        return strModel.get(key);
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值