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

}

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

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

(0)
上一篇 2021年7月18日 下午2:00
下一篇 2021年7月18日 下午3:00


相关推荐

  • 常见的动态路由协议(RIP、OSPF、ISIS、BGP)

    常见的动态路由协议(RIP、OSPF、ISIS、BGP)路由器要转发数据必须先配置路由数据 通常根据网络规模的大小可设置静态路由或设置动态路由 静态路由配置方便 对系统要求低 适用于拓扑结构简单并且稳定的小型网络 缺点是不能自动适应网络拓扑的变化 需要人工干预 动态路由协议有自己的路由算法 能够自动适应网络拓扑的变化 适用于具有一定数量三层设备的网络 缺点是配置对用户要求比较高 对系统的要求高于静态路由 并将占用一定的网络资源 nbsp nbsp nbsp nbsp nbsp nbsp

    2026年3月18日
    2
  • 在PHP代码中空格的写法,html空格代码怎么写?

    在PHP代码中空格的写法,html空格代码怎么写?html 空格代码怎么写 html 空格代码是为 html 空格字符代码 由 amp n b s p 组成 记住最后一个分号不要忘记了 CSS 中当 white space 属性取值为 pre 时 浏览器会保留文本中的空格和换行 例如 AABB 显示效果为 AABBCSS 的 letter spacing 属性用于设置文本中字符之间的间隔 例如 欢迎光临 显示效果为 欢迎光

    2025年7月30日
    8
  • OpenClaw装机火爆,专家提醒做好安全隔离

    OpenClaw装机火爆,专家提醒做好安全隔离

    2026年3月12日
    3
  • 【tensorflow】浅谈什么是张量tensor

    【tensorflow】浅谈什么是张量tensor也许你已经下载了TensorFlow,而且准备开始着手研究深度学习。但是你会疑惑:TensorFlow里面的Tensor,也就是“张量”,到底是个什么鬼?也许你查阅了维基百科,而且现在变得更加困惑。也许你在NASA教程中看到它,仍然不知道它在说些什么?问题在于大多数讲述张量的指南,都假设你已经掌握他们描述数学的所有术语。别担心!我像小孩子一样讨厌数学,所以如果我能明白,你也可以!…

    2022年6月25日
    32
  • IO 模型_netty reactor模型

    IO 模型_netty reactor模型//IOCP2.cpp:Definestheentrypointfortheconsoleapplication.//#include”stdafx.h”#include<WinSock2.h>#include<MSWSock.h>#include<Windows.h>#include&lt…

    2026年4月15日
    4
  • 损失函数——交叉熵损失函数(CrossEntropy Loss)

    损失函数——交叉熵损失函数(CrossEntropy Loss)损失函数 交叉熵损失函数 CrossEntropy 交叉熵函数为在处理分类问题中常用的一种损失函数 其具体公式为 1 交叉熵损失函数由来交叉熵是信息论中的一个重要概念 主要用于度量两个概率分布间的差异性 首先我们来了解几个概念 1 1 信息量信息论奠基人香农 Shannon 认为 信息是用来消除随机不确定性的东西 也就是说衡量信息量大小就看这个信息消除不确定性的程度 太阳从东方升起了 这条信息没有减少不确定性 因为太阳肯定从东面升起 这是句废话 信息量为 0 六

    2025年6月28日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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