当前位置:网站首页>按字节方式和字符方式读取文件_加载配置文件
按字节方式和字符方式读取文件_加载配置文件
2022-08-09 09:07:00 【胡乐天】
本文内容
IO流在我印象中一直是比较复杂的,类太多,这次整理一下,本文只是记录了I/O流中的I,只有读取文件,没有输出,通过四个静态方法,分别写了:按字节方式读取文件、按字符方式读取文件、字符加缓存读取文件(配置字符编码)、加载配置文件到Properties中(如果是在项目中加载配置文件,请参考我之前写的《jfinal加载配置文件的原理》)。

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); //字符方式读取
//charBufferRead(filePath); //字符(加缓存)方式读取,(此种方式若使用InputStreamReader可指定读取的编码)
loadProperties(filePath);//按字符方式读取配置文件,并加载到Properties中
}
/** * 加载配置文件 * 读取于前几个方法的读取一致,至于中文乱码,则在第三个方法(main中的方法顺序)中进行解决, * 故此处直接使用带编码读取方式 * @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");//此处我的配置文件是utf-8编码的,若为ANSI编码格式,请使用GBK
Properties prop = new Properties();
prop.load(reader);
//將Properties中的內容打印出來
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();
}
}
}
}
/** * 按字符方式读取,加缓存 * * @param filePath */
private static void charBufferRead(String filePath){
FileInputStream fileIn = null;
Reader reader = null;
BufferedReader bufferedReader = null;
try {
reader = new FileReader(filePath);//此方式最好读取utf-8的文本文件,读取ANSI(windows默认的编码格式)的文件会中文乱码
/* //FileReader是InputStream的子类,但是FileReader没有提供有读取编码的构造方法,详情请看源码 fileIn = new FileInputStream(filePath); reader = new InputStreamReader(fileIn,"GBK");//此种方式可以添加读取文件时的编码,默认UTF-8,若要读取ANSI需要使用编码GBK*/
bufferedReader = new BufferedReader(reader);
//存放读取的内容
StringBuilder strb = new StringBuilder();
//存储行内容
String row;
while((row = bufferedReader.readLine()) != null){
strb.append(row);
strb.append("\n");//此种方式不会读取换行符,需要自己加上
}
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();
}
}
}
}
/** * 按字符方式读取,读取文本文件,尽量采用这种方式 * 此方式最好读取utf-8的文本文件,读取ANSI(windows默认的编码格式)的文件会中文乱码 * @param filePath */
private static void charRead(String filePath) {
InputStreamReader reader = null;
try {
reader = new FileReader(filePath);
//存储字符
char[] chars = new char[1024];
//存储文件中的内容
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();
}
}
}
}
/** * 按字节读取文件(一般读取音频视频才使用这种方式,且不会直接打印),并打印文件内容 * 此方式最好读取utf-8的文本文件,读取ANSI(windows默认的编码格式)的文件会中文乱码,另外读取文本文件最好使用字符方式读取 * @param filePath */
private static void byteRead(String filePath){
InputStream in = null;
try {
in = new FileInputStream(filePath);
//bt数组存放每次读取的内容
byte [] bt = new byte[1024];
//存放本次读取的数量
int length;
//存放文件中的内容
StringBuilder strb = new StringBuilder();
//in.read(bt) 相当于 in.read(bt,0,bt.length) 返回值为读取到字节数组的长度
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 {
//凡是该类直接或间接实现过Closeable接口的,都需要关闭
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
边栏推荐
猜你喜欢
![[漏洞复现]CVE-2018-7490(路径遍历)](/img/0f/652869001b3e3b683192e6558d81fb.png)
[漏洞复现]CVE-2018-7490(路径遍历)

探索APP性能优化之稳定性优化(解决方案)

【场景化解决方案】构建医疗通讯录,“慧医钉”助力医院实现数字化管理

The difference between big-endian and little-endian storage is easy to understand at a glance

PoPW代币分配机制或将点燃下一个牛市

BUUCTF MISC刷题笔记(一)

ARMv8/ARMv9视频课程-Trustzone/TEE/安全视频课程

ctfshow-web入门 文件上传篇部分题解

Failed to mount component: template or render function not defined.

BUUCTF MISC brush notes (2)
随机推荐
SQL语言中的distinct说明
电子产品整机结构设计的一般性思路
[Vulnerability reproduction] CVE-2018-7490 (path traversal)
js在for循环中按照顺序响应请求
CPU主频 外频 芯片组 倍频 cache FSB PCI简介
The 5th Blue Cap Cup preliminary misc reappears after the game
PoPW token distribution mechanism may ignite the next bull market
探索APP性能优化之稳定性优化(解决方案)
shell 定时监控并处理脚本
【培训课程专用】CA/TA调用模型-代码导读
web3到底是什么?
define 可变参数定义
Max Flow P
PoPW代币分配机制或将点燃下一个牛市
智慧图书馆的导航方案-定位导航导览-只用一个方案全部实现
【KD】2022 KDD Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
elder blind date
Some of the topics in VNCTF2021 are reproduced
【GNN终身学习】2022 CVPR 终身图学习
parse <compoN> error: Custom Component‘name should be form of my-component, not myComponent or MyCom