JAVA--IO流

字节流

——————||InputStream           输入字节流,基类,抽象类
————————||FileInputStream     文件输入字节流,直接读取文本的二进制代码
————————||BufferedInputStream 缓冲输入字节流,实际在内存中维护了8KB的字节
                              数组,提升了传输效率,自己创建字节数组可代替
——————||OutputStream          输出字节流,基类,抽象类
————————||FileOutputStream    文件输出字节流,将需要输出的内容以二进制代码                      
                              形式输出
————————||BufferedOutputStream缓冲输出字节流,实际在内存中维护了一个8KB的
                              字节数组,提升了传输效率,可以自己创建字节数
                              组代替

示例:


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class demo1 {

    public static void main(String[] args) {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        //找到目标文件
        try{
            File file1 = new File("E:\\javafile\\1.png");
            File file2 = new File("E:\\javafile\\3.png");
            //搭建数据通道
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            //创建字节数组进行数据传输
            byte[] buf = new byte[1024];
            int length =  fis.read(buf);
            while( length  != -1 ){
                fos.write(buf, 0, length);
                length =  fis.read(buf);
            }
        }catch( Exception e){
        throw new RuntimeException(e);
        }finally{
            try{
            //关闭数据通道
            fos.close();
            fis.close();
            }catch( Exception e){
                throw new RuntimeException(e);
            }
        }
    }
}

字符流——为了中文的输入与输出

——————||Reader                输入字符流,基类,抽象类
————————||FileReader          文件输入字符流,直接以字符为单位读取文本
————————||BufferedReader     缓冲输入字节流,实际在内存中维护了8KB的字节
                              数组,提升了传输效率,自己创建字节数组可代
                              替。并且提供了readline()方法
——————||Writer                输出字符流,基类,抽象类
————————||FileWriter          文件输出字节流,将需要输出的内容以字符                      
                              形式输出
————————||BufferedWriter      缓冲输出字符流,实际在内存中维护了一个8KB的
                              字节数组,提升了传输效率,可以自己创建字节数
                              组代替。并且提供了newline()方法。

示例:以文本文件代替数据库完成登录注册

    public static void reg() throws IOException{

        System.out.println("请输入用户名:");
        String name = in.next();
        System.out.println("请输入密码");
        String psd = in.next();
        String info = name+" "+psd;

        BufferedReader br = new BufferedReader(new FileReader("E:\\1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\1.txt", true));

        boolean flag = false;
        String line;
        while((line = br.readLine()) != null){
            if( line.equals(info) ){
                flag = true;
            }
        }
        br.close();
        if( !flag ){
            System.out.println("正在注册...");
            bw.write(info);
            bw.newLine();
            bw.close();
            System.out.println("注册成功!!");

        }else{
            System.out.println("该用户已存在!!");
        }
    }

    public static void login() throws IOException{

        System.out.println("请输入用户名:");
        String name = in.next();
        System.out.println("请输入密码");
        String psd = in.next();
        String info = name+" "+psd;

        BufferedReader br = new BufferedReader(new FileReader("E:\\1.txt"));

        boolean flag = false;
        String line;
        while((line = br.readLine()) != null){
            if( line.equals(info) ){
                flag = true;
            }
        }
        br.close();
        if( flag ){
            System.out.println("欢迎"+name+"登录");
        }else{
            System.out.println("用户名或密码有误!!");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值