Java读取文件常用工具类

Java读取文件常用工具类

package org.pro.file.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * explain: ReadFile Class
 * translate: 读取文件类
 * @author Muci 伤寒
 * Copyright (C), ORG伤寒
 * qq:1877378299
 * mail:[email protected]
 * tel:18616220047
 */
public class ReadFile {

    private BufferedReader bufferedReader;

    /**
     * Read File Object
     */
    public ReadFile() {
    }

    /**
     * Read File this file path
     * @param filePath
     * @throws FileNotFoundException
     */
    public ReadFile(String filePath) throws FileNotFoundException{
        this.bufferedReader = readFile(filePath);
    }
    /**
     * Read File
     * @param file
     * @throws FileNotFoundException
     */
    public ReadFile(File file) throws FileNotFoundException{
        this.bufferedReader = readFile(file);
    }

    /**
     * explain: ReadFile Function
     * translate: 读取文件的方法
     * @param filePath 文件地址
     * @return BufferedReader 缓冲读取对象
     * @throws FileNotFoundException 文件找不到
     */
    public BufferedReader readFile(String filePath) throws FileNotFoundException{
        //Read the file path to the file reader
        FileReader fileReader = new FileReader(filePath);
        //File reader into the buffer
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        this.bufferedReader = bufferedReader;
        return bufferedReader;
    }


    /**
     * explain: ReadFile Function
     * translate: 读取文件的方法
     * @param file 文件
     * @return BufferedReader 缓冲读取对象
     * @throws FileNotFoundException 文件找不到
     */
    public BufferedReader readFile(File file) throws FileNotFoundException{
        //Read the file path to the file reader
        FileReader fileReader = new FileReader(file);
        //File reader into the buffer
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        this.bufferedReader = bufferedReader;
        return bufferedReader;
    }
    /**
     * explain: To read the file content into a string
     * translate: 将文件内容读取成字符串
     * @return BufferedReader 缓冲读取对象
     */
    public synchronized String toString(){
        //create string buffer
        StringBuffer stringBuffer = new StringBuffer();
        //is read file content line
        String line;
        try {
            if(bufferedReader!=null){
                //cycle read this file to line add string buffer
                while((line=this.bufferedReader.readLine())!=null){
                    stringBuffer.append(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString().length()>0?stringBuffer.toString():null;
    }

    /**
     * close buffered reader
     */
    public synchronized void close(){
        try {
            if(bufferedReader!=null){
                this.bufferedReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

此工具类为读取普通文件类
下面则是该类的扩展类,读取ini文件,在开发语言中,ini配置文件其实是比较常用的,因为ini文件能直接被windows所识别,所以在通常很多C/S架构的软件中,都会有对应的ini文件。同时,ini文件对于C#、C++一类型的开发语言是很好识别的。
所以,在JNI开发环境下经常要读的不只是java的配置文件,还有对应的ini,xml等文件。
下列工具类对文件读取类进行扩展,用于读取ini配置文件

package org.pro.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import org.pro.file.util.ReadFile;

/**
 * explain: Read ini file class
 * translate: 读取ini文件类
 * @author Muci 伤寒
 * Copyright (C), ORG伤寒
 * qq:1877378299
 * mail:[email protected]
 * tel:18616220047
 */
public class ReadIni extends ReadFile{
    //buffered reader
    private BufferedReader bufferedReader;
    //content buffer map
    private HashMap> hashMap;

    /**
     * Read ini Object
     */
    public ReadIni() {
    }

    /**
     * Read ini file path to content buffer map
     * @param filePath
     * @throws FileNotFoundException
     */
    public ReadIni(String filePath) throws FileNotFoundException{
        this.bufferedReader = readFile(filePath);
        try {
            contentToBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * Read ini file to content buffer map
     * @param filePath
     * @throws FileNotFoundException
     */
    public ReadIni(File file) throws FileNotFoundException{
        this.bufferedReader = readFile(file);
        try {
            contentToBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * explain: ReadFile Function
     * translate: 读取文件的方法
     * @param filePath 文件地址
     * @return BufferedReader 缓冲读取对象
     * @throws FileNotFoundException 文件找不到
     */
    public BufferedReader readFile(String filePath) throws FileNotFoundException{
        //Read the file path to the file reader
        FileReader fileReader = new FileReader(filePath);
        //File reader into the buffer
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        this.bufferedReader = bufferedReader;
        try {
            contentToBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bufferedReader;
    }


    /**
     * explain: ReadFile Function
     * translate: 读取文件的方法
     * @param file 文件
     * @return BufferedReader 缓冲读取对象
     * @throws FileNotFoundException 文件找不到
     */
    public BufferedReader readFile(File file) throws FileNotFoundException{
        //Read the file path to the file reader
        FileReader fileReader = new FileReader(file);
        //File reader into the buffer
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        this.bufferedReader = bufferedReader;
        try {
            contentToBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bufferedReader;
    }

    /**
     * explain: content read to the buffer map function
     * translate: 读取内容到缓冲区
     * @throws IOException
     */
    private void contentToBuffer() throws IOException{
        //init content map buffer
        hashMap = new HashMap>();
        //init key and value temporary buffer
        HashMap keyValue = new HashMap();
        //is read file content line
        String line;
        //is line buffer temporary save
        String lineBuffer = null;
        //this area title
        String title = null;
        //is read file content line
        while((line = bufferedReader.readLine())!=null){
            line = line.trim();
            //this line is title
            if(line.matches("\\[.*\\]")){
                //this is title not null but key value is have value
                if(null!=title && !title.isEmpty()){
                    //content map buffer add title and key value
                    hashMap.put(title, keyValue);
                    //init key value temporary buffer
                    keyValue = new HashMap();
                    //init line buffer temporary
                    lineBuffer = null;
                }
                //get title temporary
                title = line.substring(line.indexOf("[")+1,line.indexOf("]"));
            }else if(line.matches(".*=.*")){
                //This line is the content of the title if not the title just drop it
                if(null!=title && !title.isEmpty()){
                    //cutting line
                    int i = line.indexOf("=");
                    lineBuffer = line;
                    //get the line key
                    String key = line.substring(0,i);
                    //get the line value
                    String value = line.substring(i+1);
                    //add this key value to keyValue buffer
                    keyValue.put(key, value);
                }
            }
        }
        //if this title is not null and this line buffer is not null just this line have value
        if(null!=title && !title.isEmpty() && null!=lineBuffer && !lineBuffer.isEmpty()){
            //get this line buffer the value
            int i = lineBuffer.indexOf("=");
            //get the line buffer key
            String key = lineBuffer.substring(0,i);
            //get the line buffer value
            String value = lineBuffer.substring(i+1);
            //add this buffer value to keyValue buffer
            keyValue.put(key, value);
            //add this title and keyValue buffer to content map buffer
            hashMap.put(title, keyValue);
        }
    }

    public HashMap getTitle(String title) throws IOException{
        return hashMap!=null?this.hashMap.get(title):null;
    }

    public void close(){
        if (bufferedReader!=null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

相对于这扩展工具类,其实仅仅只是调用了其读文件的功能方法进行。
对于该读取ini配置文件类,ini配置文件是有多种格式的,该读取类读取的文件格式是

[区域名1]
区域对应变量名1=值
区域对应变量名2=值
区域对应变量名3=值
区域对应变量名N=值
[区域名2]
区域对应变量名1=值
区域对应变量名2=值
区域对应变量名3=值
区域对应变量名N=值
[区域名N]
区域对应变量名1=值
区域对应变量名2=值
区域对应变量名3=值
区域对应变量名N=值

本文章其实算不上是文章,仅仅是对于这工具类的一个方案推广。对于文件读取,伤寒在此表示一直在进行对应研发及修改更新,以及对密文的读取等进行一系列研发。
在此也告知一些初学者,其实读写文件并不可怕,可怕的是在编码过程中如同一个无头苍蝇一般不知道从何下手。

本文由:OP-伤寒编写,如转发及复制请注明摘要地址http://blog.csdn.net/kevin_muqi
或 http://www.yoodb.com

如有疑问可加Q群248148860询问伤寒

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