io流讲解

io流

概念
  • 文件:保存数据的地方
  • 文件流:文件在程序中是以流的形式来操作的

FileInputStream—字节输入流
public static void tes1() throws IOException {//字节输入流
    //创建字节输入流对象
    FileInputStream file=new FileInputStream("D:\\QQ\\3054897093\\FileRecv\\数据结构\\图书.txt");
    int result;//接受读取的数据
    /*
    * read()方法每次读取一个字节,没有数据返回-1
    * read(char[] c)每次读取字节数组大小的数据,不够则读取剩余的数据
    */
    while((result=file.read())!=-1){
        System.out.print((char)result);//将读取到的数据转换为字符输出
    }
    byte[] c=new byte[100];
    while((result=file.read(c))!=-1){
        //输出读取到的字节数组,但是由于汉字是三个字节组成的,所以这种读取方法读取汉字会发生乱码
        System.out.println(new String(c,0,result));
    }
    file.close();//关闭流
}
FileOutputStream—字节输出流
public static void tes2() throws IOException {//字节输出流
    //创建字节输出流对象,文件不存在就会指定位置创建一个文件
    FileOutputStream file=new FileOutputStream("D:\\图书.txt",true);
    String s="Hello,word!";
    /*
    * new FileOutputStream(String s),这种方式写入的内容是从头开始的,也就是多次写入是会覆盖之前写入的内容的
    * new FileOutputStream(String s,true),追加写入
    * write()可写入单个字节
    * write(byte(),0,4)也可写入一个字节数组,或写入数组的某一部分,但是下标不能越界
    */
    file.write(s.getBytes());
	//        file.write(s.getBytes(),0,11);
    file.close();//关闭流
}
字节输入输出的综合使用—复制文件
public static void tes3() throws IOException {//综合使用,文件复制,文件可以是文字,图像,音频等
    //创建文件输入流
    FileInputStream fileInputStream=new FileInputStream("D:\\笔记\\java图片\\1.png");
    //创建文件输出流
    FileOutputStream fileOutputStream=new FileOutputStream("D:\\2.png");
    byte[] bytes=new byte[1024];//创建字节数组
    int index;//定义索引,记录每次查询到的字节数量
    while((index=fileInputStream.read(bytes))!=-1){
        fileOutputStream.write(bytes,0,index);
    }
    fileInputStream.close();//关闭流
    fileOutputStream.close();//关闭流
}
FileReader—字符输入流

public static void te8() throws IOException {//字符输入流
    FileReader fileReader=new FileReader("D:\\QQ\\3054897093\\FileRecv\\数据结构\\图书.txt");
    char[] x=new char[150];
    int index;
	//while((index=fileReader.read(x))!=-1){
    //    System.out.print(new String(x,0,index));
    //}
    while((index=fileReader.read())!=-1){
        System.out.print((char)index);
    }
    fileReader.close();
}
FileWriter—字符输出流

此类操作完之后数据并没有立即写入文件,而是在内存(缓存区)中,需要执行flush()方法或者close()方法,将数据写到文件中,如果不执行这两个方法,则文件中仍不会有数据!!!

节点流和处理流
  • 节点流:可以从一个特定的数据源读写数据,如FileReader、FileWriter
  • 处理流:也叫包装流,是“连接”已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,如BufferedReader、BufferedWriter
  • 在包装流中有Reader属性,可以封装一个节点流
BufferedReader和BufferedWriter

是按字符读取数据的,不能读取字节文件,如音频,视频,word文档,pdf等

BufferedReader
public static void te9() throws IOException {//字符处理流
    BufferedReader file=new BufferedReader(new FileReader("D:\\QQ\\3054897093\\FileRecv\\数据结构\\图书.txt"));
    String s;
    while((s=file.readLine())!=null){//整行读取
        System.out.println(s);
    }
    file.close();
}
BufferedWriter
public static void tes4() throws IOException {//字符处理流
    //处理流追加写入还是在字节流创建时设置
    BufferedWriter file=new BufferedWriter(new FileWriter("D:\\图书.txt",true));
    String s="Hello,word!";
    file.write(s);
    file.newLine();
    file.close();
}
BufferedInputStream和BufferedOutputStream
public static void tes5() throws IOException {//字节处理流
    //与字节流使用类似,只是BufferedOutputStream必须要有close()或者flush()操作,否则数据不会写入文件
    BufferedInputStream bi=new BufferedInputStream(new FileInputStream("D:\\壁纸\\19BEADA9C73466F375DB69BC16AE5170.mp4"));
    BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream("D:\\2.mp4"));
    byte[] bytes=new byte[1024];
    int index;
    while((index=bi.read(bytes))!=-1){
        bo.write(bytes,0,index);
    }
    bo.close();
    bi.close();
}
序列化(ObjectOutputStream)与反序列化(ObjectInputStream)
  • 读写顺序要一致
  • 要求序列化或反序列化对象实现Serializable
  • 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
  • 序列化对象时,默认将里面所有的属性都进行序列化,但除了static或transient修饰的成员
  • 序列化对象时,要求里面属性的类型也需要实现序列化接口
  • 序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经实现了序列化
标准输入流与表准输出流
标准输入流(System.in)
  • 是System类中的一个属性,编译类型为inputstream类,运行类型为BInputStream类
  • System类中的 public final static InputStream in = null;
标准输出流(System.out)
  • System类中的 public final static PrintStream out = null;
  • 编译类型和运行类型都为PrintStream类
转换流
public static void tes6() throws IOException {//转换流是字符流
    String filepath="D:\\图书.txt";
    String filepath1="D:\\图书1.txt";
    //可以在传入字节流时指定编码格式
    InputStreamReader isr=new InputStreamReader(new FileInputStream(filepath),"GBK");
    OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(filepath1),"UTF-8");
    char[] bytes=new char[10];
    int index;
    while((index=isr.read(bytes))!=-1){
        osw.write(bytes,0,index);
        System.out.print(new String(bytes,0,index));
    }
    osw.close();
    isr.close();
}
public static void tes7() throws IOException {//转换流是字符流,但仍是字节流,一般会转换为处理流以提高效率
    String filepath="D:\\图书.txt";
    String filepath1="D:\\图书1.txt";
    //可以在传入字节流时指定编码格式
    BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(filepath),"GBK"));
    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath1), StandardCharsets.UTF_8));
    String s;
    while((s=br.readLine())!=null){
        bw.write(s);
        bw.newLine();
    }
    bw.close();
    br.close();
}
  • InputStreamReader:Reader的子类,可以将InputStream(字节流)包装成(转换)Reader(字符流)
  • OutputStreamWriter:Writer的子类,实现将OutputStream(字节流)包装成Writer(字符流)
  • 当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换成字符流
  • 可以在使用时指定编码格式(比如utf-8, gbk , gb2312, IS08859-1等)
PrintStream字节打印流和PintWriter字符打印流
public static void tes8() throws IOException {
    //默认输出位置为控制台
    PrintStream out=System.out;
    out.print("123");
    out.close();
    //修改默认输出位置为指定文件
    System.setOut(new PrintStream("D:\\图书2.txt"));
    System.out.print("hello 苏相甲!");
}
Properties类
public static void tes9() throws IOException {//Properties类---读取文件
    String filepath="src\\suan\\Study\\jdbc.properties";
    Properties properties=new Properties();
    properties.load(new FileReader(filepath));
    properties.list(System.out);
    System.out.println();
    String user = properties.getProperty("user");
    String mima = properties.getProperty("mima");
    System.out.println(user);
    System.out.println(mima);
}
public static void test1() throws IOException {//Properties类---读取文件
    String filepath="src\\suan\\Study\\jdbc1.properties";
    Properties properties=new Properties();
    properties.setProperty("zhanghao","123456");
    properties.setProperty("mima","123456111");
    properties.setProperty("ip","123.456.786.541");
    //创建文件,后面的null值是增加的注解说明,可以添加,若不想添加,填null即可
    properties.store(new FileWriter(filepath),null);
}

tes9输出信息

– listing properties –

mima=qweewqasddsa

user=654456321123

ip=123.321.123.456

654456321123

qweewqasddsa

  • load: 加载配置文件的键值对到Properties对象
  • list:将数据显示到指定设备
  • getProperty(key):根据键获取值
  • setProperty(key,value):设置键值对到Properties对象
  • store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
使用io流时可能遇到的问题
文件访问路径问题

在io流中读取文件,若不加相对路径和绝对路径的话(即只使用文件名来定位文件),则默认在jvm所在的路径查找文件(即项目根目录)。

如,在IDEA中通过以下方式读取abc.txt文件,则该文件必须在根目录下,否则会报java.io.FileNotFoundException异常。

File file = new File("abc.txt");
读写冲突问题

在使用io流时,应避免同时使用输入流和输出流对同一个文件进行操作,因为这样会产生读写冲突,很可能会使文件的内容为空!!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的IO流分为输入流和输出流,其中输入流用于读取数据,输出流用于写入数据。我们可以通过使用Java IO流来读取或写入文件、网络通信、内存缓冲区等。下面分别对Java IO流中的输入流和输出流进行详细讲解。 1. Java输入流 Java中的输入流用于读取数据,分为字节输入流和字符输入流两种。 字节输入流:InputStream InputStream是Java中所有字节输入流的父类,主要提供了读取字节的方法,包括: - int read():读取一个字节的数据并返回,如果已经到达输入流的末尾,则返回-1; - int read(byte[] b):读取b.length个字节的数据,并存储到数组b中,返回实际读取的字节数,如果已经到达输入流的末尾,则返回-1; - int read(byte[] b, int off, int len):读取len个字节的数据,并存储到数组b中从off位置开始的地方,返回实际读取的字节数,如果已经到达输入流的末尾,则返回-1。 字符输入流:Reader Reader是Java中所有字符输入流的父类,主要提供了读取字符的方法,包括: - int read():读取一个字符并返回,如果已经到达输入流的末尾,则返回-1; - int read(char[] c):读取c.length个字符的数据,并存储到数组c中,返回实际读取的字符数,如果已经到达输入流的末尾,则返回-1; - int read(char[] c, int off, int len):读取len个字符的数据,并存储到数组c中从off位置开始的地方,返回实际读取的字符数,如果已经到达输入流的末尾,则返回-1。 2. Java输出流 Java中的输出流用于写入数据,分为字节输出流和字符输出流两种。 字节输出流:OutputStream OutputStreamJava中所有字节输出流的父类,主要提供了写入字节的方法,包括: - void write(int b):写入一个字节的数据; - void write(byte[] b):写入数组b中所有的字节数据; - void write(byte[] b, int off, int len):写入数组b中从off位置开始的len个字节数据; - void flush():刷新该流的缓冲区,确保所有缓存的字节都被写出到目标设备中; - void close():关闭输出流。 字符输出流:Writer Writer是Java中所有字符输出流的父类,主要提供了写入字符的方法,包括: - void write(int c):写入一个字符; - void write(char[] c):写入数组c中所有的字符数据; - void write(char[] c, int off, int len):写入数组c中从off位置开始的len个字符数据; - void flush():刷新该流的缓冲区,确保所有缓存的字符都被写出到目标设备中; - void close():关闭输出流。 以上就是Java IO流的详细讲解,希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值