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

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

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号