字节流,字符流

1.IO流

1.什么是IO流

java中IO操作是指java.io包下的基本内容, 进行输入和输出的操作. 输入也叫作读取数据,输出就是写入数据

2.IO的分类

数据流向:
输入流和输出流
  • 输入流: 把数据从其他设备上读取到内存中的流
  • 输出流:把数据从内存中写入到其他设备的流
格局数据类型:
字节流和字符流
  • 字节流: 以字节为单位,读取数据的流
  • 字符流:以字符为单位,读写数据的流

3.IO的流向说明

	  输入							输出
硬盘-------->内存 				硬盘 <--------- 内存

4.顶级父类

 输入流输出流
字节流 字节输入流InputStream 字节输出流OutputStream
字符流 字符输入流 Reader 字符输出流Writer

2.字节流

1.一切皆为字节

在计算机中,数据是以二进制进行报存,都是一个一个的字节.

2.字节输出流[OutputStream]

java.io.OutputStream抽象类是表示字节输出流所有类的父类

  • public void close():关闭此输出流并释放与此流相关的系统资源
  • public void flush:刷新此输出流并强制任何缓冲的输出字节被写出
  • public void write(byte[] b): 写入一个字节数组到输出流
  • public void write(byte[] b, int off, int len):每次写出从off索引开始,len个字节
  • public abstract void write(int b): 将制定的字节流输出

3.子类FileOutputStream

java.io.FileOutputStream是文件输出流,用于写文件

构造方法:
  • public FileOutputStream(File file):创建文件输出流写入有指定的file对象

  • public FileOutputStream(String name): 创建文件输出流以制定的名称写入文件

    注意: 当你创建一个对象的时候,必须传入一个文件路径.该路径下,如果没有这个文件,会创建该文件,如果有文件,则清空文件的数据.

  • 构造例子:

    public class FileOutputStream throws IOExcepton{
        public static void main(String[] args){
            // 使用file对象创建流对象
            File file = new File("1.txt");
            FileOutputStream fos = new FileOutputStream(file);
            // 使用文件名创建
            FileOutputStream fos = new FileOutputStream("1.txt");
        }
    }
    
写出字节数据
1.写出字节

write(int b)方法,每次可以写出一个字节数据

public class FOSWrite{
    public static void main(String[] args) throws IOException{
        // 使用文件名创建流对象
        FileOutputStream fos = new FileOutputStream("fos.txt");
        // 写出数据
        fos.write(97); // 写出的第一个字节  a
        // 虽然参数为int类型四个字节,但是只会保留一个字节的信息写出
        //关闭资源
        fos.close();
    }
}
2.写出字节数组

write(byte[] b) 每次可以写出数组中的数据

public class FosWrite{
    public static void main(String[] args) throws IOException{
        // 使用文件名创建流对象
        FileOutputStream fos = new FileOutputStream("fos.txt");
        // 字符串转为byte数组
        byte[] bytes = "我是黑马程序员".getBytes();
        // 写出字节数组数据
        fos.write(bytes);
        //关闭资源
        fos.close();
    }
}
3.写出指定长度的字节数组

write(byte[] b, int off,int len): ,每次写出从off索引开始,len个字节

public class FOSWrite{
    public static void main(String[] args) throws IOexception{
        // 使用文件名创建流对象
        FileOutputStream fos = new FileOutputStream("fos.txt");
        //字符串转为数组
        byte[] bytes = "abcde".getBytes();
        // 写出数据
        // 从索引2开始,写三个数据  cde
        fos.write(bytes,2,3);
        //关闭资源
        fos.close();
    }
}
数据追加续写
  • public FileOutputStream(File file, boolean append): 创建文件输出流写入指定的file对象
  • public FileOutputStream(String name, boolean append): 创建文件输出流以指定的名称写入文件
  • true表示追加数据, false清空所有的数据
public class FOSWrite{
    public static void main(String[] args) throws IOexception{
        // 使用文件名创建流对象
        FileOutputStream fos = new FileOutputStream("fos.txt", true);
        //字符串转为数组
        byte[] bytes = "abcde".getBytes();
        // 写出数据
        // 从索引2开始,写三个数据  cde
        fos.write(bytes,2,3);
        //关闭资源
        fos.close();
    }
}
写出换行

windows系统中, 换行是\r\n

public class FOSWrite{
    public static void main(String[] args) throws IOexception{
        // 使用文件名创建对象
        FileOutputStream fos = new FileOutputStream("fos.txt");
        // 定义字节数据
        byte[] bytes = {97,98,99,100,101};
        //遍历数组
        for(int i=0;i<bytes.length;i++){
            // 写出一个字节
            fos.write(bytes[i]);
            fos.write("\r\n".getBytes());
        }
        //关闭资源
        fos.close();
    }
}
  • 回车符 \r 和换行符 \n :
    • 回车符:回到一行的开头(return)。
    • 换行符:下一行(newline)。
  • 系统中的换行:
    • Windows系统里,每行结尾是 回车+换行 ,即 \r\n ;
    • Unix系统里,每行结尾只有 换行 ,即 \n ;
    • Mac系统里,每行结尾是 回车 ,即 \r 。从 Mac OS X开始与Linux统一。

4.字节输入流[InputStream]

java.io.InputStream抽象类是表示字节输入流的父类, 可以读取字节信息到内存中

  • public void close():关闭资源
  • public abstract int read(): 读取数据到下一个字节
  • public int read(byte[] b):从输入流读取一些字节数,存储到字节数组b中

5. FileInputStream

java.io.FileInputStream类是文件输入流,从文件中读取字节

构造方法:
  • FileInputStream(File file):通过打开与实际文件的连接类创建一个FileInputStream, 该文件由文件系统的File对象file命名

  • FileInputStream(String name):通过打开与实际文件来创建一个FileInputStream,该文件由系统中的路径名name命名

  • 注意: 必须传入一个路径, 该路径下未找到文件,会跑出FileNotFoundException

  • 代码如下:

    public class FileInputStreamConstructor{
        public static void main(String[] args) throws IOException {
            // 使用File对象创建流对象
            File file = new File("1.txt");
            FileInputStream fis = new FileInputStream(file);
            
            // 使用文件名创建流对象
            FileInputStream fis = new FileInputStream("1.txt");
        }
    }
    
读取字节数据
1.读取字节

read()方法, 每次可以读取一个字节, 提升为int类型, 读取到文件末尾,返回 -1

public class FISRead{
    public static void main(String[] args) throws IOException{
        // 使用文件名称创建流对象
        FileInputStream fis = new FileInputStream("read.txt");
        // 读取数据,返回一个字节
        int read = fis.read();
        System.out.println((char) read);
        read = fis.read();
        System.out.println((char) read);
        // 关闭资源
        fis.close();
    }
}
// 使用循环进行改进
public class PISRead{
    public static void main(String[] args){
        // 使用文件名创建输入流对象
        FileInputStream fis = new FileInputStream("read.txt");
        // 定义int变量用来记录数字
        int i;
        // 循环读取
        while((i=fis.read())!=-1){
            System.out.println((char)i);
        }
        // 关闭资源
        fis.close();
    }
}
//虽然读取了一个字节,但是会自动提升为int类型。
2.使用字符数组读取

read(byte[] b)每次读取b个长度个字节到数组中, 返回读取到的有效字节个数,读取到末尾使,返回-1

public class FisRead{
    public static void main(String[] args){
        // 使用文件名创建流对象
      FileInputStream FIS = new FileInputStream("read.txt");
      // 定义变量,作为有效个数
      int len;
      // 定义字节数组
      byte[] b = new byte[2]; // 每次读取二哥数据
      while ((len=FIS.read(b))!=-1){
        // 每次读取后,将数组的有效字节部分,进行字符串打印
        System.out.println(new String(b, 0, len));//len每次读取的有效字节个数
      }
      // 关闭资源
      FIS.close();
    }
}

3.字符流

1.字符输入流[reader]

java.io.Reader抽象类是表示用于读取字节流的所有类的超类, 可以读取字符信息到内存中

  • public void close(): 关闭资源
  • public int read():从输入流读取一个字符
  • public int read(char[] chr): 读取一些字符,存储到字符数组chr中

2.FileReader类

java.io.FileReader类是读取字符文件的便利类

构造方法
  • FileReader(File file):创建一个新的FileReader,给定要读取的FIle对象

  • FileReader(String name):创建一个FileReader,给定要读取的文件的名称

  • 构造例子

    public class FileReaderDemo throws IOException{
      public static void main(String[] args){
        // 使用file对象进行创建
        File file = new File("file.txt");
        FileReader fr = new FileReader(file);
        
        // 使用文件名进行创建
        FileReader fr = new FIleReader("file.txt");
      }
    }
    
读取字符数据
1.读取字符

read()方法, 每次读取一个字符的数据,提升为int类型, 读取到文件末尾返回-1

public class FileReaderDemo{
  public static void main(String[] args) throws IOException{
    // 使用文件名创建流对象
    FileReader fr = new FileReader("read.txt");
    // 定义变量, 保存数据
    int b;
    // 循环读取
    while((b=fr.read())=-1){
      System.out.println((char)b);
    }
    //关闭资源
    fr.close();
  }
}
2.使用字符数组读取

read(char[] b)每次读取b个长度的字符到数组中,返回读取到的有效字符个数,读取到末尾返回-1

public class FileReaderDemo{
  public static void main(String[] args){
    // 使用文件名创建流对象
    FileReader fr = new FileReader("read.txt");
    // 定义读取的字符
    int len;
    // 定义字符数组, 妆字符的数据的容器
    char[] chr = new char[2]; 
    while ((len=fr.read(chr))!=-1){
      System.out.println(new String(chr, 0, len));
    }
    //关闭资源
    fr.close();
  }
}

3.字符输出流[Writer]

java.io.Writer抽象类是表示写出字符流的所有类的超类,将指定的字符 信息写出到目的地.

  • public void write(int c):写入单个字符
  • public void write(char[] chr):写入字符数组
  • public void write(char[] chr, int off, int len):每次写入某一部分,off开始索引,len写入的字符个数
  • public void write(String str):写入字符串
  • public void write(String str, int off, int len):写入字符串的某一部分,off开始位置索引,len写入的个数
  • public void flush():刷新该流的缓冲
  • public void close():关闭此流,但是先刷新

4.FileWriter类

构造方法
  • FileWriter(File file):创建一个FileWriter,给定读取的File对象

  • FileWriter(String FileName):创建一个FileWriter对象,给定读取的name

  • 构造例子

    public class FileWriterDemo{
      public static void main(String[] args){
        // 使用file对象创建流对象
        File file = new File("a.txt");
        FileWriter fw = new FileWriter(file);
        
        //使用字符串名字创建流对象
        FileWriter fw = new FileWriter("a.txt");
      }
    }
    
基本写出数据
写出字符

write(int b):每次写出一个字符数据

public class FwWriter{
  public static void main(String[] args){
    //使用字符串名字创建对象
    FileWriter fw = new FileWriter("a.txt");
    //写出数据
    fw.write(99);
    //写出第二个数据
    fw.write(100);
    // 写入文件的时候必许关闭,不然数据保存在缓冲区,未保存到文件中
    fw.close();
  }
}
关闭和刷新
  • flush:刷新缓冲区,流对象可以继续使用
  • close: 先刷新缓冲区,在关闭,流对象不可以使用
public class FwWrter{
  public static void main(String[] args){
    //使用文件名创建流对象
    FileWriter fw = new FileWriter("a.txt");
    //写入数据
    fw.write('刷');
    fw.flush();
    fw.write('新');
    fw.flush();
    // 写出数据
    fw.write('关'); 
    fw.close();
    // 一下代码会报错, 流对象已关闭
    fw.write('闭');
    fw.close();
  }
}

写出其他数据
1.写出字符数组

write(char[] chr)write(char[] chr, int off, int len)

public class FwDemo{
  public static void main(String[] args){
    // 使用文件名创建流对象
    FileWriter fw = new FileWriter("a.txt");
    // 字符串转数组
		char[] chars = "黑马程序员".toCharArray();
    //写出字符数组
    fw.write(chars);
    //写出指定索引,指定长度
    fw.write(chars,1,2); // 马程
    //关闭资源
    fw.close();
  }
}

2.写出字符串

write(String str)和写入指定长度的字符串write(String str, int off, int len)

public class FwDemo{
  public static void main(String[] args){
    //使用文件名创建流对象
    FileWriter fw = new FileWriter("a.txt");
    // 定义字符串
    String str = "我是中国人";
    //写入字符串
    fw.write(str);
    // 写入指定长度的字符串
    fw.write(str,2,3); //中国人
    //关闭资源
    fw.close();
  }
}

3.续写和换行
public class DemoFw{
  public static void main(String[] args){
    // 使用文件名创建流对象
    FileWriter fw = new FileWriter("a.txt", true);
    // 定义字符串
    fw.write("黑马");
    fw.wrute("\r\n");
    fw.write("程序员");
    //关闭资源
    fw.close();
  }
}
posted on 2019-08-15 16:57  liudemeng  阅读(322)  评论(0编辑  收藏  举报