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

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

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

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

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

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

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

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

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();
	}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小爷欣欣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值