主頁 > 後端開發 > IO流詳解

IO流詳解

2022-07-13 07:42:06 後端開發

一、IO流概述

1.原理

![](https://img2022.cnblogs.com/blog/2901531/202206/2901531-20220621172751004-1385246087.png)

2.流的分類

3.流的體系,藍底框為重點掌握的

二、IO流操作

1.節點流-字符流

(1).FileReader讀入資料的基本操作

點擊查看代碼
package com.Tang.io;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class IOTest {
    @Test
    public void test() {
        FileReader fr = null;
        //為了保證流資源一定可以執行關閉操作,需要使用try-catch-finally
        //讀入的檔案一定要存在,否則就會報FileNotFoundException,
        try {
            //將Hello工程下的hello.txt檔案內容讀入程式中,并輸出到控制臺
            //1.實體化File類物件,指明要操作的檔案
            File file = new File("hello.txt");
            //2.提供具體的流
            fr = new FileReader(file);
            //3.資料的讀入
            //read():回傳讀入的一個字符,如果達到檔案末尾,回傳-1;否則回傳字符的Ascall值
            int data = https://www.cnblogs.com/twq46/p/fr.read();
            while(data != -1){
                System.out.print((char)data);//讀取檔案第一個字符
                data = fr.read();//讀取檔案下一個字符
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的關閉操作
            try {
                if(fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

運行結果圖

①.FileReader對read()操作升級:使用read的多載方法
代碼中for回圈處如果寫為i < cubf.length會出現一下問題 ![](https://img2022.cnblogs.com/blog/2901531/202206/2901531-20220622150055674-2067087883.png)
點擊查看代碼
//對read()操作升級:使用read的多載方法
    @Test
    public void test1(){
        FileReader fr = null;
        try {
            //1.File類的實體化
            File file = new File("hello.txt");
            //2.FileReader流的實體化
            fr = new FileReader(file);
            //3.讀入的操作
            //read(char[] cbuf):回傳每次讀入cbuf陣列中的字符的個數,當讀到檔案末尾時回傳-1
            char[] cbuf = new char[5];//相當于一個容量池,每次能從檔案能讀出的最大字符數
            int len;
            while((len = fr.read(cbuf) )!= -1){
                //方式一:
                //錯誤寫法
//                for (int i = 0; i <cbuf.length ; i++) {
//                    System.out.print(cbuf[i]);
//                }
                //正確寫法:每次讀到幾個字符就輸出幾個
//                for (int i = 0; i <len; i++) {
//                    System.out.print(cbuf[i]);
//                }
                //方式二:
                // 將陣列轉化為字符,因為每一次讀到的字符都是在上一次陣列上的覆寫
                //因此每次只需取出從陣列開始位置到所能讀到的字符的即可結束
                String str = new String(cbuf,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
//        fr.read(cbuf);

    }
運行結果圖

(2)FileWriter寫出資料的操作

點擊查看代碼
 /*
    1.輸出操作,對應的File可以不存在,并不會報例外
    2.
         File對應的硬碟中的檔案如果不存在:在輸出的程序中,會自動創建此檔案
         File對應的硬碟中的檔案如果存在:
             如果流使用的構造器是FileWriter(file,false) / FileWriter(false):對原有檔案的覆寫
             如果流使用的構造器是FileWriter(file,true) /:不會對原有檔案覆寫,而是在原有檔案基礎上追加內容
     */

@Test
    public void test2()  {
        FileWriter fw = null;
        try {
            //1.提供File類的物件,指明寫出到的檔案
            File file = new File("hello1.txt");
            //2.提供FileWriter的物件,用于資料的寫出
            fw = new FileWriter(file);
            //3.寫出的操作
            fw.write("I have a dream\n");
            fw.write("you need to hava a dream");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流資源的關閉
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
運行結果圖

(3).使用FileReader和FileWriter實作文本檔案的復制

①一開始就按下方圖片的代碼去寫,然后選中除關閉流的以外的代碼按快捷鍵ctrl + alt + t生成 try - catch - finally然后將關閉流的代碼放入finally中并單獨生成 try - catch ![](https://img2022.cnblogs.com/blog/2901531/202206/2901531-20220622160639391-1084456259.png)
點擊查看代碼
 @Test
    public void test3(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //1.創建File類的物件,指明讀入和寫出的檔案
            File file = new File("hello.txt");
            File file1 = new File("hello2.txt");
            //2.創建輸入流和輸出流的物件
            fr = new FileReader(file);
            fw = new FileWriter(file1);
            //3.資料的讀入和寫出操作
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){//從hello.txt文本中讀入到cbuf陣列len個字符
                //每次將讀入到的len個字符寫出到hello2.txt中
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.關閉流資源
            try {
                if(fw !=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(fr != null)
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
運行結果圖


注意:字符流不能處理圖片檔案的測驗

2.節點流-位元組流

(1)FileInputStream的使用

①位元組流處理文本檔案
點擊查看代碼
/*
    結論:
    1.對于文本檔案(.txt , .java , .c , .cpp)使用字符流處理
    2.對于非文本檔案(.jpg, .mp3, .mp4, .doc, .ppt ......)使用位元組流處理
     */
    @Test
    public void test4(){
        //使用位元組流FileInputStream處理文本檔案,可能出現亂碼
        FileInputStream fis = null;
        try {
            //1.造檔案
            File file = new File("hello.txt");
            //2.造流
            fis = new FileInputStream(file);
            //3.讀資料
            byte[] bytes = new byte[5];
            int len;//記錄每次讀取的位元組的個數
            while((len=fis.read(bytes)) != -1){
                String str = new String(bytes,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//4.關閉資料
            try {
                if(fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
②位元組流處理非文本檔案
點擊查看代碼
//實作對圖片的復制
    @Test
    public void test5() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File file = new File("QQ20210927-0.jpg");
            File file1 = new File("QQ20210927-1.jpg");

            fis = new FileInputStream(file);
            fos = new FileOutputStream(file1);
            byte[] bytes = new byte[5];
            int len ;
            while((len = fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
運行結果圖

(2)FileInputStream和FileOutputStream復制檔案的方法測驗

點擊查看代碼
//指定路徑下的檔案復制
    public void copyFile(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File file = new File(srcPath);
            File file1 = new File(destPath);

            fis = new FileInputStream(file);
            fos = new FileOutputStream(file1);
            byte[] bytes = new byte[1024];
            int len ;
            while((len = fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void test6(){
        long start = System.currentTimeMillis();
        String srcPath = "/Users/twq/Downloads/01.mp4";
        String destPath = "/Users/twq/Downloads/03.mp4";
        copyFile(srcPath,destPath);
        long end = System.currentTimeMillis();
        System.out.println("復制操作花費的時間為:"+(end - start));
    }
運行結果圖

3.緩沖流-位元組流

(1)實作非文本檔案的復制

點擊查看代碼
/*
    實作非文本檔案的復制
     */
    @Test
    public void test7() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造檔案
            File file = new File("QQ20210927-0.jpg");
            File file1 = new File("QQ20210927-2.jpg");
            //2.造流
            //2.1造節點流
            fis = new FileInputStream(file);
            fos = new FileOutputStream(file1);
            //2.2造緩沖流:處理流是包裝在節點流之上的
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //3.復制的細節:讀取、寫入
            byte[] bytes = new byte[1024];
            int len;
            while((len = bis.read(bytes))!= -1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //4.資源關閉
        //要求:先關閉外層的流,在關閉內層的流
        //關閉外層流的同時,內層流也會自動進行關閉,關于內層流的關閉我們可以省略
//        fos.close();
//        fis.close();
    }
運行結果圖

(2)緩沖流相較于節點流的優勢

點擊查看代碼
/*
    實作非文本檔案的復制
     */
    @Test
    public void test7() {
        long start = System.currentTimeMillis();
        String srcPath = "/Users/twq/Downloads/01.mp4";
        String destPath = "/Users/twq/Downloads/02.mp4";
        copyFileBuffer(srcPath,destPath);
        long end = System.currentTimeMillis();
        System.out.println("緩沖流復制操作花費的時間為:"+(end - start));
    }
    //指定路徑下的檔案復制
    public void copyFileBuffer(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造檔案
            File file = new File(srcPath);
            File file1 = new File(destPath);
            //2.造流
            //2.1造節點流
            fis = new FileInputStream(file);
            fos = new FileOutputStream(file1);
            //2.2造緩沖流:處理流是包裝在節點流之上的
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //3.復制的細節:讀取、寫入
            byte[] bytes = new byte[1024];
            int len;
            while((len = bis.read(bytes))!= -1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //4.資源關閉
        //要求:先關閉外層的流,在關閉內層的流
        //關閉外層流的同時,內層流也會自動進行關閉,關于內層流的關閉我們可以省略
//        fos.close();
//        fis.close();

    }
運行結果圖


緩沖流能提高讀寫速度的原因:內部提供了一個緩沖區

4.緩沖流-字符流

使用BufferReader和BufferWriter實作文本檔案的復制
點擊查看代碼
//使用BufferReader和BufferWriter實作文本檔案的復制
    @Test
    public void test8() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //創建檔案和相應的流
            br = new BufferedReader(new FileReader(new File("hello.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hello3.txt")));
            //讀
            char[] chars = new char[1024];
            int len ;
            while((len = br.read(chars))!= -1){
                bw.write(chars,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

5.圖片的加解密

相同的代碼在運行一次就可以進行解密,主要是因為兩次相同的異或之后可以得到運來的資料

6.轉換流

(1)InputStreamReader和OutputStreamWriter都屬于字符流,作用都是提供位元組流與字符流之間的轉換

①InputStreamReader:將一個位元組的輸入流轉換為字符的輸入流

點擊查看代碼
@Test
    public  void test() {
        InputStreamReader isr = null;
        try {
            FileInputStream fis = new FileInputStream("hello.txt");
//        InputStreamReader isr = new InputStreamReader(fis);//使用系統默認的字符集
            //引數2指明了字符集,具體使用那個字符集,取決于檔案hello.txt保存時使用的字符集
            isr = new InputStreamReader(fis,"UTF-8");
            char[] chars = new char[20];
            int len ;
            while((len = isr.read(chars))!= -1){
                String s = new String(chars, 0, len);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
運行結果圖:

②OutputStreamWriter:將一個字符的輸出流轉為位元組的輸出流
(2)綜合使用InputStreamReader和OutputStreamWriter

點擊查看代碼
@Test
    public  void test1(){
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            File file = new File("hello.txt");
            File file1 = new File("hello4.txt");

            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(file1);

            isr = new InputStreamReader(fis);
            osw = new OutputStreamWriter(fos,"gbk");

            char[] chars = new char[20];
            int len;
            while((len = isr.read(chars)) != -1){
                osw.write(chars,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
運行結果圖

7.資料流

(1)DataInputStream 和 DataOutputStream作用:擁有讀取或寫出基本資料型別的變數或字串
點擊查看代碼
    將檔案中存盤的基本資料型別變數和字串讀取到記憶體中,保存在變數中
    注意點:讀取不同型別的資料要與當初寫入檔案時,保存的資料的順序一致!
     */
    @Test
    public  void test3()  {
        DataInputStream dis = null;
        try {
            dis = new DataInputStream(new FileInputStream("data.txt"));

            String name = dis.readUTF();
            int age = dis.readInt();
            boolean isMale = dis.readBoolean();

            System.out.println("name = " + name);
            System.out.println("age = " + age);
            System.out.println("isMale = " + isMale);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(dis != null){
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public  void test2() {
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream("data.txt"));

            dos.writeUTF("唐昊");
            dos.flush();//重繪操作,將記憶體中的資料寫入檔案
            dos.writeInt(23);
            dos.flush();
            dos.writeBoolean(true);
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(dos != null){
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
運行結果圖


注意:需要先運行向檔案里寫的操作,才能繼續運行讀的操作,并且讀的順序必須與寫的順序一致,否則就會報EOFException例外

8.物件流

(1)ObjectInputStream和ObjectOutputStream:用于存盤和讀取基本資料型別資料或物件的處理流

①序列化與反序列化
點擊查看代碼
/*
    反序列化:將磁盤檔案的物件還原為記憶體中的一個Java物件
    使用ObjectInputStream來實作
     */
    @Test
    public  void test6(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("Object.txt"));
            Object o = ois.readObject();
            String str = (String)o;
            Person p =(Person) ois.readObject();
            System.out.println(str);
            System.out.println(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    /*
    序列化程序:將記憶體中的Java物件保存到磁盤中或通過網路傳輸出去
    使用ObjectOutputStream實作
     */
    @Test
    public  void test5(){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("Object.txt"));
            oos.writeObject(new String("北京天安門"));
            oos.flush();//重繪操作
            //要想一個Java物件時可序列化的,需要滿足相應的要求,見Person.java
            oos.writeObject(new Person("王名",23));
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
②自定義類的序列化與反序列化
Person類代碼如下
點擊查看代碼
package com.Tang.io;

import java.io.Serializable;
/*
Person需要滿足如下的要求,方可序列化
  1.需要實作介面:Seriallizabe
  2.當前類提供一個全域常量:serialVersionUID
  3.除了當前Person類需要實作Serializable介面之外,還必須保證其內部所有屬性也必須是可序列化的(默認情況下:基本資料型別可序列化
 */

public class Person implements Serializable {
    public static final long serialVersionUID = 3476465475L;
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

運行結果圖:


注:代碼的運行得先運行序列化的代碼然后再運行反序列化的代碼

③public static final long serialVersionUID :這個id如果沒有寫的話,就進行的序列化操作(沒有進行反序列化),然后對類進行一些修改之后,在進行反序列化就會報錯,起初定義好序列化id是為了反序列化能根據此id進行無差錯的反序列化

9.RandomAccessFile的使用

(1)實作非文本檔案的復制

點擊查看代碼
/*
    RandomAccessFile的使用
    1.RandomAccessFile直接繼承與Java.lang.object類,實作了DataInput和DataOutput介面
    2.RandomAccessFile既可以作為一個輸入流,又可以作為一個輸出流

     */
    @Test
    public  void test7(){
        RandomAccessFile raf = null;
        RandomAccessFile rw = null;
        try {
            raf = new RandomAccessFile(new File("hello.txt"), "r");
            rw = new RandomAccessFile(new File("hello1.txt"), "rw");
            byte[] bytes = new byte[1024];
            int len;
            while((len = raf.read(bytes))!= -1){
                rw.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(raf != null){
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(rw != null){
                try {
                    rw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

(2)單獨作為輸出流時

點擊查看代碼
/*
    如果RandomAccessFile作為輸出流時,寫出到的檔案如果不存在,則在執行程序中自動創建
    如果寫到的檔案存在,則會對原有檔案從頭開始覆寫
     */
    @Test
    public  void test8(){
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(new File("hello.txt"),"rw");
            raf.write("xyz".getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(raf != null){
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
運行之前hello.txt檔案里的內容如下圖


上述代碼運行之后hello.txt 中的內容如下

(3)實作在檔案指定位置插入資料

點擊查看代碼
@Test
    public  void test8(){
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(new File("hello.txt"),"rw");

            raf.seek(3);//將指標調到角標為3的位置
            //保存指標3后面的所有資料到StringBuilder中
            StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
            byte[] bytes = new byte[20];
            int len;
            while((len = raf.read(bytes))!= -1){
                builder.append(new String(bytes,0,len));
            }
            //調回指標寫入"xyz"
            raf.seek(3);
            raf.write("xyz".getBytes());
            //將StringBuilder 中的資料寫入到檔案中
            raf.write(builder.toString().getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(raf != null){
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
代碼運行前hello.txt中的內容如下


運行代碼在檔案內容角標為3的位置插入xyz之后結果如下

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

標籤:Java

上一篇:在Spring Boot中如何使用@ConfigurationProperties系結配置引數呢?

下一篇:Java面試題(三)--虛擬機

標籤雲
其他(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