当前位置:网站首页>按字节方式和字符方式读取文件_加载配置文件
按字节方式和字符方式读取文件_加载配置文件
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();
}
}
}
}
}
边栏推荐
- SQL语言中的distinct说明
- QT程序生成独立exe程序(避坑版)
- parse <compoN> error: Custom Component‘name should be form of my-component, not myComponent or MyCom
- 【GNN】2022 G-Mixup: Graph Data Augmentation for Graph Classification
- Calendar类和Date类转换时区 && 部分时区城市列表
- gin清晰简化版curd接口例子
- Amplify Shader Editor手册 Unity ASE(中文版)
- 【GNN终身学习】2022 CVPR 终身图学习
- ctf misc 图片题知识点
- 一篇文章带你熟悉 TCP/IP 协议(网络协议篇二)
猜你喜欢
随机推荐
XCTF高校战“疫”网络安全分享赛Misc wp
Failed to mount component: template or render function not defined.
小程序调用百度api实现图像识别
When and How to use MALLOC
[V&N2020 Open] Memory Forensics
RDMA
C#获取网卡地址
leetcode 36. 有效的数独(模拟题)
图像识别后将识别结果整理成列表,点击列表可跳转到搜索页面
canal工作原理及简单案例演示
【场景化解决方案】OA审批与用友U9数据集成
makefile的foreach、filter、filter-out函数
gin清晰简化版curd接口例子
BUUCTF MISC brush notes (2)
PoPW token distribution mechanism may ignite the next bull market
绝了,这套RESTful API接口设计总结
数据库期末复习这一篇就够了(期末预习大概也行)
+ 6000 words, help you understand the Internet architecture evolution.
算术表达式求值演示
二叉树的遍历(非递归)