每次看javaIO的时候就很烦躁,一是因为太繁琐了,看到这么多类,又是字节流又是字符流的,二是我做javaEE开发的时候用的实在不多,过两天就忘了。
流又分为输入流和输出流:这个是以内存为参照的,如果是向内存存入的就是输入流,从内存流出就是输出流。
java的IO分为字符流和字节流,字节输入流每次都是从文件或者内存中的读取都是以字节为单位的,在abstract类InputStream中有一个abstract的read方法
/**
Reads the next byte of data from the input stream. The value byte is
* returned as an <code>int</code> in the range <code>0</code> to
* <code>255</code>. If no byte is available because the end of the stream
* has been reached, the value <code>-1</code> is returned
*/
public abstract int read() throws IOException;//这个方法的的用法是返回一个int类型的,虽然是int但是0~255那么只有一个字节的大小
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
//以上方法是将读取的字节存储到一个字节数组中
public int read(byte b[], int off, int len) throws IOException
在这个类中读取字节的主要方法是一个abstract的,我以为会在子类中得到源码,打开FileInputStream后发现是一个native方法
public native int read() throws IOException;
//那么java在底层调用了C++写的代码了
所以我们看不到他的具体实现是什么,不过可以知道的是在读取文件的时候还负责了游标的后移等的操作
在javaIO字节输入流中这个方法是核心,再来看一下DataInputStream,它是能够从文件或者内存中读取入int,char,short,long之类的数据的,字节输出流每次读取的都是字节,那么它是怎么读取返回数据的呢?
public final int readInt() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
//这上面是一个读取int类型的数据方法,仔细看会发现它实际上每次读取了四个字节,将第一个字节左移三个字节长度,
//第二个字节左移两个字节长度,第三个字节左移一个字节长度,然后将他们相加起来就得到了一个int
很相像的还有
public final char readChar() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (char)((ch1 << 8) + (ch2 << 0));
}
//在这里值得注意的是判断条件ch1 | ch2) < 0
//为什么这么判断呢?是因为读取的是字节那么范围在0~255,这样两者或运算应该是正数,而出现负数只有一种可能那就是读到文件结尾不够了,返回了-1
public final int readUnsignedShort() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (ch1 << 8) + (ch2 << 0);
}
我一直不清楚的是在字节输入输出流中的布尔类型的数据是这么存储的,看了源码发现是这样的
在DataOutPutStream类中
/**
* Writes a <code>boolean</code> to the underlying output stream as
* a 1-byte value. The value <code>true</code> is written out as the
* value <code>(byte)1</code>; the value <code>false</code> is
* written out as the value <code>(byte)0</code>. If no exception is
* thrown, the counter <code>written</code> is incremented by
* <code>1</code>.
*
* @param v a <code>boolean</code> value to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public final void writeBoolean(boolean v) throws IOException {
out.write(v ? 1 : 0);
incCount(1);
}
//就是说java将boolean存储成了一个byte大小的数据
在DataInputStream类中读取boolean是这样的
public final boolean readBoolean() throws IOException {
int ch = in.read();
if (ch < 0)
throw new EOFException();
return (ch != 0);
}
再来看看对象字节流,我们甚至可以使用字节的方式将对象写入文件中,当然该对象的类必须实现了Serializable接口,当我们需要再恢复这个对象的时候,系统不需要再调用构造器来执行初始化。当然了恢复出来的对象与原对象还是不同的对象,除了他们在堆中的地址之外具有其他一样的属性。
package com.gengu.input;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.gengu.assist.Book;
/**
* 这个类测试对象字节输入流
* */
public class TestObjectInput {
public static Object objectInput(String filename) throws IOException, ClassNotFoundException{
FileInputStream fin = new FileInputStream("F://新建文件夹 (2)/TestIO/src/com/gengu/input/test.txt");
ObjectInputStream oin = new ObjectInputStream(fin);
Object object = oin.readObject();
fin.close();
oin.close();
return object;
}
//向文件中写入对象
public static void objectOutput(String filename,Object obj) throws IOException{
FileOutputStream fout = new FileOutputStream(filename,false);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(obj);
oout.close();
fout.close();
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
Book book = new Book();
TestObjectInput.objectOutput("F://新建文件夹 (2)/TestIO/src/com/gengu/input/test.txt", book);
Book book1 = (Book) TestObjectInput.objectInput("F://新建文件夹 (2)/TestIO/src/com/gengu/input/test.txt");
System.out.println(book1.getClass()==book.getClass());
}
}
分享到:
相关推荐
Java IO学习笔记+代码,全面介绍IO中的方法、类,很适合初学者
java学习笔记1(java io/nio)设计模式
java IO流学习笔记
收集的java李兴华老师的课件笔记。感觉还不错,适合回顾和新手补习。
NULL 博文链接:https://lpf.iteye.com/blog/1471932
《Java JDK6学习笔记》是作者良葛格本人近几年来学习Java的心得笔记,结构按照作者的学习脉络依次展开,从什么是Java、如何配置Java开发环境、基本的Java语法到程序流程控制、管理类文件、异常处理、枚举类型、泛型...
这是我自己整理的Java学习笔记,希望对大家有帮助
io流学习笔记,主要适合入门的人士学习及观看
java学习笔记之Java_IO操作共19页.pdf.zip
java学习笔记大全:java内容介绍 java编程可以分成三个方向: 1、java se (j2se)桌面开发 java中的基础中的基础 2、java ee (j2ee)web开发 3、java me (j2me)手机开发 java se课程介绍 java面向对象编程(基础) java...
IO—黑马程序员Java学习笔记.rar
文档是关于JAVA常用IO流的学习笔记,可以用于参考,学习。
这是关于java各个IO流,从节点流到装饰缓冲流的一系列笔记纲要,帮助更好的理解和学习java的IO流
尚硅谷康师傅java学习笔记。 、2020-4-5 java学习笔记 2020-4-6 java笔记 ---内部类 2020-4-6 java笔记 ---异常 2020-4-6 java笔记 --多线程 2020-4-8 java笔记 String类 2020-4-9 java 比较器 2020-4-10 java笔记 ...
自己总结,知识点全面,包含了,IO流,异常机制等学习笔记 含有代码实例可供参考,需要mybase打开 持续更新,需要的自行下载 附上本人写的学习博客 https://blog.csdn.net/qq_35577787/article/details/105088073
详细介绍了IO的用法,基本实现原理,并介绍了序列化与反序列化的实现方法。谢谢
自己总结的java中IO流的笔记,绘制了详细的思维导图,每个思维导图中均有详细的博文解释,方便大家学习和理解,免费分享给大家。适合java的爱好者和学习者