Java 操作文本 封装类

来源:互联网 发布:java多线程同步方法 编辑:程序博客网 时间:2024/05/17 04:31

package com.sidi.card.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用于对文本的操作
 *
 * @author 朱志杰
 *
 */
public class NoteOperate {
    // txt文件路径
    private String filePath;

    /**
     * 构造函数
     *
     * @param filePath
     *            文本文件全路径
     */
    public NoteOperate(String filePath) {
        this.filePath = filePath;
    }

    /**
     * 构造函数
     *
     * @param file
     *            需要读取的文本文件
     */
    public NoteOperate(File file) {
        this.filePath = file.getPath();
    }

    /**
     * 判断文本文件是否存在
     *
     * @return 存在返回true 否则返回false
     */
    public boolean exists() {
        File file = new File(this.filePath);
        return file.exists();
    }

    /**
     * 得到这个txt所有的列的数据 空行将自动跳过,并自动删除文字前后的空格
     *
     * @return List<String>
     * @throws IOException
     */
    public List<String> fileLinesContent() throws IOException {
        List<String> strs = new ArrayList<String>();
        File file = new File(this.filePath);
        FileReader fr = new FileReader(file);// 建立FileReader对象,并实例化为fr
        BufferedReader br = new BufferedReader(fr);// 建立BufferedReader对象,并实例化为br
        String Line = br.readLine();// 从文件读取一行字符串
        // 判断读取到的字符串是否不为空
        while (Line != null) {
            if (!"".equals(Line))
                strs.add(Line.trim());
            Line = br.readLine();// 从文件中继续读取一行数据
        }
        br.close();// 关闭BufferedReader对象
        fr.close();// 关闭文件
        return strs;
    }

    /**
     * 创建一个空的记事本文档 如果這個記事本文档存在就不再创建 函数还未写实现部分<br/> 如果文本已经存在则不再创建
     *
     * @throws IOException
     */
    public void createEmptyNote() throws IOException {
        File file = new File(this.filePath);
        if (!file.exists())
            file.createNewFile();
    }

    /**
     * 将内容写入这个文本 注意以前的内容将会被删除
     *
     * @param str
     *            将要写入的内容
     * @throws IOException
     */
    public void writeString(String str) throws IOException {
        File file = new File(this.filePath);
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        output.write(str);
        output.close();// 关闭BufferedReader对象
    }

    /**
     * 在文本的指定行插入文字。如果该行已经存在,该行内容将会被删除。如果行号不存在,将会被插入到最后一行
     *
     * @param i
     *            行号 行号为0时,将插入到最后一行
     * @param str
     *            将要插入的内容
     * @throws IOException
     */
    public void insertWords(int i, String str) throws IOException {
        List<String> strs = fileLinesContent();
        // 进行插入操作
        if (i == 0 || strs.size() < i) // 插入到最后一行
        {
            strs.add(str);
        } else { // 插入到文本中
            strs.set(i - 1, str);
        }
        // 重新写入到文本
        StringBuffer sb = new StringBuffer();
        for (String temp : strs) {
            sb.append(temp.replace("/r/n", "")+"/r/n");
        }
        writeString(sb.toString());
    }
    /**
     * 得到具体某一行的数据,行数小于0或者行数大于总行数返回null,否则返回该行的内容
     * @param lineNumber 基于0的行数
     * @return String
     * @throws IOException
     */
    public String getLineContent(int lineNumber) throws IOException {
        List<String> strs=new ArrayList<String>();
        strs=fileLinesContent();
        //越界或者行数设置不正确
        if(lineNumber<0 || lineNumber > strs.size())
            return null;
        return strs.get(lineNumber);
    }
}