Java -- IO -- 目录

    操作文件的类 -- -- File

      File类的基本介绍

      使用File类操作文件

      范例 -- -- 列出指定目录的全部内容

    RandomAccessFile类

      使用RandomAccessFile类写入数据

      使用RandomAccessFile类读取数据

    字节流与字符流的基本操作

      -- -- -- -- -- -- -- -- 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,程序需要保存数据时使用输出流。

      -- -- -- -- -- -- -- -- 在java.io包中流的操作主要有字节流、字符流两大类。

      -- -- -- -- -- -- -- -- -- -- -- 在字节流中输出数据主要使用OutputStream类完成,输入使用的是InputStream类。

      -- -- -- -- -- -- -- -- -- -- -- 在字符流中输出数据主要使用Writer类完成,输入主要是使用Reader类完成。

      字节流

      -- -- -- 字节流主要操作byte类型数据,以byte数组为准。

      -- -- -- -- -- Class : OutputStream

public abstract class OutputStream implements Closeable, Flushable

      -- -- -- -- -- -- Class : FileOutputStream

public class FileOutputStream extends OutputStream

      -- -- -- -- -- -- -- -- -- -- -- 

        ⊙ public void close() throws IOException : 关闭此文件输出流并释放与此流有关的所有系统资源。

        ⊙ public void flush() throws IOException : 刷新此输出流并强制写出所有缓冲的输出字节。

        ⊙ public void write(int b) throws IOException : 将指定的字节写入此输出流。

        ⊙ public void write(byte b[]) throws IOException :  将 b.length 个字节从指定的 byte 数组写入此输出流。

        ⊙ public void write(byte b[], int off, int len) throws IOException : 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。

      -- -- -- -- -- Class : InputStream

public abstract class InputStream implements Closeable

      -- -- -- -- -- -- Class : FileInputStream

public class FileInputStream extends InputStream

      -- -- -- -- -- -- -- -- -- -- -- 

        ⊙ public native int available() throws IOException : 返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。

        ⊙ public void close() throws IOException : 关闭此输入流并释放与该流关联的所有系统资源。

        ⊙ public int read() throws IOException :  从输入流中读取数据的下一个字节。

        ⊙ public int read(byte b[]) throws IOException : 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。

        ⊙ public int read(byte b[], int off, int len) throws IOException :  将输入流中最多 len 个数据字节读入 byte 数组。

      字符流

      -- -- -- 在程序中一个字符等于两个字节。Reader和Writer操作字符流。

      -- -- -- -- -- Class : Writer

public abstract class Writer implements Appendable, Closeable, Flushable

      -- -- -- -- -- -- Class : OutputStreamWriter

public class OutputStreamWriter extends Writer

      -- -- -- -- -- -- -- Class : FileWriter

public class FileWriter extends OutputStreamWriter

      -- -- -- -- -- -- -- -- -- -- -- 

        ⊙ abstract public void close() throws IOException : 关闭此流,但要先刷新它。

        ⊙ public void write(int c) throws IOException : 写入单个字符。

        ⊙ public void write(char cbuf[]) throws IOException : 写入字符数组。

        ⊙ abstract public void write(char cbuf[], int off, int len) throws IOException : 写入字符数组的某一部分。

        ⊙ public void write(String str) throws IOException : 写入字符串。

        ⊙ public void write(String str, int off, int len) throws IOException : 写入字符串的某一部分。

        ⊙ abstract public void flush() throws IOException : 刷新该流的缓冲。

      -- -- -- -- -- Class : Reader

public abstract class Reader implements Readable, Closeable

      -- -- -- -- -- -- -- Class : InputStreamReader

public class InputStreamReader extends Reader

      -- -- -- -- -- -- -- -- -- Class : FileReader

public class FileReader extends InputStreamReader

      -- -- -- -- -- -- -- -- -- -- -- 

      ⊙ abstract public void close() throws IOException :  关闭该流并释放与之关联的所有资源。

      ⊙ public int read() throws IOException : 读取单个字符。

      ⊙ public int read(char cbuf[]) throws IOException : 将字符读入数组。

      ⊙ abstract public int read(char cbuf[], int off, int len) throws IOException : 将字符读入数组的某一部分。

      字节流与字符流的区别

      -- -- -- -- -- -- -- -- 字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的。

      -- -- -- -- -- -- -- -- 字符流在操作时使用了缓冲区,通过缓冲区在操作文件。

      范例 -- -- 文件复制

    转换流 -- -- OutputStreamWriter类与InputStreamReader类

      -- -- -- OutputStreamWriter : 是Writer的子类,将输出的字符流变为字节流。

public OutputStreamWriter(OutputStream out)

      -- -- -- InputStreamReader :  是Reader的子类,将输入的字节流变为字符流。

public InputStreamReader(InputStream in)

      -- -- 不论是字节流还是字符流,实际上最终都是以字节的形式操作输入/输出流的。

    内存操作流

      -- -- -- ByteArrayOutputStream 主要是将内存中的数据输出。

      -- -- -- ByteArrayInputStream 主要完成将内容写入到内存中。

      -- -- -- -- -- -- Class : ByteArrayInputStream

public class ByteArrayInputStream extends InputStream

      -- -- -- -- -- -- -- -- Construction : 

public ByteArrayInputStream(byte buf[])

        ⊙ public void close() throws IOException : 关闭 ByteArrayInputStream 无效。

        ⊙ public synchronized int read() : 从此输入流中读取下一个数据字节。

        ⊙ public synchronized int read(byte b[], int off, int len) : 将最多 len 个数据字节从此输入流读入 byte 数组。

      -- -- -- -- -- -- Class : ByteArrayOutputStream

public class ByteArrayOutputStream extends OutputStream

      -- -- -- -- -- -- -- -- Construction : 

public ByteArrayOutputStream()

        ⊙ public synchronized void write(int b) : 将指定的字节写入此 byte 数组输出流。

        ⊙ public synchronized void write(byte b[], int off, int len) : 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte 数组输出流。

        ⊙ public synchronized String toString() : 使用平台默认的字符集,通过解码字节将缓冲区内容转换为字符串。

        ⊙ public void close() throws IOException : 关闭 ByteArrayOutputStream 无效。

      -- -- 内存操作流一般在生成一些临时信息时才会使用。

    管道流

      管道流的主要作用是可以进行两个线程间的通信。分为管道输出流(PipedOutputStream)和管道输入流(PipedInputStream)。

      如果要进行管道输出,则必须把输出流连在输入流上。

      -- -- -- Class : PipedOutputStream

public class PipedOutputStream extends OutputStream 

      -- -- -- -- -- -- Function : connect(PipedInputStream)

public synchronized void connect(PipedInputStream snk) throws IOException

      -- Class : Send

package object.pipedStream;

import java.io.IOException;
import java.io.PipedOutputStream;

//实现Runnable接口
public class Send implements Runnable {

    // 管道输出流
    private PipedOutputStream pos = null;

    public Send() {
        // 实例化输出流
        this.pos = new PipedOutputStream();
    }

    @Override
    public void run() {
        String context = "为何在这里 疲倦象半醉
曾在这空间 跟你相拥抱
只有唏嘘的追忆
无言落寞地落泪
谁在这晚里放纵与她温馨
无尽空虚
似把刀锋静静穿过心窝";
        try {
            // 输出信息
            this.pos.write(context.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            // 关闭输出流
            this.pos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 通过线程类得到输出流
    public PipedOutputStream getPos() {
        return this.pos;
    }

}

      -- Class : Receive

package object.pipedStream;

import java.io.IOException;
import java.io.PipedInputStream;

//实现Runnable接口
public class Receive implements Runnable {

    private PipedInputStream pis = null;

    public Receive() {
        // 实例化输入流
        this.pis = new PipedInputStream();
    }

    @Override
    public void run() {
        byte b[] = new byte[1024];
        int len = 0;
        try {
            // 接收数据
            len = this.pis.read(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            this.pis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("接收到的内容为:
" + new String(b));
    }

    public PipedInputStream getPis() {
        return pis;
    }

}

      -- Class : PipedStreamDemo

package object.pipedStream;

import java.io.IOException;

public class PipedStreamDemo {

    public static void main(String[] args) throws IOException, InterruptedException {
        Send s = new Send();
        Receive r = new Receive();
        // 连接管道
        s.getPos().connect(r.getPis());
//    r.getPis().connect(s.getPos());
// 启动线程 new Thread(r).start(); // 启动线程 new Thread(s).start(); } }

      -- Console : 

接收到的内容为:
为何在这里 疲倦象半醉
曾在这空间 跟你相拥抱
只有唏嘘的追忆
无言落寞地落泪
谁在这晚里放纵与她温馨
无尽空虚
似把刀锋静静穿过心窝

        -- 以上程序定义了两个线程对象,在发送的线程类中定义了管道输出流,在接收的线程类中定义了管道输入流,在操作时只需要使用PipedOutputStream类中提供的connect()方法就可以将两个线程管道连接在一起,线程启动后会自动进行管道的输入、输出操作。

    打印流

      -- -- -- 打印流主要包含字节打印流(PrintStream)和字符打印流(PrintWriter)

      打印流的基本操作

      使用打印流进行格式化

    System类对IO的支持

      System.out

      System.err

      System.in

      输入/输出重定向

    BufferedReader类

      键盘输入数据的标准格式

      相关操作实例

    Scanner类

      Scanner类简介

      使用Scanner类输入数据

    数据操作流

      DataOutputStream

      DataInputStream

      -- -- DataOutputStream : 

public class DataOutputStream extends FilterOutputStream implements DataOutput

      -- -- FilterOutputStream : 

public class FilterOutputStream extends OutputStream

        ⊙ public DataOutputStream(OutputStream out) : 

        ⊙ public synchronized void write(int b) throws IOException : 

        ⊙ public synchronized void write(byte b[], int off, int len) throws IOException : 

        ⊙ public final void writeByte(int v) throws IOException : 

        ⊙ public final void writeShort(int v) : 

        ⊙ public final void writeInt(int v) throws IOException : 

        ⊙ public final void writeLong(long v) throws IOException : 

        ⊙ public final void writeFloat(float v) throws IOException : 

        ⊙ public final void writeDouble(double v) throws IOException : 

        ⊙ public final void writeBoolean(boolean v) throws IOException : 

        ⊙ public final void writeChar(int v) throws IOException : 

        ⊙ public final void writeChars(String s) throws IOException : 

        ⊙ public final void writeBytes(String s) throws IOException : 

        ⊙ public void flush() throws IOException : 

      -- -- DataInputStream: 

public class DataInputStream extends FilterInputStream implements DataInput

      -- -- FilterInputStream: 

public class FilterInputStream extends InputStream

        ⊙ public DataInputStream(InputStream in) : 

        ⊙ public final int read(byte b[]) throws IOException : 

        ⊙ public final int read(byte b[], int off, int len) throws IOException : 

        ⊙ public final byte readByte() throws IOException : 

        ⊙ public final short readShort() throws IOException : 

        ⊙ public final int readInt() throws IOException : 

        ⊙ public final long readLong() throws IOException : 

        ⊙ public final float readFloat() throws IOException : 

        ⊙ public final double readDouble() throws IOException : 

        ⊙ public final boolean readBoolean() throws IOException : 

        ⊙ public final char readChar() throws IOException : 

        ⊙ public final String readLine() throws IOException : 

    合并流

      -- -- SequenceInputStream : 

public class SequenceInputStream extends InputStream

        ⊙ public SequenceInputStream(InputStream s1, InputStream s2) : 

        ⊙ public int available() throws IOException : 

        ⊙ public int read() throws IOException : 

        ⊙ public int read(byte b[], int off, int len) throws IOException : 

        ⊙ public void close() throws IOException : 

    压缩流

      ZIP压缩输入/输出流简介

        -- -- java.util.zip

          -- -- ZipFile、ZipOutputStream、ZipInputStream、ZipEntry

        -- -- java.util.jar

          -- --  JarOutputStream、JarInputStream、JARFile、JAREntry

        -- -- java.util.zip

          -- -- GZIPOutputStream、GZIPInputStream

        -- -- -- -- 在每一个压缩文件中都会存在多个子文件,那么每一个子文件在Java中就使用ZipEntry表示。

          -- -- -- -- -- -- -- --

          ⊙ public ZipEntry(String name) : 使用指定名称创建新的 ZIP 条目。

          ⊙ public boolean isDirectory() :  如果为目录条目,则返回 true。

      ZipOutputStream类

        -- -- FilterOutputStream

publicclass FilterOutputStream extends OutputStream

        -- -- DeflaterOutputStream

public class DeflaterOutputStream extends FilterOutputStream

        -- -- ZipOutputStream

public class ZipOutputStream extends DeflaterOutputStream implements ZipConstants

        ⊙ public ZipOutputStream(OutputStream out) : 创建新的 ZIP 输出流。

        ⊙ public void putNextEntry(ZipEntry e) throws IOException : 开始写入新的 ZIP 文件条目并将流定位到条目数据的开始处。

        ⊙ public void setComment(String comment) : 设置 ZIP 文件注释。

package object.outputStream.zipOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipOutputStreamDemo2 {

    public static void main(String[] args) throws IOException {
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("F:/limezip.zip"));
        zipDir("F:/limes",zipOut);
        zipOut.close();
    }

    private static void zipDir(String inFile, ZipOutputStream zipOut) throws IOException {
        File file = new File(inFile);
        if(file.isDirectory()){
            File[] listFiles = file.listFiles();
            for(File fl : listFiles){
                zipDir(fl.getPath(), zipOut);
            }
        }else{
            zipOut.putNextEntry(new ZipEntry(inFile));
            InputStream in = new FileInputStream(inFile);
            byte[] temp = new byte[2048 * 2];
            while(in.read(temp) != -1){
                zipOut.write(temp);
            }
            in.close();
        }
    }
}

      ZipFile类

        -- -- 每一个压缩文件都可以使用ZipFile表示,还可以使用ZipFile根据压缩后的文件名找到每一个压缩文件中的ZipEntry并将其进行解压缩操作。

          ⊙ public ZipFile(String name) throws IOException : 

          ⊙ public ZipFile(File file, int mode) throws IOException : 

          ⊙ public ZipFile(File file) throws ZipException, IOException : 

          ⊙ public ZipFile(File file, int mode, Charset charset) : 

          ⊙ public ZipFile(String name, Charset charset) throws IOException : 

          ⊙ public ZipFile(File file, Charset charset) throws IOException : 

          ⊙ public ZipEntry getEntry(String name) : 

          ⊙ public InputStream getInputStream(ZipEntry entry) throws IOException : 

          ⊙ public String getName() : 

      ZipInputStream类

        -- -- 

          ⊙ public ZipInputStream(InputStream in) : 

          ⊙ public ZipEntry getNextEntry() throws IOException : 

    回退流

      -- -- PushbackInputStream

      -- -- PushbackReader

        -- -- PushbackInputStream

          ⊙ public PushbackInputStream(InputStream in) : 

          ⊙ public PushbackInputStream(InputStream in, int size) : 

          ⊙ public void unread(int b) throws IOException : 

          ⊙ public void unread(byte[] b) throws IOException : 

          ⊙ public void unread(byte[] b, int off, int len) throws IOException : 

package object.outputStream.zipOutputStream;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class PushBackInputStreamDemo {

    public static void main(String[] args) throws IOException {
        String str = "lime.oracle.Yu";
        PushbackInputStream pushbackInputStream = new PushbackInputStream(new ByteArrayInputStream(str.getBytes()));
        System.out.println(str);
        System.out.println("------------");
        int temp = 0;
        while((temp = pushbackInputStream.read()) != -1){
            if(temp == (int)'.'){
                pushbackInputStream.unread((int)'/');
            }else{
                System.out.print((char)temp);
            }
        }
    }
}

    

package object.outputStream.zipOutputStream;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class PushBackInputStreamDemo {

    public static void main(String[] args) throws IOException {
        String str = "lime.oracle.Yu";
        PushbackInputStream pushbackInputStream = new PushbackInputStream(new ByteArrayInputStream(str.getBytes()),2);
        System.out.println(str);
        System.out.println("------------");
        int temp = 0;
        while((temp = pushbackInputStream.read()) != -1){
            if(temp == (int)'.'){
                pushbackInputStream.unread((int)'/');
                pushbackInputStream.unread((int)'!');
            }else{
                System.out.print((char)temp);
            }
        }
    }
}

    字符编码

      Java 常见编码简介

        ⊙ ISO8859-1 : 属于单字节编码,最多只能表示0~255的字符范围,主要在英文上应用。

        ⊙ GBK/GB2312 : 中文的国标编码,专门用来表示汉字,是双字节编码。GBK可以标识简体中文和繁体中文,而GB2312只能标识简体中文,GBK兼容GB2312.

        ⊙  unicode: Java中使用此编码方式,是最标准的一种编码,使用十六进制标识编码。但此编码不兼容ISO8859-1编码。

        ⊙  UTF : 由于unicode不支持ISO8859-1编码,而且容易占用更多的空间,而且对于英文字母也需要使用两个字节编码,这样使用unicode不便于传输和存储,因此产生了UTF编码。UTF编码兼容了ISO8859-1编码,同时也可以用来标识所有的语言字符,不过UTF编码是不定长编码,每一个字符的长度为1~6个字节不等。

      得到本机的编码显示

System.out.println("系统默认编码 : " + System.getProperty("file.encoding"));
系统默认编码 : UTF-8

      乱码产生

    对象序列化

      基本概念与Serializable接口

        -- 对象序列化就是把一个对象变为二进制的数据流的一种方法。通过对象序列化可以方便地实现对象的传输或存储。

        -- -- Serializable : 标识接口,标识其实现类具有被序列化的能力。

public interface Serializable {
}

        -- -- -- 使用对象输出流输出序列化对象的步骤有时也称为序列化,而使用对象输入流读取对想的过程有时也称为反序列化。

        -- -- -- -- 提示:

        -- -- -- -- 在对象进行序列化或反序列化操作时,要考虑JDK版本的问题。如果序列化的JDK版本和反序列化的JDK版本不同意则就有可能造成异常,所以在序列化操作中引入了一个serialVersionUID的常量,可以通过此常量来验证版本的一致性。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现反序列化版本不一致的异常。

        -- -- -- -- 当实现java.io.Serializable接口的实体(类)没有显示地定义一个名为serialVersionUID、类型为long的变量时,Java序列化机制在编译时会自动生成一个此版本的serialVersionUID。当然,如果不希望通过编译来自动生成,也可以直接显示地定义一个名为serialVersionUID、类型为long的变量,只要不修改这个变量值的序列化实体,都可以相互进行串行化和反串行化。

      对象输出流ObjectOutputStream

        -- -- ObjectOutputStream : 只有属性被序列化

          ⊙ public ObjectOutputStream(OutputStream out) throws IOException :

          ⊙ public final void writeObject(Object obj) throws IOException :

      对象输入流ObjectInputStream

        -- -- ObjectOutputStream : 只有属性被序列化

          ⊙ public ObjectInputStream(InputStream in) throws IOException :

          ⊙ public final Object readObject() throws IOException, ClassNotFoundException:

      Externalizable接口

        -- -- Externalizable : 指定序列化的内容

public interface Externalizable extends java.io.Serializable

          ⊙ public void writeExternal(ObjectOutput out) throws IOException  :   对象实现 readExternal 方法来恢复其内容,它通过调用 DataInput 的方法来恢复其基础类型,调用 readObject 来恢复对象、字符串和数组。

          ⊙ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException : 该对象可实现 writeExternal 方法来保存其内容,它可以通过调用 DataOutput 的方法来保存其基本值,或调用 ObjectOutput 的 writeObject 方法来保存对象、字符串和数组。

          -- -- OjbectOutput : 

public interface ObjectOutput extends DataOutput, AutoCloseable

          

//    覆写此方法,根据需要可以保存属性或具体内容,序列化时使用
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(this.name);
        out.writeInt(this.age);
    }

          -- -- OjbectInput : 

public interface ObjectInput extends DataInput, AutoCloseable

        

//    覆写此方法,根据需要读取内容,反序列化时使用
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.name = (String)in.readObject();
        this.age = in.readInt();
    }

        -- -- 如果一个类要使用Externalizable实现序列化时,在此类中必须存在一个无参构造方法,因为在反序列化时会默认调用无参构造实例化对象,如果没有此无参构造,则运行时会将出现异常,这一点的实现机制与Serializable接口是不同的。

      transient关键字

        -- -- transient:对象中被transient修饰的属性不会被序列化。

    private transient String name;
    private int age;

      序列化一组对象

    实例操作 -- -- 单人信息管理程序

  Java 新IO 

    Java 新IO简介

    缓冲区与Buffer

      Buffer的基本操作

      深入缓冲区操作

      创建子缓冲区

      创建只读缓冲区

      创建直接缓冲区

    通道

      FileChannel

      内存映射

      文件锁:FileLock

      字符集:Charset

      Selector

啦啦啦

    啦啦啦

  啦啦啦

啦啦啦

原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6901232.html