输入输出流,图案解锁demo

目的:
1.掌握输入输出流的概念
2.学会运用输入输出流有关函数
3.现学现用,提高知识的掌握程度
概念部分:
读取文件的内容
I/O 流
流的方向:参考的是自己的内存空间
输出流:从内存空间将数据写到外部设备(磁盘/硬盘/光盘)
输入流:将外部数据写到内存中
流:统一管理数据的写入和写出
输入流:开发者只需要将内存里面的数据写到流里面
输入流:或者从流里面读取数据
输入流:OutputStream 字节流 Writer字符流
输出流:InputStream字节流 Reader字符流
I/O流对象 不属于内存对象 需要自己关闭、
文件读写:

创建文件输出流对象
        FileOutputStream fos = new FileOutputStream(file);

        // 2.调用write方法写入
        byte[] text = {'1','2','3','4'};
        fos.write(text);

        // 3.操作完毕需要关闭stream对象
        fos.close();

        // 向文件写入数据-字符流
        FileWriter fw = new FileWriter(file);

        char[] name = {'安','卓','开','发'};
        fw.write(name);

        fw.close();

        //读取内容
        FileInputStream fis = new FileInputStream(file);

        byte[] name1 = new byte[12];
        int count = fis.read(name1);

        fis.close();

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

        FileReader fr = new FileReader(file);

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

        fr.close();

图片文件复制:

 String sourcePath="C:/Users/DELL/Desktop/1.jpg.png";
        String desPath = "C:\\Users\\DELL\\AndroidStudioProjects\\JavaClass1\\javaday1\\src\\main\\java\\day8.1.jpg.png";
        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();

图案解锁demo:

public class FileOperation {
    public static final String PATH = "/Users/pengxiaodong/Desktop/day1/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异常");
        }
    }
}

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

            //判断是否已经登录
            String password = FileOperation.instance.password;
            if (password != null) {
                logined = true;
            }

            //提示用户操作
            String alert;
            if (logined) {
                alert = "请输入密码";
            } else {
                alert = "请设置密码";
            }
            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) {
                    //已经登陆过 直接比较
                    if (password.equals(inputPassword)) {
                        System.out.println("解锁成功");
                        break;
                    } else {
                        System.out.println("解锁失败 请重新输入");
                        wrongTime--;
                    }
                } else {
                    //没有登陆过 在设置密码
                    //判断是设置密码的第一次还是第二次
                    if (first == null) {
                        //第一次  保存第一次输入的密码
                        first = inputPassword;
                        System.out.println("请确认密码 ");
                    } else {
                        //第二次 比较两次输入的密码是否相同
                        if (first.equals(inputPassword)) {
                            System.out.println("设置密码成功");
                            //保存设置的密码
                            FileOperation.instance.save(firt);
                            break;
                        } else {
                            System.out.println("两次密码不一致 请重新设置密码:");
                            firt = null;
                            wrongTime--;
                        }
                    }


                }
                scanner.nextLine();
            }
        }

感受:听着输入输出流的概念,和文件读写等函数的概念和应用,感觉知识滔滔不绝的涌入脑海,但是装下了多少就不清楚了。记不住全部,感觉要勤看勤记。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容