前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java按字节、字符、行、随机读取文件,并设置字符编码格式

java按字节、字符、行、随机读取文件,并设置字符编码格式

作者头像
全栈程序员站长
发布2022-06-30 13:08:53
1.3K0
发布2022-06-30 13:08:53
举报
文章被收录于专栏:全栈程序员必看

首先介绍可能用到的java类:

inputStream:是字节输入流的所有类的超类,是一个抽象类;返回0-225内的字节值,如果没有字节可以读取则返回-1;

FileInputStream:读取文件中的字节,转成字节流,字节流读取不存在编码问题

FileReader:读取文件中的字符,转成字符流,字符读取需要注意编码问题

BufferedInputStream:字节读取,减少磁盘开销,不带缓存没读取一个字节就要写入一个字节,而带缓存则放在缓冲区(内存)等到设置的缓冲区限度时再写入。

BufferedReader:字符读取,减少磁盘开销,可以使用readline()方法整行读取。

inputStreamReader:可以将读如stream转换成字符流方式,是reader和stream之间的桥梁,并可以设置字符编码

代码语言:javascript
复制
package com.liuxin.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javassist.expr.NewArray;

public class Read {

	public static void main(String[] args) throws Exception {
		String fileName="D://1.txt";//读取文件
		System.out.println("----------字节读取文件前1024个字节内容的方法-------------");
		readFileByBytes(fileName);//读取文件前1024个字节内容的方法
		System.out.println("----------字节读取文件中所有字节的方法-------------");
		readFileAllByBytes(fileName);//读取文件中所有字节的方法
		System.out.println("----------字节以每次读取512个字节,循环读取文件内容-------------");
		readFileRoundBy512(fileName);//以每次读取512个字节,循环读取文件内容
		System.out.println("----------字节创建缓冲流读取读取文件内容-------------");
		readFileBufferByte(fileName);
		System.out.println("----------读取文件前1024个字符内容的方法-------------");
		readFileByChar(fileName);
		System.out.println("----------字符读取文件中所有内容的方法-------------");
		readFileAllByChar(fileName);
		System.out.println("----------字符创建缓冲流整行读取文件内容-------------");
		readFileBufferChar(fileName);
		System.out.println("----------字符创建缓冲流整行读取文件内容,并设置字符编码-------------");
		readFileSetEncode(fileName);
		
	}


	private static void readFileSetEncode(String fileName)throws Exception {
		//2017年9月30日 下午12:46:05
		InputStream is=new FileInputStream(fileName);
		InputStreamReader isr=new InputStreamReader(is,"gbk");
		BufferedReader br=new BufferedReader(isr);
		String tempLine=null;
		while((tempLine=br.readLine())!=null){
			System.out.println(tempLine);
		}
		br.close();
		isr.close();
		is.close();
	}


	private static void readFileBufferChar(String fileName)throws Exception {
		//2017年9月30日 上午11:55:11
		BufferedReader br=new BufferedReader(new FileReader(fileName));
		String tempLine=null;
		while((tempLine=br.readLine())!=null){
			System.out.println(tempLine);
		}
		br.close();
	}


	private static void readFileAllByChar(String fileName) throws Exception{
		//2017年9月30日 上午11:41:44
		FileReader fr=new FileReader(fileName);
		int tempChar=-1;
		while((tempChar=fr.read())!=-1){//循环读取,每次循环读取一个字,每个汉字都有对应的char数字对应,因此需要将汉字对应的数字强转成char。
			System.out.print((char)tempChar);
		}
		fr.close();
	}


	private static void readFileByChar(String fileName)throws Exception {
		//2017年9月30日 上午11:29:56
		FileReader fr=new FileReader(fileName);
		char[] buf=new char[1024];
		int tempChar=fr.read(buf);
		if(tempChar!=-1){
			System.out.println(new String(buf,0,tempChar));
		}
		fr.close();
	}


	private static void readFileBufferByte(String fileName) throws Exception{
		//2017年9月30日 上午10:49:45
		File file=new File(fileName);
		BufferedInputStream bis=null;//buffered是创建缓冲区,减少磁盘开销,不带缓存没读取一个字节就要写入一个字节,而带缓存则放在缓冲区(内存)等到设置的缓冲区限度时再写入。
		bis=new BufferedInputStream(new FileInputStream(file),512);
		byte[] buf=new byte[bis.available()];
		int tempByte=-1;
		while((tempByte=bis.read(buf))!=-1){
			System.out.println(new String(buf,0,tempByte));
		}
		bis.close();
	}


	private static void readFileRoundBy512(String fileName) throws Exception {
		//2017年9月30日 上午10:10:57
		FileInputStream fis =new FileInputStream(fileName);
		byte[] buf=new byte[512];
		int tempByte=-1;
		while((tempByte=fis.read(buf))!=-1){
			System.out.print(new String(buf,0,tempByte));  //不能使用println,否则会出现错行的现象
		}
		fis.close();
	}


	private static void readFileAllByBytes(String fileName) throws Exception {
		FileInputStream fis=new FileInputStream(fileName);
		byte[] buf =new byte[fis.available()];//fis.available()方法是读取文件中的所有内容的字节长度
		int tempByte=fis.read(buf);
		if(tempByte != -1){
			System.out.println(new String(buf,0,tempByte));
		}
		fis.available();
	}

	private static void readFileByBytes(String fileName) throws Exception {
		FileInputStream fis=new FileInputStream(fileName);
	 	byte[] buf=new byte[1024];
	 	int tempByte=fis.read(buf);
	 	if(tempByte !=-1 ){
	 		System.out.println(new String(buf,0,tempByte));
	 	}
	 	fis.close();
	}

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106299.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档