JAVA学习随笔(6)--FileInputStream随笔

2024-04-20 09:58

本文主要是介绍JAVA学习随笔(6)--FileInputStream随笔,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.字节流
1)InputStream抽象了应用程序读取数据的方式
OutputStream抽象应用程序写出数据的方式
2)EOF = End 读到-1就读到结尾
3)输入流基本方法
int b = in.read();//读取一个字节无符号填到int低8位
in.read(byte[] buf);
in.read(byte[] buf,int start,int end);
4)输出流基本方法,和输入流类似
out.write(int b);
out.write(byte[] buff);
out.write(byte[] buf,int start,int size);

package file;import java.io.FileInputStream;
import java.io.IOException;public class IOUtil {               public static void printHex(String fileName) throws IOException{FileInputStream in = new FileInputStream(fileName);int b;int i = 1;while((b = in.read()) != -1){if(b <= 0xf){System.out.print("0" + Integer.toHexString(b) + " ");}else{System.out.print(Integer.toHexString(b) + " ");}if(i++ % 10 == 0){System.out.println();}}in.close();}}
package file;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;public class IOUtil {               public static void printHex(String fileName) throws IOException{FileInputStream in = new FileInputStream(fileName);int b;int i = 1;while((b = in.read()) != -1){if(b <= 0xf){System.out.print("0" + Integer.toHexString(b) + " ");}else{System.out.print(Integer.toHexString(b) + " ");}if(i++ % 10 == 0){System.out.println();}}in.close();}public static void printHexByByteArray(String fileName) throws IOException{FileInputStream in = new FileInputStream(fileName);//最大缓存设20KBbyte[] buf = new byte[20*1024];//返回的是读到的字节数int bytes = in.read(buf,0,buf.length);int flag= 1;for(int i=0;i<bytes;i++){if(buf[i] < 0xf){System.out.print("0" + Integer.toHexString(buf[i]) + " ");}else{System.out.print(Integer.toHexString(buf[i]) + " ");}if(flag++ % 10 == 0){System.out.println();}}in.close();}}

单字节读取不适合读比较大的文件,文件数据量上去后单字节读取方式时间消耗更大。批量读取是最常见的读取文件方式

package file;import java.io.IOException;public class IOUtilTest02 {public static void main(String[] args) throws IOException {// TODO Auto-generated method stublong start = System.currentTimeMillis();IOUtil.printHexByByteArray("e:\\javaio\\111.mp3");long batchEnd = System.currentTimeMillis();IOUtil.printHex("e:\\javaio\\111.mp3");long aloneEnd = System.currentTimeMillis();System.out.println("单独读取耗时:" + (aloneEnd - batchEnd));System.out.println("批量读取耗时:" + (batchEnd - start));}}

这里写图片描述

5)FileOutputStream–实现向文件中写出byte数据的方法

public class FileOutDemo1{public static void main(String[] args) throws IOException {//如果文件不存在,则直接创建,如果存在则删除后创建FileOutputStream out = new FileOutputStream("e://javaio//out.txt");out.write('A');//写出'A'的低8位int a = 10;out.write(a >>> 24);out.write(a >>> 16);out.write(a >>> 8);out.write(a);byte[] buf = "张小红".getBytes("gbk");out.write(buf);out.close();}
}

这篇关于JAVA学习随笔(6)--FileInputStream随笔的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/919945

相关文章

Spring Boot 2.7.8 集成 Thymeleaf的最佳实践与常见问题讨论

《SpringBoot2.7.8集成Thymeleaf的最佳实践与常见问题讨论》本文详细介绍了如何将SpringBoot2.7.8与Thymeleaf集成,从项目依赖到配置文件设置,再到控制器... 目录前言一、如何构建SpringBoot应用1、项目依赖 (pom.XML)2、控制器类3、Thymelea

SpringBoot项目jar依赖问题报错解析

《SpringBoot项目jar依赖问题报错解析》本文主要介绍了SpringBoot项目中常见的依赖错误类型、报错内容及解决方法,依赖冲突包括类找不到、方法找不到、类型转换异常等,本文给大家介绍的非常... 目录常见依赖错误类型及报错内容1. 依赖冲突类错误(1) ClassNotFoundExceptio

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi