当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
猜你喜欢
Getting started with ctfshow-web Part of the file upload part solution
[漏洞复现]CVE-2018-7490(路径遍历)
【LeetCode每日一题】——225.用队列实现栈
支付宝小程序使用自定义组件(原生)
对于栈、递归的关系的理解
关于指针、地址的大小的问题(以及malloc的用法)
数理逻辑MOOC+知识点总结(未完无待续)
Where does detection go forward?
Some of the topics in VNCTF2021 are reproduced
QT program generates independent exe program (pit-avoiding version)
随机推荐
parse <compoN> error: Custom Component‘name should be form of my-component, not myComponent or MyCom
PID控制电机输出作为电机PWM占空比输入的理解
【Harmony OS】【ArkUI】ets开发 简易视频播放器
nodeMCU(ESP8266)和RC522的接线图
js在for循环中按照顺序响应请求
智慧图书馆的导航方案-定位导航导览-只用一个方案全部实现
【场景化解决方案】构建门店通讯录,“门店通”实现零售门店标准化运营
管理方向发展
支付宝小程序禁止页面弹性下拉或上拉
js实现看板全屏功能
NodeMCU(ESP8266) 接入阿里云物联网平台 踩坑之旅
1. LVGL 8.3 在 Visual Studio 2019 模拟器中的环境搭建
【场景化解决方案】OA审批与用友U9数据集成
【LeetCode每日一题】——225.用队列实现栈
epoll LT和ET 问题总结
vim 按了Ctrl+S后 卡死
这下你知道为什么程序员要和产品干架了吧?
MySQL查漏补缺(三) 计算字段
makefile 遗漏分割符 您的意思是用TAB代替8个空格?
QT设置exe可执行文件的图标