当前位置:网站首页>Read file by byte and character_load configuration file

Read file by byte and character_load configuration file

2022-08-09 09:12:00 Hu Letian

本文内容

IOStreams have always been complicated in my mind,类太多,这次整理一下,This article is just for the recordI/O流中的I,Only read files,没有输出,Via four static methods,分别写了:Read the file byte by byte、Read character by character文件、Character plus cache to read files(配置字符编码)、加载配置文件到Properties中(If the configuration file is loaded in the project,Please refer to what I wrote earlier《jfinalThe principle of loading configuration files》).

在这里插入图片描述

package com.io.file;

import java.io.*;
import java.util.Properties;

public class ReadFile {
    
    public static void main(String[] args) {
    
        String filePath = "G:/test.properties";
        //byteRead(filePath); //字节方式读取
        //charRead(filePath); //read character
        //charBufferRead(filePath); //字符(加缓存)方式读取,(If this method is usedInputStreamReaderThe encoding to be read can be specified)
        loadProperties(filePath);//Read configuration files character-by-character,并加载到Properties中
    }

    /** * 加载配置文件 * The reading is consistent with the reading of the previous methods,As for Chinese garbled characters,Then in the third method(mainmethod order in )中进行解决, * Therefore, the coded reading method is directly used here * @param filePath */
    private static void loadProperties(String filePath) {
    
        InputStream in = null;
        Reader reader = null;
        try {
    
            in = new FileInputStream(filePath);
            reader = new InputStreamReader(in,"utf-8");//Here my config file isutf-8编码的,若为ANSI编码格式,请使用GBK
            Properties prop = new Properties();
            prop.load(reader);

            //將Propertiesprint out the contents
            for(Object key : prop.keySet()){
    
                System.out.println("key:" + key + ";value:" + prop.get(key));
            }

        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }finally {
    
            if (reader != null){
    
                try {
    
                    reader.close();
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
            
            if(in != null){
    
                try {
    
                    in.close();
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
        }
    }

    /** * Read character by character,加缓存 * * @param filePath */
    private static void charBufferRead(String filePath){
    
        FileInputStream fileIn = null;
        Reader reader = null;
        BufferedReader bufferedReader = null;

        try {
    
            reader = new FileReader(filePath);//It is best to read this wayutf-8的文本文件,读取ANSI(windows默认的编码格式)The file will be garbled in Chinese
/* //FileReader是InputStream的子类,但是FileReaderThere is no constructor provided to read the encoding,详情请看源码 fileIn = new FileInputStream(filePath); reader = new InputStreamReader(fileIn,"GBK");//In this way, the encoding when reading the file can be added,默认UTF-8,若要读取ANSIEncoding is requiredGBK*/
            bufferedReader = new BufferedReader(reader);
            //存放读取的内容
            StringBuilder strb = new StringBuilder();
            //Store row content
            String row;
            while((row = bufferedReader.readLine()) != null){
    
                strb.append(row);
                strb.append("\n");//Newlines are not read this way,需要自己加上
            }
            System.out.println(strb.toString());
        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }finally {
    

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

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

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

        }
    }

    /** * Read character by character,读取文本文件,Try to do it this way * It is best to read this wayutf-8的文本文件,读取ANSI(windows默认的编码格式)The file will be garbled in Chinese * @param filePath */
    private static void charRead(String filePath) {
    
        InputStreamReader reader = null;
        try {
    
            reader = new FileReader(filePath);
            //存储字符
            char[] chars = new char[1024];
            //Store the contents of the file
            StringBuilder strb = new StringBuilder();
            int count;
            while ((count = reader.read(chars)) != -1){
    
                strb.append(String.valueOf(chars,0,count));
            }
            System.out.println(strb.toString());
        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }finally {
    
            if(reader != null){
    
                try {
    
                    reader.close();
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
        }
    }

    /** * 按字节读取文件(This method is generally used to read audio and video,and will not print directly),并打印文件内容 * It is best to read this wayutf-8的文本文件,读取ANSI(windows默认的编码格式)The file will be garbled in Chinese,In addition, it is best to use character mode to read text files * @param filePath */
    private static void byteRead(String filePath){
    
        InputStream in = null;
        try {
    
            in = new FileInputStream(filePath);
            //btThe array holds the content of each read
            byte [] bt = new byte[1024];
            //Stores the number of reads this time
            int length;
            //Store the contents of the file
            StringBuilder strb = new StringBuilder();
            //in.read(bt) 相当于 in.read(bt,0,bt.length) The return value is the length read into the byte array
            while ((length = in.read(bt)) != -1){
    
                strb.append(new String(bt,0,length));
            }
            System.out.println(strb.toString());
        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }finally {
    //Any such class has been implemented directly or indirectlyCloseable接口的,都需要关闭
            if(in != null){
    
                try {
    
                    in.close();
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
        }

    }
}

原网站

版权声明
本文为[Hu Letian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090907014092.html