主頁 > 後端開發 > Java-原生IO通覽

Java-原生IO通覽

2020-12-09 07:03:44 後端開發

為什么叫原生IO,也就是后續有更加強大的NIO、AIO操作,但原生IO是基礎,先學習一下!

IO流的概念

前面介紹JavaAPI的時候,只是對目錄/檔案進行操作,而具體的內容操作就需要IO流

I/O的全稱是Input/Output,顧名思義就是用來設備之間傳輸資料的

io的示意圖

分類

io體系

io體系

io體系

乍一看分類太雜了,但是如果作為初學者不用全部掌握,掌握幾個流的操作方法,其它的流操作也是大同小異!

基本使用步驟

之前在介紹JavaAPI的時候介紹過File類的含義,我們進行IO操作的時候,就是依靠這個File類,先去創建它的物件!

輸入:

  1. 創建File類的物件,指定讀取資料的來源

  2. 創建對應的輸入流物件,將File類的物件作為引數

  3. 傳輸資料,創建相應的byte[] 或 char[],

  4. 關閉流物件(占用系統資源)

輸出:

  1. 創建File類的物件,指定讀取資料的來源,檔案不存在時會進行創建

  2. 創建對應的輸入流物件,將File類的物件作為引數

  3. 傳輸資料,write(char[]/byte[] buffer,0,len)

  4. 關閉流物件(占用系統資源)

輸入流

位元組流

主要操作的是抽象類InputStream的子類,看InputStream的描述

/**
 * This abstract class is the superclass of all classes representing
 * an input stream of bytes.
 *
 * <p> Applications that need to define a subclass of <code>InputStream</code>
 * must always provide a method that returns the next byte of input.
 *
 * 此抽象類表示所有位元組輸入流類的超類
 * InputStream的子類應用程式需要提供一個輸入位元組的方法,
 */
 public abstract class InputStream implements Closeable {

//讀取資料的方法
public abstract int read() throws IOException;  //一個一個位元組進行讀取,讀到最后一個位元組之后回傳-1
public int read(byte b[]) throws IOException{} //讀取陣列長度的位元組
public int read(byte b[], int off, int len) throws IOException{}
public class TestFileInputsream {
    public static void main(String[] args) throws IOException {
        //1、創建源檔案物件
        File file = new File("F:\\test.txt");
         //2、創建源檔案到程式的輸出流物件
        InputStream inputStream = new FileInputStream(file);
        //3、讀取源檔案
        int read = inputStream.read();  //一個一個位元組進行讀取,讀到最后一個位元組之后回傳-1
        while (read != -1) {
            System.out.print((char) read);
            read = inputStream.read();
        }
        //4、關閉輸出流物件
         inputStream.close();
    }
}
package com.ty.inputstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class TestFileInputsream2 {
    public static void main(String[] args) throws IOException {
        //1、創建源檔案物件
        File file = new File("F:\\test.txt");
        //2、創建源檔案到程式的輸出流物件
        InputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[1024];  //利用緩沖陣列,開辟1024個空間
        //3、讀取源檔案,讀取陣列長度的位元組
        int read = inputStream.read(bytes);
        while (read != -1) {
            for (int i = 0; i < read; i++) {
                System.out.print((char) bytes[i]);
            }
            read = inputStream.read();
        }
        //4、關閉輸出流物件
        inputStream.close();
    }
}

System.in

前面在常用的API介紹過System是一個工具類,而System.in此時得到的其實是一個標準的輸入流,而且是一個位元組流

/**
 * The "standard" input stream. This stream is already
 * open and ready to supply input data. Typically this stream
 * corresponds to keyboard input or another input source specified by
 * the host environment or user.
 *
 * “標準”輸入流,該流已經打開,可以提供輸入資料了,通常,此流對應于鍵盤輸入或主機環境和用戶指定的另一個輸入源,
 */
public final static InputStream in = null;
public class TestSystemIn {
    public static void main(String[] args) throws IOException {
        /*
         * InputStream in = System.in;
         * int read = in.read();//read方法等待鍵盤的錄入,是一個阻塞方法,
         * System.out.println(read);
         */
        /**從鍵盤錄一個資料
         * Scanner 掃描器,掃描鍵盤到程式的那個輸入流
         * 還可以掃描其他流,比如:Scanner sc=new Scanner(new FileInputStream(new File("F:\\test.txt")));
         */
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(num);

        Scanner sc=new Scanner(new FileInputStream(new File("F:\\test.txt")));
        while (sc.hasNext()){
            System.out.println(sc.next());
        }
    }
}

字符流

主要操作的是抽象類Reader的子類,看Reader的描述

/**
 * Abstract class for reading character streams.  The only methods that a
 * subclass must implement are read(char[], int, int) and close().  Most
 * subclasses, however, will override some of the methods defined here in order
 * to provide higher efficiency, additional functionality, or both.
 *
 * 讀取字符流的抽象類, 子類必須實作的唯一方法是read(char [],int,int)和close(),
 * 但是,大多數子類將覆寫此處定義的某些方法,以提供更高的效率和/或附加功能,
 */
 public abstract class Reader implements Readable, Closeable {
     
 //讀取資料的方法
 public int read() throws IOException{}
 public int read(char cbuf[]) throws IOException{}
 abstract public int read(char cbuf[], int off, int len) throws IOException;
public class TestFileReader {
    public static void main(String[] args) throws IOException {
        //1、創建源檔案物件
        File file = new File("F:\\test.txt");
        //2、創建源檔案到程式的輸出流物件
        Reader reader = new FileReader(file);
        //3、讀取源檔案,一個一個字符進行讀取
        int read = reader.read();
        while (read != -1) {
            System.out.print((char) read);
            read = reader.read();
        }
        //4、關閉輸出流物件
        reader.close();
    }
}
public class TestFileReader2 {
    public static void main(String[] args) throws IOException {
        //1、創建源檔案物件
        File file = new File("F:\\test.txt");
        //2、創建源檔案到程式的輸出流物件
        Reader reader = new FileReader(file);
        char[] chars=new char[1024];
        //3、讀取源檔案,讀取陣列長度的字符
        int read = reader.read(chars);
        while (read != -1) {
//            for (int i = 0; i < read; i++) {
//                System.out.print(chars[i]);
//            }
            System.out.print(new String(chars,0,read)); //將陣列轉為String
            read=reader.read(chars);
        }
        //4、關閉輸出流物件
        reader.close();
    }
}

輸出流

位元組流

主要操作的是抽象類OutputStream的子類,看OutputStream的描述

/**
 * This abstract class is the superclass of all classes representing
 * an output stream of bytes. An output stream accepts output bytes
 * and sends them to some sink.
 * <p>
 * Applications that need to define a subclass of
 * <code>OutputStream</code> must always provide at least a method
 * that writes one byte of output.
 *
 * 此抽象類表示所有位元組輸出流類的超類,
 * OutputStream的子類應用程式需要提供一種回傳一個位元組輸出的方法,
 */
public abstract class OutputStream implements Closeable, Flushable {
    
//輸出資料的方法
public abstract void write(int b) throws IOException;//把資料輸出到檔案中,一個一個位元組輸出位元組
public void write(byte b[]) throws IOException{}		 //輸出位元組長度的位元組
public void write(byte b[], int off, int len) throws IOException{} //輸出位元組長度的位元組,從off開始長度為len
public class TestFileOutputStream {
    public static void main(String[] args) throws IOException {
        //1、創建目標檔案
        File file=new File("F:\\demo.txt");
        //2、創建程式到目標檔案的輸出流,這種情況前面指定的目標檔案時如果不存在則會進行創建
        OutputStream outputStream=new FileOutputStream(file);
      //OutputStream outputStream=new FileOutputStream(file,true);默認會對檔案進行覆寫,這樣會對檔案進行追加
        String str="hello";
        byte[] bytes = str.getBytes();
        for (byte b : bytes) {
            //3、把資料輸出到檔案中
            outputStream.write(b);
        }
        //4、關閉輸出流
        outputStream.close();
    }
}
public class TestFileOutputStream2 {
    public static void main(String[] args) throws IOException {
        //1、創建目標檔案
        File file=new File("F:\\demo.txt");
        //2、創建程式到目標檔案的輸出流,這種情況前面指定的目標檔案時如果不存在則會進行創建
        OutputStream outputStream=new FileOutputStream(file);
        String str="hello,world";
        byte[] bytes = str.getBytes();
        //3、把資料輸出到檔案中,以位元組長度輸出到檔案
        outputStream.write(bytes);
        //4、關閉輸出流
        outputStream.close();
    }
}

System.out

此時回傳的是一個輸出流 、 列印流(PrintStream)

/**
 * The "standard" output stream. This stream is already
 * open and ready to accept output data. Typically this stream
 * corresponds to display output or another output destination
 * specified by the host environment or user.
 * <p>
 * For simple stand-alone Java applications, a typical way to write
 * a line of output data is:
 * <blockquote><pre>
 *     System.out.println(data)
 * </pre></blockquote>
 * <p>
 * See the <code>println</code> methods in class <code>PrintStream</code>.
 *
 * “標準”輸出流, 該流已經打開,并準備接受輸出資料,通常,此流對應于主機環境或用戶指定的顯示輸出或另一個輸出目標,
 *  對于簡單的獨立Java應用程式,寫一行輸出資料的典型方法是:System.out.println(data)
 *  請參見類PrintStream的print方法,
 */
 public final static PrintStream out = null;
public class TestSystemOut {
    public static void main(String[] args) {
        PrintStream out = System.out;
        out.println("hello");        // System.out.println("hello"),輸出之后換行
        out.print("hello,world");  // System.out.print("hello,world"),直接輸出
    }
}

字符流

主要操作的是抽象類Writer的子類,看Writer的描述

/**
 * Abstract class for writing to character streams.  The only methods that a
 * subclass must implement are write(char[], int, int), flush(), and close().
 * Most subclasses, however, will override some of the methods defined here in
 * order to provide higher efficiency, additional functionality, or both.
 *
 * 寫入字符流的抽象類,子類必須實作的唯一方法是write(char [],int,int),flush()和close(),
 * 但是,大多數子類將覆寫此處定義的某些方法,以提供更高的效率和/或附加功能,
 */
public abstract class Writer implements Appendable, Closeable, Flushable {
    
//輸出資料的方法 
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 class TestFileWriter {
    public static void main(String[] args) throws IOException {
        //創建程式到目標檔案的輸出流,創建目標檔案,
        Writer writer=new FileWriter(new File("F:\\demo.txt"));
        String str="夜曲";
        for (int i = 0; i < str.length(); i++) {
            //把資料輸出到檔案中
            writer.write(str.charAt(i));
        }
        writer.write(str,0,str.length());
        writer.write("我的夢!");
       //關閉輸出流
        writer.close();
    }
}
public class TestFileWriter2 {
    public static void main(String[] args) throws IOException {
        Writer writer=new FileWriter(new File("F:\\demo.txt"));
        String str="夜曲";
        char[] chars=str.toCharArray();
        writer.write(chars);
        writer.close();
    }
}

復制文本

public class CopyText {
    public static void main(String[] args) throws IOException {
        Reader reader = new FileReader(new File("F:\\a.txt"));
        Writer writer = new FileWriter(new File("F:\\b.txt"));
        char[] chars = new char[1024];
        int read = reader.read(chars);
        while (read != -1) {
            writer.write(chars, 0, read);
            read = reader.read(chars);
        }
        //關閉流的流程:先用后關
        writer.close();
        reader.close();
    }
}

處理流

處理流就是在普通流也就是節點流的基礎再嵌套一個流!

緩沖流

緩沖區(Buffered):BufferedOutputStream/BufferInputStream;字符:BufferedReader/BufferedWriter

  • 提高IO效率,減少訪問磁盤的次數

  • 資料存盤在緩沖區中,flush是將快取區的內容寫入檔案中,也可以直接close

public class TestInputStream {
    public static void main(String[] args) throws IOException {
        File file = new File("F:\\test.txt");
        InputStream inputStream = new FileInputStream(file);
        //在FileInputStream的基礎上套用一個BufferedInputStream
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] bytes = new byte[1024 * 10];
        int read = bufferedInputStream.read(bytes);
        while (read != -1) {
            for (int i = 0; i < read; i++) {
                System.out.println((char) bytes[i]);
            }
            read = bufferedInputStream.read(bytes);
        }
        bufferedInputStream.close();
    }
}

復制圖片

public class CopyPicture {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream(new File("F:\\test.jpg"));
        OutputStream outputStream = new FileOutputStream(new File("F:\\copy.jpg"));
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        byte[] chars = new byte[1024 * 10];
        int read = bufferedInputStream.read(chars);
        while (read != -1) {
            bufferedOutputStream.write(chars, 0, read);
            bufferedOutputStream.flush();
            read = bufferedInputStream.read(chars);
        }
        bufferedOutputStream.close();
        bufferedInputStream.close();
    }
}

復制檔案夾

public class CopyDir {
    public static void main(String[] args) {
        //copyFile(new File("F:/a.txt"), new File("F:/b.txt"));
        copyDir(new File("F:/a"), new File("F:/b"));
    }

    //復制檔案夾
    public static void copyDir(File srcFile, File targetFile) {
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        File[] files = srcFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                copyDir(new File(srcFile + File.separator + file.getName()), new File(targetFile + File.separator + file.getName())
                );
            }
            if (file.isFile()) {
                copyFile(new File(srcFile + File.separator + file.getName()), new File(targetFile + File.separator + file.getName()));
            }
        }
    }

    //復制檔案
    public static void copyFile(File srcFile, File targetFile) {
        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(srcFile));
            outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
            byte[] bytes = new byte[1024 * 8];
            int read = inputStream.read(bytes);
            while (read != -1) {
                outputStream.write(bytes, 0, read);
                read = inputStream.read(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

通常我們進行io操作,例外不會向上拋,只會處理例外,但上面代碼處理例外看的有些累贅!用上之前在例外章節說得try-resource寫法

public class CopyDir {
    public static void main(String[] args) {
        copyDir(new File("F:/a"), new File("F:/b"));
    }

    //復制檔案夾
    public static void copyDir(File srcFile, File targetFile) {
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        File[] files = srcFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                copyDir(new File(srcFile + File.separator + file.getName()), new File(targetFile + File.separator + file.getName())
                );
            }
            if (file.isFile()) {
                copyFile(new File(srcFile + File.separator + file.getName()), new File(targetFile + File.separator + file.getName()));
            }
        }
    }

    //復制檔案
    public static void copyFile(File srcFile, File targetFile) {
        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile));
             BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));) {
            byte[] bytes = new byte[1024 * 8];
            int read = inputStream.read(bytes);
            while (read != -1) {
                outputStream.write(bytes, 0, read);
                read = inputStream.read(bytes);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

關閉io代碼都不用我們處理了,其實是底層幫我們處理的,看反編譯之后的class

public class CopyDir
{
  public static void main(String[] args)
  {
    copyDir(new File("F:/c"), new File("F:/d"));
  }

  public static void copyDir(File srcFile, File targetFile)
  {
    if (!targetFile.exists()) {
      targetFile.mkdirs();
    }
    File[] files = srcFile.listFiles();
    for (File file : files) {
      if (file.isDirectory()) {
        copyDir(new File(srcFile + File.separator + file.getName()), new File(targetFile + File.separator + file.getName()));
      }

      if (file.isFile())
        copyFile(new File(srcFile + File.separator + file.getName()), new File(targetFile + File.separator + file.getName()));
    }
  }

  public static void copyFile(File srcFile, File targetFile)
  {
    try {
      BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile)); Throwable localThrowable6 = null;
      try { BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));

        Throwable localThrowable7 = null;
        try {
          byte[] bytes = new byte[8192];
          int read = inputStream.read(bytes);
          while (read != -1) {
            outputStream.write(bytes, 0, read);
            read = inputStream.read(bytes);
          }
        }
        catch (Throwable localThrowable1)
        {
          localThrowable7 = localThrowable1; throw localThrowable1;
        }
        finally
        {
          if (outputStream != null) if (localThrowable7 != null) try { outputStream.close(); } catch (Throwable localThrowable2) { localThrowable7.addSuppressed(localThrowable2); } else outputStream.close();
        }
      }
      catch (Throwable localThrowable4)
      {
        localThrowable6 = localThrowable4; throw localThrowable4;
      }
      finally
      {
        if (inputStream != null) if (localThrowable6 != null) try { inputStream.close(); } catch (Throwable localThrowable5) { localThrowable6.addSuppressed(localThrowable5); } else inputStream.close();  
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

轉換流

主要操作的是InputStreamReader/OutputStreamWriter類

/**
 * An InputStreamReader is a bridge from byte streams to character streams: It
 * reads bytes and decodes them into characters using a specified {@link
 * java.nio.charset.Charset charset}.  The charset that it uses
 * may be specified by name or may be given explicitly, or the platform's
 * default charset may be accepted.
 *
 * InputStreamReader是從位元組流到字符流的橋梁:它讀取位元組,并使用指定的charset它們解碼為charset , 
 * 它使用的字符集可以通過名稱指定,也可以顯式指定,或者可以接受平臺的默認字符集,
 */
public class InputStreamReader extends Reader {
/**
 * An OutputStreamWriter is a bridge from character streams to byte streams:
 * Characters written to it are encoded into bytes using a specified {@link
 * java.nio.charset.Charset charset}.  The charset that it uses
 * may be specified by name or may be given explicitly, or the platform's
 * default charset may be accepted.
 *
 * OutputStreamWriter是從字符流到位元組流的橋梁:寫入到字符流的字符使用指定的charset編碼為位元組, 
 * 它使用的字符集可以通過名稱指定,也可以顯式指定,或者可以接受平臺的默認字符集,
 */
public class TestInputStreamReader {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream(new File("F:/test.txt"));
        
        /**需要指定一個編碼格式,如果不指定則按照開發工具的編碼格式進行轉換
         * 如果轉換格式不統一就會亂碼
         */
        InputStreamReader reader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(reader);
        char[] chars = new char[1024 * 10];
        int read = bufferedReader.read(chars);
        while (read != -1) {
            for (int i = 0; i < read; i++) {
                System.out.print(chars[i]);
            }
            read = bufferedReader.read(chars);
        }
        bufferedReader.close();
    }
}
public class Copy {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader=new InputStreamReader(new FileInputStream(new File("F:\\test.txt")));
        OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream(new File("F:\\copy.txt")));
        BufferedReader bufferedReader=new BufferedReader(reader);
        BufferedWriter bufferedWriter=new BufferedWriter(writer);
        char[] chars=new char[1024*10];
        int read = bufferedReader.read(chars);
        while (read!=-1){
            bufferedWriter.write(chars);
            read = bufferedReader.read(chars);
        }
        bufferedWriter.close();
        bufferedReader.close();
    }
}

鍵盤輸入到文本

public class InputToText {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in);
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File("F:\\text.txt")));
        BufferedReader bufferedReader = new BufferedReader(reader);
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        String str = bufferedReader.readLine();
        while (!"exit".equals(str)) {
            bufferedWriter.write(str);
            bufferedWriter.newLine();
            str = bufferedReader.readLine();
        }
        bufferedWriter.close();
        bufferedReader.close();
    }
}

資料流

用來操作基本資料型別和字串的,主要操作DataInputStream和DataOutputStream類

/**
 * A data input stream lets an application read primitive Java data
 * types from an underlying input stream in a machine-independent
 * way. An application uses a data output stream to write data that
 * can later be read by a data input stream.
 * <p>
 * DataInputStream is not necessarily safe for multithreaded access.
 * Thread safety is optional and is the responsibility of users of
 * methods in this class.
 *
 * 資料輸入流允許應用程式以與機器無關的方式從基礎輸入流中讀取原始Java資料型別, 
 * 應用程式使用資料輸出流來寫入資料,以后可以由資料輸入流讀取,
 * DataInputStream對于多執行緒訪問不一定是安全的,執行緒安全是可選的,并且是此類中用戶的責任
 * DataInputStream:將檔案中存盤的基本資料型別和字串寫入記憶體的變數中
 */
public class DataInputStream extends FilterInputStream implements DataInput {
/**
 * A data output stream lets an application write primitive Java data
 * types to an output stream in a portable way. An application can
 * then use a data input stream to read the data back in.
 *
 * 資料輸出流允許應用程式以可移植的方式將原始Java資料型別寫入輸出流,然后,應用程式可以使用資料輸入流來讀回資料,
 * DataOutputStream:將記憶體中的基本資料型別和字串的變數寫出到檔案中
 */
public class DataOutputStream extends FilterOutputStream implements DataOutput {
public class TestDataOutputStream {
    public static void main(String[] args) throws IOException {
        //路徑不指定盤符是相對路徑,指代當前專案工程下
        DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(new File("data.txt")));
        outputStream.writeUTF("同一首歌!");
        outputStream.write(66);
        outputStream.writeBoolean(true);
        outputStream.writeDouble(6.6);
        outputStream.close();

        // 檔案輸出:同一首歌!B@ffffff,這個不是給你看的,是給程式看的
    }
}
public class TestDataInputStream {
    public static void main(String[] args) throws IOException {
        DataInputStream inputStream = new DataInputStream(new FileInputStream(new File("data.txt")));
        //寫出的型別跟讀入的型別一一匹配!
        System.out.println(inputStream.readUTF());
        System.out.println(inputStream.read());
        System.out.println(inputStream.readBoolean());
        System.out.println(inputStream.readDouble());
        inputStream.close();
    }
}

物件流

可以把Java中的物件寫入到資料源中,也能把物件從資料源中還原回來,主要操作ObjectInputStream和ObjectOutputStream類

/**
 * An ObjectInputStream deserializes primitive data and objects previously
 * written using an ObjectOutputStream.
 *
 * ObjectInputStream反序列化以前使用ObjectOutputStream撰寫的原始資料和物件,
 */
public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants{
/**
 * An ObjectOutputStream writes primitive data types and graphs of Java objects
 *
 * ObjectOutputStream將Java的原始資料型別和物件型別寫入OutputStream,
 * 此操作也被稱為序列化
 */

物件序列化的細節:

  • 必須實作Serializable介面
  • serialVersionUID的作用:序列化版本號,保證更改序列化類的結構時,不會對反序列化結果產生影響
  • 必須保證其所有屬性均可序列化
  • 使用transient和static關鍵字修飾的屬性,不會參與序列化
public class User implements Serializable {

    private static final long serialVersionUID = 7516451367496182472L;

    private String userName;
    private transient String password;
    private static double balance;
    private Integer number;

    public User() {
    }

    public User(String userName, String password, Integer number) {
        this.userName = userName;
        this.password = password;
        this.number = number;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static double getBalance() {
        return balance;
    }

    public static void setBalance(double balance) {
        User.balance = balance;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName=" + userName +
                ", password='" + password + '\'' +
                ", number=" + number +
                ", balance=" + balance +
                '}';
    }
}
public class TestObjectOutputStream {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream(new File("user.txt")));
        User user=new User("jack","123456",17);
        User.setBalance(8500.0);
        System.out.println(user);
        outputStream.writeObject(user);
        outputStream.write(45);
        outputStream.writeBoolean(true);
        outputStream.close();
    }
}
public class TestObjectInputStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream(new File("user.txt")));
        System.out.println(inputStream.readObject());
        System.out.println(inputStream.read());
        System.out.println(inputStream.readBoolean());
        inputStream.close();
    }
}

編碼格式

編碼分類

字符編碼 字符編碼介紹
ISO-8856-1 收錄ASCII外,還包括西歐、希臘語、泰語、阿拉伯語、希伯來語對應的文字符號
UTF-8 針對Unicode的可變長度字符編碼,Windows系統中文默認為3個位元組
GB2312 簡體中文
GBK 簡體中文、擴充,Windows系統中文默認為2個位元組
BIG5 臺灣,繁體中文
注意: 當編碼方式和解碼方式不一致時,會出現亂碼

編碼和解碼轉換

public class TestEncoding {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "hello,歡迎";
        //不傳字符集時,默認按照IDE的編碼
        byte[] binary = str.getBytes("UTF-8"); //獲得字串的二進制表現形式:104  101    108    108    111    44 -26    -84    -94    -24    -65    -114
        for (int i = 0; i < binary.length; i++) {
            System.out.print(binary[i] + "\t");
        }

        String text=new String(binary,"gbk");
        System.out.println(text); //hello,嬈㈣繋

        //錯誤做法
        byte[] bytes = str.getBytes("gbk");
        String s = new String(bytes, "utf-8");
        System.out.println(s);//輸出:hello,???  亂碼那部分表示在UTF-8的編碼中沒有對應gbk的編碼

        //正確做法,編碼和解碼保持一致
        bytes=str.getBytes("utf-8");
        s = new String(bytes, "utf-8");
        System.out.println(s); //輸出:hello,歡迎
    }
}

工具庫:commons-io

原生的io操作比較復雜,有一個工具庫可以簡化我們的操作就是Common IO

參考地址:http://commons.apache.org/proper/commons-io/index.html,可以查看使用的API

先引入commons-io的jar包,我們用maven引入,不會使用的話,手動下載之后導包也可以

 <dependencies>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.8.0</version>
    </dependency>
 </dependencies>
public class TestCommon {
    public static void main(String[] args) throws IOException {
        long size = FileUtils.sizeOf(new File("F:\\c"));
        System.out.println("檔案夾大小:" + size);
        FileUtils.copyFile(new File("F:\\a\\test.txt"),new File("F:\\a\\copy.txt"));
        FileUtils.write(new File("F:\\a\\test.txt"),"hello,Spring!");
    }
}

ps:以上只是io體系的普通io,java有另一種io,即:NIO,是面向緩沖區的,所以會更高效,后續也是必學的!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/231743.html

標籤:Java

上一篇:SpringCloud-服務間通信方式

下一篇:6、Sping Cloud Feign

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more