您的位置 首页 > 数码极客

「java如何读取byte数组长度」java循环读取byte数组…

前言

其实有很多人在学习IO编程的时候有点好奇,为什么可以读取文件,读取到的数据为什么是byte而不是我们想要的String或者其他数据结构?然而在java中的基础数据类型就那么几种,而且String也不是基础数据类型,String是由byte数组构成。所以很多时候我们看到的io流都是以byte[]字节数组来进行读写。

FileInputStream(文件输入流):FileInputStream 从文件系统中的文件获取输入字节。可用的文件取决于主机环境。一般用于程序读取文件系统中的文件。

FileOutputStream(文件输出流):FileOutputStream 用于写入原始字节流。

这两个类是可以配对使用的。

Demo

public class FileStreamDemo { public static void main(String[] args) throws Exception { //1、读取一个文件 File file = new File("/Users/chenzeyao/Desktop/te;); //2、先判断文件是否存在 if ()) { //3、 将文件读入inputStream流 InputStream inputStream = new FileInputStream(file); //4、 得到文件中所有数据的字节流 byte [] bs = in(); //5、 创建一个写outFile文件 File outFile = new File("/Users/chenzeyao/Desktop/te;); // 6、创建输出流进行写操作 OutputStream outputStream = new FileOutputStream(outFile); //7、 用outputStream流将数据写入outFile文件中 ou(bs); } } }

效果

一开始test文件夹中只有一个aa.txt文件。程序运行后,效果如程序运行后图所示

程序运行前

程序运行后

从上面的效果图,有些人可能会很好奇,为啥for循环遍历打印出来的是72,101...一些整数呢?原因是byte会自转成int类型,而输出int的数值就是对应ascall码表中的十进制。感兴趣的同学可以这个网站找十进制对应的字符。而在bb.txt输出的内容就是如下也就是Hello FileInputStream。

FileInputStream常用方法

java.io.FileInputStream#FileInputStream)

public FileInputStream(File file) throws FileNotFoundException { String name = (file != null ? () : null); // java程序启动时是否开启了安全管理,一般来说,我们是默认关闭的 SecurityManager security = Sy(); if (security != null) { (name); } if (name == null) { throw new NullPointerException(); } if ()) { throw new FileNotFoundException("Invalid file path"); } // 文件描述符 fd = new FileDescriptor(); //fd和FileInputStream对象建立关联 (this); path = name; //调用jvm本地方法栈去打开文件 open(name); altFinalizer = getFinalizer(this); if (altFinalizer == null) { FileCleanable.register(fd); // open set the fd, register the cleanup } }private void open(String name) throws FileNotFoundException { open0(name); }// 打开文件去读取 private native void open0(String name) throws FileNotFoundException;//如果 FIS 需要终结器,则返回终结器对象;否则为空。 //如果 FIS 有关闭方法;它需要一个 AltFinalizer。 private static Object getFinalizer(FileInputStream fis) { Class<?> clazz = (); while (clazz != FileIn) { try { clazz.getDeclaredMethod("close"); return new AltFinalizer(fis); } catch (NoSuchMethodException nsme) { // ignore } clazz = clazz.getSuperclass(); } return null; }//完成时调用 FileIn 的类。 //如果需要对流进行最终确定,则在其构造函数中创建一个实例。 //当与流相关的实例集无法访问时,AltFinalizer 会执行所需的对流的 close 方法的调用。 static class AltFinalizer { private final FileInputStream fis; AltFinalizer(FileInputStream fis) { = fis; } @Override @SuppressWarnings("deprecation") protected final void finalize() { try { if != null) && != FileDe)) { /* if fd is shared, the references in FileDescriptor * will ensure that finalizer is only called when * safe to do so. All references using the fd have * become unreachable. We can call close() */ (); } } catch (IOException ioe) { // ignore } } }

2、available

//返回可以从此输入流中读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 //当文件位置超出 EOF 时返回 0。 //下一次调用可能是同一个线程或另一个线程。 //单次读取或跳过这么多字节不会阻塞,但可能会读取或跳过更少的字节。 //更直白的理解,可以理解为读取后剩下多少个字节, //如果读取完,则会返回0 public int available() throws IOException { return available0(); }

demo

//运行的结果和上面的结果是一样的 public class FileStreamDemo { public static void main(String[] args) throws Exception { //1、读取一个文件 File file = new File("/Users/chenzeyao/Desktop/te;); //2、先判断文件是否存在 if ()) { //3、 将文件读入inputStream流 InputStream inputStream = new FileInputStream(file); while ()>0) { Sy()+","); } } } }

3、read

//读一个字节 public int read() throws IOException { return read0(); } //调用jvm本地方法 private native int read0() throws IOException;

4、java.io.FileInputStream#read(byte[])

//一次读取b数组长度的字节数据; //这里说明一下,如果b数组不够长的话,读取的数据就读取不完整 public int read(byte b[]) throws IOException { return readBytes(b, 0, b.length); }

从下面debug出来的结果就很清楚,只会将读取到2个字节方法byte数组中

5、java.io.InputStream#skip

//直接跳过第n个字节,也就是说,第n个字节数据不做读取 public long skip(long n) throws IOException { return skip0(n); } private native long skip0(long n) throws IOException;

上面是FileInputStream类的一些常用方法

FileOutputStream常用方法

1、java.io.FileOutputStream#FileOutputStream)

public FileOutputStream(File file) throws FileNotFoundException { this(file, false); }//你会发现这里的实现步骤和FileInputStream是一样的 //1、检查是否有SecurityManager //2、创建FileDescriptor文件描述符 //3、建立关联 //4、打开文件 //5、最后处理器 public FileOutputStream(File file, boolean append) throws FileNotFoundException { String name = (file != null ? () : null); SecurityManager security = Sy(); if (security != null) { (name); } if (name == null) { throw new NullPointerException(); } if ()) { throw new FileNotFoundException("Invalid file path"); } = new FileDescriptor(); (this); = name; open(name, append); altFinalizer = getFinalizer(this); if (altFinalizer == null) { FileCleanable.register(fd); // open sets the fd, register the cleanup } }

2、核心方法write,但是最终也是调用jvm本地方法

总结

其实很多io实现类,都最终调用了jvm本地方法。源码阅读部分不复杂,只要是从使用方面去讲解。

喜欢的同学,可以点赞、关注+收藏,后期还会推出其他框架源码分析。

如果有需要补充的地方,留言区见。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“java如何读取byte数组长度,java循环读取byte数组,java读取文件byte数组,java,inputstream读取byte数组”边界阅读