Java Day8

文件的相關(guān)操作

目的

掌握文件的相關(guān)操作,理解什么是“流”和清楚I/O流。

技術(shù)

1.創(chuàng)建一個(gè)文件
2.向文件寫入數(shù)據(jù)(字節(jié)流)
3.向文件寫入數(shù)據(jù)(字符流)
4.從文件讀取數(shù)據(jù)(字節(jié)流)
5.從文件讀取數(shù)據(jù)(字符流)
6.向文件存一個(gè)對(duì)象
7.從文件中讀取對(duì)象
8.將一個(gè)文件copy到另一個(gè)位置
9.用緩沖流copy文件

技術(shù)實(shí)現(xiàn)

1.創(chuàng)建文件

 String path ="/Users/Administrator/Desktop/Android/firstExperience/java/src/main/java/day8";
        File file = new File(path.concat("/1.txt"));

        // 判斷是否存在
         if (file.exists() == false){
             // 不存在就創(chuàng)建
             try {
                 file.createNewFile();
             }catch (IOException e){
                 System.out.println("IO異常了");
             }

2.向文件寫入數(shù)據(jù)(字節(jié)流)

// 1. 創(chuàng)建文件輸出流對(duì)象
             FileOutputStream fos = new FileOutputStream(file);

             // 2.調(diào)用write方法寫入
             byte[] text = {'1','2','3','4'};
             fos.write(text);

             // 3.操作完畢需要關(guān)閉stream對(duì)象
             fos.close();

3.向文件寫入數(shù)據(jù)(字符流)

 FileWriter fw = new FileWriter(file);
             char[] name = {'安','卓','開','發(fā)'};
             fw.write(name);

             fw.close(); 

4.從文件讀取數(shù)據(jù)(字節(jié)流)

  FileInputStream fis = new FileInputStream(file);

             byte[] name = new byte[8];
            int count = fis.read(name);

             fis.close();
             System.out.println(count+""+  new String(name));

5.從文件讀取數(shù)據(jù)(字符流)

 FileReader fr = new FileReader(file);

             char[] book = new char[4];
             count = fr.read(book);

             fr.close();
             System.out.println(count+""+new String(book));

6.向文件存一個(gè)對(duì)象
創(chuàng)建一個(gè)類并序列化

import java.io.Serializable;

public class Person implements Serializable {
    public  String name ;
    public int age;
}

創(chuàng)建該類的一個(gè)對(duì)象并存入文件

Person xw = new Person();
        xw.name = "小王";
        xw.age = 20;

        OutputStream os = new FileOutputStream(file);

        ObjectOutputStream oos = new ObjectOutputStream(os);

        oos.writeObject(xw);
        oos.close();

7.從文件讀取對(duì)象

InputStream is = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(is);
         Person xw = (Person) ois.readObject();

         System.out.println(xw.name+"  "+xw.age);

         ois.close();

8.將一個(gè)文件copy到另一個(gè)文件

 // 1.源文件的路徑
        String sourcePath = "C:\\Users\\Administrator\\Desktop\\17981969_1359970249177.jpg";

        // 2.目標(biāo)文件路徑
        String desPath = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\java\\src\\main\\java\\day8\\1.jpg";

        // 3. 圖片 字節(jié)
        FileInputStream fis = new FileInputStream(sourcePath);
        FileOutputStream fos = new FileOutputStream(desPath);

        byte[] in = new byte[1024];

//        while (true) {
//            int count = fis.read(in);
//            if(count != -1){
//                // 讀取到內(nèi)容了
//                // 將這一次讀取的內(nèi)容寫入到目標(biāo)文件
//                fos.write(in,0,count);
//            }else {
//                break;
//            }
//        }
        int count = 0;
        while ((count = fis.read(in)) != -1){
            fos.write(in,0,count);
        }


        fis.close();
        fos.close();

9.用緩沖流copy文件

String sourcePath = "C:\\Users\\Administrator\\Desktop\\17981969_1359970249177.jpg";
        String desPath = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\java\\src\\main\\java\\day8\\1.jpg";
        InputStream is = new FileInputStream(sourcePath);
        BufferedInputStream bis = new BufferedInputStream(is);

        OutputStream os = new FileOutputStream(desPath);
        BufferedOutputStream bos = new BufferedOutputStream(os);

        byte[] in = new byte[1024];
        int count = 0;
        while ((count = bis.read(in)) != -1){
            bos.write(in,0,count);
        }
        bis.close();
        bos.close();

技術(shù)運(yùn)用

  • 密碼解鎖demo
    創(chuàng)建一個(gè)類實(shí)現(xiàn)文件操作部分

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOperation {
    public static final String PATH = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\java\\src\\main\\java\\day8\\demo\\pwd.txt";
    String password;

    public static final FileOperation instance = new FileOperation();

    private FileOperation(){
        try {
            load();
        }catch (IOException e){
            System.out.println("io 異常");
        }
    }

    public void load() throws IOException {
        FileInputStream fis = new FileInputStream(PATH);

        byte[] pwd = new byte[4];

        int count = fis.read(pwd);

        if (count == -1){
            password = null;
        }else{
            password = new String(pwd);
        }
        fis.close();
    }

    public void save(String password){
        try {
            FileOutputStream fos = new FileOutputStream(PATH);
            fos.write(password.getBytes());
            fos.close();
        } catch (IOException e){
            System.out.println("io異常");
        }
    }
}

密碼解鎖

import java.util.Scanner;

public class MyClass {
    public static void main(String[] args){
        boolean logined =false;

        //判斷是否已經(jīng)登錄
        String password = FileOperation.instance.password;
        if (password != null){
            logined = true;
        }

        //提示用戶操作
        String alert;
        if (logined) {
            alert = "請(qǐng)輸入密碼";
        } else {
            alert = "請(qǐng)?jiān)O(shè)置密碼";
        }
        System.out.println(alert);

        String first = null;
        int wrongTime = 3;
        while (wrongTime >= 0) {

            //接收用戶輸入
            Scanner scanner = new Scanner(System.in);
            String inputPassword = scanner.next();

            //判斷操作
            if (logined) {
                //已經(jīng)登陸過 直接比較
                if (password.equals(inputPassword)) {
                    System.out.println("解鎖成功");
                    break;
                } else {
                    if (wrongTime == 0)
                    {
                        System.out.println("密碼錯(cuò)誤次數(shù)過多,已退出!");
                        return;
                    }
                    System.out.println("解鎖失敗 請(qǐng)重新輸入");
                    wrongTime--;
                }
            }else{
                //沒有登陸過 在設(shè)置密碼
                //判斷是設(shè)置密碼的第一次還是第二次
                if (first == null){
                    //第一次  保存第一次輸入的密碼
                    first = inputPassword;
                    System.out.println("請(qǐng)確認(rèn)密碼 ");
                }else{
                    //第二次 比較兩次輸入的密碼是否相同
                    if (first.equals(inputPassword)){
                        System.out.println("設(shè)置密碼成功");
                        //保存設(shè)置的密碼
                        FileOperation.instance.save(first);
                        break;
                    }else{
                        System.out.println("兩次密碼不一致 請(qǐng)重新設(shè)置密碼:");
                        first = null;
                        wrongTime--;
                    }
                }


            }
            scanner.nextLine();
        }
    }

}

運(yùn)行結(jié)果
QQ圖片20190814185120.png
QQ圖片20190814185223.png
QQ圖片20190814185304.png

心得

文件的學(xué)習(xí)雖然容易理解,但是內(nèi)容還是有點(diǎn)雜的。感覺說不定哪天我就會(huì)把字符流和字節(jié)流記混了。還需溫故而知新,這樣我才能記得牢。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,644評(píng)論 1 32
  • 一、基礎(chǔ)知識(shí):1、JVM、JRE和JDK的區(qū)別:JVM(Java Virtual Machine):java虛擬機(jī)...
    殺小賊閱讀 2,563評(píng)論 0 4
  • 五、IO流 1、IO流概述 (1)用來處理設(shè)備(硬盤,控制臺(tái),內(nèi)存)間的數(shù)據(jù)。(2)java中對(duì)數(shù)據(jù)的操作都是通過...
    佘大將軍閱讀 585評(píng)論 0 0
  • 1、IO流 1.1、概述 之前學(xué)習(xí)的File類它只能操作文件或文件夾,并不能去操作文件中的數(shù)據(jù)。真正保存數(shù)據(jù)的是文...
    Villain丶Cc閱讀 2,785評(píng)論 0 5
  • ORA-00001: 違反唯一約束條件 (.) 錯(cuò)誤說明:當(dāng)在唯一索引所對(duì)應(yīng)的列上鍵入重復(fù)值時(shí),會(huì)觸發(fā)此異常。 O...
    我想起個(gè)好名字閱讀 5,957評(píng)論 0 9

友情鏈接更多精彩內(nèi)容