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

来源:互联网 发布:吴京票房号召力知乎 编辑:程序博客网 时间:2024/05/02 21:25

首先介绍可能用到的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:05InputStream 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:11BufferedReader 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:44FileReader 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:56FileReader 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:45File 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:57FileInputStream 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();}}


阅读全文
0 0
原创粉丝点击