java-IO流

IO流概述

程序需要实现与设备和不同介质之间的数据传输,例如:键盘录入、读取电脑文件等,Java将这种通过不同输入输出设备(键盘,显示器,网络)等之间的数据传输抽象表述为“流”

IO流分类

按照操作的数据不同,可以分为:

字节流:字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
字符流:字符流只能操作纯字符数据,比较方便。

按照流向分,又可以分为

输入流和输出流

字节流

字节输出流

字节输出流OutputStream

OutputStream是抽象类,是所有字节输出流类的超类。操作的数据都是字节,该类定义了字节输出流的基本共性功能方法。

  • void close() 关闭此输出流并释放与此流有关的所有系统资源
  • void flush() 刷新输出流并强制写出所有缓冲的输出字节
  • void write(byte[] b) 将 b.length个字节从指定的byte数组写入此输出流
  • void write(byte[] b, int off, int len) 将指定byte数组中从偏移量Off开始的len个字节写入此输出流
  • abstract void write(int b) 将指定的字节写入此输出流
FileOutputStream类

OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。
FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流

FileOutputStream的构造方法,构造方法可以接受File、String类型的数据,append为是否追加数据

  • FileOutputStream(File file) 创建一个指定File对象表示的文件中写入数据的文件输出流
  • FileOutputStream(File file, boolean append) 创建一个指定File对象表示的文件中写入数据的文件输出流
  • FileOutputStream(String name) 创建一个向具有指定名称的文件中写入数据的文件输出流
  • FileOutputStream(String name, boolean append) 创建一个向具有指定name的文件中写入数据的文件输出流

输出的方法,close()方法可以关闭流,write方法可以输出数据

  • void close() 关闭此文件输出流并释放与此流有关的所有系统资源
  • void write(byte[] b) 将b.length个字节从指定byte数组写入此文件输出流中
  • void write(byte[] b, int off, int len) 将指定byte数组中从偏移量off开始的len个字节写入此文件输出流
  • void write(int b) 将指定字节写入此文件输出流

示例:

public class Test {
    public static void main(String[] args) throws Exception {
        try {
            // File file = new File("1.txt");
            // FileOutputStream out3 = new FileOutputStream(file);
            FileOutputStream out = new FileOutputStream("1.txt");// 相对路径

            // 输出
            // write(int a):输出一个字节
            out.write(97);// 输出:a

            byte[] byteArray = {97, 98, 99, 100};
            // write(byte[] b, int off,int len):输出一个字节数组的一部分
            out.write(byteArray, 2, 2);
            // 输出byteArray数组的:cd
            
            out.close();// 关闭流

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//acd
给文件中续写和换行

如果直接使用构造方法:new FileOutputStream(file)创建输出流对象,向文件中写入数据,会覆盖原有文件的内容,那么我们想在原有的文件中续写内容怎么办呢?可以使用另外的构造方法

  • FileOutputStream(File file, boolean append) 创建一个指定File对象表示的文件中写入数据的文件输出流
  • FileOutputStream(String name, boolean append) 创建一个向具有指定name的文件中写入数据的文件输出流
public class Test {
    public static void main(String[] args) throws Exception {
        try (FileOutputStream out = new FileOutputStream("1.txt", true);) {
            out.write("你好".getBytes());
            out.write("\r\n".getBytes());//换行符
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

字节输入流

字节输入流InputStream

通过字节输出流,我们可以把内存中的数据写出到文件中,那如何想把文件中的数据读到内存中,可以通过InputStream可以实现。
InputStream是抽象类,是所有字节输入流类的超类,该类定义了字节输入流的基本共性功能方法。

  • void close() 关闭此输入流并释放与该流关联的所有系统资源
  • abstract int read() 从输入流中读取数据的下一个字节
  • int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
  • int read(byte[] b,int off, int len) 将输入流中最多len个数据字节读入byte数组

int read():读取一个字节并返回,没有字节返回-1.
int read(byte[]):读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。

FileInputStream类

InputStream有很多子类,其中子类FileInputStream可用来读取文件内容。
FileInputStream 从文件系统中的某个文件中获得输入字节。
FileOutputStream 用于写入诸如图像数据之类的原始字节的流。

构造方法

  • FileInputStream(File file) 通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的File对象file来指定
  • FileInputStream(String name) 通过打开一个到实际文件的连接来创建一个FileInputStream,该文件系统中的路劲名name指定
FileInputStream类读取数据read方法

在读取文件中的数据时,调用read方法,实现从文件中读取数据

int read() 从此输入流中读取一个数据字节

public class Test {
    public static void main(String[] args) throws Exception {
        //1.txt文件中存有:acd
        FileInputStream in = new FileInputStream("1.txt");
        // 直接使用File文件对象
        // File file = new File("demo15.txt");
        // FileInputStream in2 = new FileInputStream(file);

        // 写法一:一次读一个字节
        // int n = in.read();
        // 	while(n != -1){
        // 		System.out.println("读取的字节:" + n);
        // 		System.out.println("转换为字符:" + (char)n);
        // 		n = in.read();
        // }

        // 写法二:将赋值语句放到while判断条件里面
        int n = 0;
        while ((n = in.read()) != -1) {
            System.out.println("读取的字符:" + (char) n);
        }
        // 释放资源
        in.close();
    }
}

结果:

读取的字符:a
读取的字符:c
读取的字符:d
FileInputStream读取数据read(byte[])方法

在读取文件中的数据时,调用read方法,每次只能读取一个,需要频繁的操作文件,效率非常低,于是我们可以定义数组作为临时的存储容器,这时可以调用重载的read方法,一次可以读取多个字符。

public class Test {
    public static void main(String[] args) throws Exception {
    	//1.txt文件中存有:acd
        InputStream is = new FileInputStream(new File("1.txt"));
        byte[] bytes = new byte[3];
        //写法一:
//		int count = is.read(bytes);
//		while(count!=-1){
//			//读取到多少个数据,那么就取多少个
//			for(int i = 0; i<count; i++){
//				System.out.println(bytes[i]);
//			}
//			count = is.read(bytes);
//		}

        //写法二
        int count = -1;
        while((count = is.read(bytes))!=-1){
            for(int i = 0; i<count; i++){
                System.out.println(bytes[i]);
            }
        }
        is.close();
        System.out.println("程序结束");
    }
}

结果:

97
99
100

字符流

字符输入流Reader

API中给我们提供了读取相应字符的功能流对象,Reader,读取字符流的抽象超类

  • int read(char[] cbuf) 将字符读入数组
  • abstract int read(char[] cbuf, int off, int len) 将字符读入数组的某一部分

read():读取单个字符并返回
read(char[]):将数据读取到数组中,并返回读取的个数。

FileReader类

要读取字符数据,使用 FileReader。
构造方法:

  • FileReader(File file) 在给定从中读取数据的File的情况下创建一个新FileReader
  • FileReader(String fileName) 在给定从中读取数据的文件名的情况下创建一个新FileReader

注意:FileReader类没有特有的方法,所有方法均继承自父类

public class Test {
    public static void main(String[] args) throws Exception {
        // 给文件中写中文
        writeCNText();
        // 读取文件中的中文
        readCNText();
    }
    // 读取中文
    public static void readCNText() throws IOException {
        FileReader fr = new FileReader("1.txt");
        int ch = 0;
        while ((ch = fr.read()) != -1) {
            // 输出的字符对应的编码值
            System.out.println(ch);
            // 输出字符本身
            System.out.println((char) ch);
        }
    }
    // 写中文
    public static void writeCNText() throws IOException {
        FileOutputStream fos = new FileOutputStream("2.txt");
        fos.write("你好".getBytes());
        fos.close();
    }
}

字符输出流Writer

构造方法:

  • protected Writer() 创建一个新的字符流 writer,其关键部分将同步 writer 自身。
  • protected Writer(Object lock) 创建一个新的字符流 writer,其关键部分将同步给定的对象。

方法:

  • Writer append(char c) 将指定字符添加到此 writer。
  • void write(char[] cbuf) 写入字符数组。
  • abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
  • void write(int c) 写入单个字符。
  • void write(String str) 写入字符串。
  • void write(String str, int off, int len) 写入字符串的某一部分。
FileWriter类

要写入字符流,使用 FileWriter。
构造方法:

  • FileWriter(File file) 根据给定的 File 对象构造一个 FileWriter 对象
  • FileWriter(String fileName) 根据给定的文件名构造一个 FileWriter 对象
  • FileWriter(String fileName, boolean append) 根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象
public class Test {
    public static void main(String[] args) throws Exception {
        //writer
        FileWriter fw = new FileWriter("1.txt");
        fw.write("你好");
        //刷新
        fw.flush();

        char[] chars = {'你','好'};
        fw.write(chars);
//        fw.flush();

        //关闭流
        fw.close();//关闭后会自动刷新
    }
}
flush()和close()的区别
  • abstract void close() 关闭此流,但要先刷新它
  • abstract void flush() 刷新该流的缓冲

flush():将流中的缓冲区缓冲的数据刷新到目的地中,刷新后,流还可以继续使用。
close():关闭资源,但在关闭前会将缓冲区中的数据先刷新到目的地,否则丢失数据,关闭流后,流不可以再继续使用。如果写入数据多,一定要一边写一边刷新(flush),最后一次可以不刷新,由close完成刷新并关闭。

转换流

有时候字节流和字符流之间需要进转换,此时就需要用到转换流

OutputStreamWriter

OutputStreamWriter是Writer的子类,可以将一个字节输出流包装成字符输出流,方便直接写入字符,通过构造方法可以指定字符的编码格式

public static void writeCN() throws Exception {
	// 创建与文件关联的字节输出流对象
	FileOutputStream fos = new FileOutputStream("1.txt");
	// 创建可以把字节转成字符的转换流对象,并指定编码
	OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
	// 调用转换流,把文字写出去,其实是写到转换流的缓冲区中
	osw.write("你好");// 写入缓冲区。
	osw.close();
}

InputStreamReader

InputStreamReader 是Reader的子类,可以将一个字节输入流包装成字符输入流,方便直接读取字符,通过构造方法可以指定字符编码格式

在这里插入代码片public static void readCN() throws IOException {
	// 创建读取文件的字节流对象
	InputStream in = new FileInputStream("1.txt");
	// 创建转换流对象
	// InputStreamReader isr = new
	// InputStreamReader(in);这样创建对象,会用本地默认码表读取,将会发生错误解码的错误
	InputStreamReader isr = new InputStreamReader(in, "utf-8");
	// 使用转换流去读字节流中的字节
	int ch = 0;
	while ((ch = isr.read()) != -1) {
		System.out.println((char) ch);
	}
	// 关闭流
	isr.close();
}

缓冲流

在使用流读取数据量大的文件时,读取的速度会很慢,影响程序的效率,想提高效率时,就会使用缓冲流

缓冲流分类

根据流的分类分类字节缓冲流与字符缓冲流

字节缓冲流

字节缓冲流根据流的方向,有2个:

1.写入数据到流中,字节缓冲输出流 BufferedOutputStream
2.读取流中的数据,字节缓冲输入流 BufferedInputStream

它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度

字节缓冲输出流BufferedOutputStream

构造方法

BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。

public class Test {
    public static void main(String[] args) throws Exception {
        // 写数据到文件的方法
        write();
    }
    private static void write() throws IOException {
        // 创建基本的字节输出流
        FileOutputStream fos = new FileOutputStream("1.txt");
        // 使用高效的流,把基本的流进行封装,实现速度的提升
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 2,写数据
        bos.write("hello".getBytes());
        // 3,关闭流
        bos.close();
        fos.close();
    }
}

字节缓冲输入流BufferedInputStream

构造方法

BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
BufferedInputStream(InputStream in, int size) 创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

public class Test {
    public static void main(String[] args) throws Exception {
        // 1. 创建缓冲流对象
        FileInputStream fis = new FileInputStream("1.txt");
        // 把基本的流包装成高效的流
        BufferedInputStream bis = new BufferedInputStream(fis);
        // 2. 读数据
        int ch = -1;
        while ((ch = bis.read()) != -1) {
            // 打印
            System.out.print((char) ch);
        }
        bis.close();
        fis.close();
    }
}
缓冲流效率的对比

使用四种操作流的方式来拷贝数据,查看其效率
字节流四种读写方式效率的对比:

基本字节流:
FileOutputStream:
FileInputStream:
1.一次读写一个字节: m1(); 54986 毫秒
2.一次读写一个字节数组: m2(); 97 毫秒

缓冲字节流:
BufferedOutputStream:
BufferedInputStream:
1.一次读写一个字节: m3(); 666 毫秒
2.一次读写一个字节数组: m4(); 38 毫秒

public class Test {
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        // method1();//54986 毫秒
        // method2();//97 毫秒
        // method3();//666毫秒
        method4();// 38 毫秒
        long end = System.currentTimeMillis();
        System.out.println("执行时间:" + (end - start) + " 毫秒");
    }

    // 基本流:一次读写一个字节
    private static void m1() throws IOException {
        // 1.输入流:
        FileInputStream in = new FileInputStream("1.txt");
        // 2.输出流
        FileOutputStream out = new FileOutputStream("copy1.txt");
        // 一次读写一个字节
        int n = 0;
        while ((n = in.read()) != -1) {
            out.write(n);
        }
        // 释放资源
        in.close();
        out.close();
    }

    // 基本流:一次读写一个字节数组
    private static void method2() throws IOException {
        // 1.输入流:
        FileInputStream in = new FileInputStream("1.txt");
        // 2.输出流
        FileOutputStream out = new FileOutputStream("copy2.txt");
        // 一次读写一个字节数组
        byte[] byteArray = new byte[1024];
        int n = 0;
        while ((n = in.read(byteArray)) != -1) {
            out.write(byteArray, 0, n);
        }
        in.close();
        out.close();
    }

    // 缓冲流:一次读写一个字节
    private static void method3() throws IOException {
        // 1.缓冲输入流
        BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream("1.txt"));
        // 2.缓冲输出流
        BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream("copy3.txt"));
        // 一次读写一个字节
        int n = 0;
        while ((n = bufIn.read()) != -1) {
            bufOut.write(n);
        }
        bufIn.close();
        bufOut.close();
    }

    // 缓冲流:一次读写一个字节数组
    private static void method4() throws IOException {
        // 1.缓冲输入流
        BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream("1.txt"));
        // 2.缓冲输出流
        BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream("copy4.txt"));
        // 一次读写一个字节数组
        byte[] byteArray = new byte[1024];
        int n = 0;
        while ((n = bufIn.read(byteArray)) != -1) {
            bufOut.write(byteArray, 0, n);
        }
        bufIn.close();
        bufOut.close();
    }
}

字符缓冲流

字符缓冲输入流 BufferedReader
字符缓冲输出流 BufferedWriter
完成文本数据的高效的写入与读取的操作

字符缓冲输出流BufferedWriter

将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入

BufferedWriter新增(独有)的方法

  • void flush() 刷新该流的缓冲。
  • void newLine() 写入一个行分隔符。
public class Test {
    public static void main(String[] args) throws Exception {
        // 基本字符输出流
        FileWriter fileOut = new FileWriter("1.txt");
        // 把基本的流进行包装
        BufferedWriter out = new BufferedWriter(fileOut);
        // 2,写数据
        for (int i = 0; i < 5; i++) {
            out.write("hello");
            out.newLine();
        }
        // 3,关闭流
        out.close();
    }
}
字符缓冲输入流BufferedReader

从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。

String readLine() 读取一个文本行。

public class Test {
    public static void main(String[] args) throws Exception {
        // 1,创建流
        BufferedReader in = new BufferedReader(new FileReader("1.txt"));
        // 2,读数据
        // 一次一个字符
        // 一次一个字符数组
        // 一次读取文本中一行的字符串内容
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        // 3,关闭流
        in.close();
    }
}
使用字符缓冲流拷贝文件
字符流复制文件的五种方式:

基本流:FileWriter、FileReader
1).一次读写一个字符; method1()
2).一次读写一个字符数组; method2()

缓冲流:BufferedWriter、BufferedReader
1).一次读写一个字符; method3()
2).一次读写一个字符数组; method4()
3).一次读写一行数据: method5()

public class Test {
    public static void main(String[] args) throws Exception {
        // m1();
        // m2();
        // m3();
        // m4();
        m5();
        System.out.println("复制完毕!");
    }

    // 基本流:一次读写一个字符
    private static void m1() throws IOException {
        // 1.输入流
        FileReader in = new FileReader("1.txt");
        // 2.输出流
        FileWriter out = new FileWriter("copy1.txt");
        // 一次读写一个字符
        int n = 0;
        while ((n = in.read()) != -1) {
            out.write(n);
        }
        in.close();
        out.close();
    }

    // 基本流:一次读写一个字符数组
    private static void m2() throws IOException {
        // 1.输入流
        FileReader in = new FileReader("1.txt");
        // 2.输出流
        FileWriter out = new FileWriter("copy2.txt");
        // 一次读写一个字符数组
        char[] charArray = new char[1024];
        int n = 0;
        while ((n = in.read(charArray)) != -1) {
            out.write(charArray, 0, n);
        }
        in.close();
        out.close();
    }

    // 缓冲流:一次读写一个字符
    private static void m3() throws IOException {
        // 1.输入流:
        BufferedReader in = new BufferedReader(new FileReader("1.txt"));
        // 2.输出流:
        BufferedWriter out = new BufferedWriter(new FileWriter("copy3.txt"));
        // 一次读写一个字符
        int n = 0;
        while ((n = in.read()) != -1) {
            out.write(n);
        }
        in.close();
        out.close();
    }

    // 缓冲流:一次读写一个 字符数组
    private static void m4() throws IOException {
        // 1.输入流
        BufferedReader in = new BufferedReader(new FileReader("1.txt"));
        // 2.输出流
        BufferedWriter out = new BufferedWriter(new FileWriter("copy4.txt"));
        // 一次读写一个字符数组
        char[] charArray = new char[1024];
        int n = 0;
        while ((n = in.read(charArray)) != -1) {
            out.write(charArray, 0, n);
        }
        in.close();
        out.close();
    }

    // 缓冲流:一次读写一行
    private static void m5() throws IOException {
        // 1.输入流
        BufferedReader in = new BufferedReader(new FileReader("1.txt"));
        // 2.输出流
        BufferedWriter out = new BufferedWriter(new FileWriter("copy5.txt"));
        // 一次读写一行
        String row = null;
        while ((row = in.readLine()) != null) {
            out.write(row);
            out.newLine();
        }
        in.close();
        out.close();
    }
}

其它流

对象序列化流

序列化ObjectOutputStream

是指将一个"对象(包含属性值)"存储到一个文件中,或者通过网络进行传输
类介绍:ObjectOutputStream

构造方法

ObjectOutputStream(OutputStream out)

序列化的方法

void writeObject(Object obj) :将指定的对象写入 ObjectOutputStream

注意:当某个对象需要被"序列化"时,此类必须实现:Serializable(接口)

在Serializable接口中,没有任何方法,这种接口叫:标记接口;它类似于一个标记,某个类如果实现了这样的接口,表示这个类就具有了某种权限(功能)

反序列化ObjectInputStream

是指将一个文本文件转成一个Java对象

类介绍:ObjectInputStream

构造方法

ObjectInputStream(InputStream in)

反序列化的方法

Object readObject() :从 ObjectInputStream 读取对象。

“序列号”(serialVersionUID)

当一个类实现了Serializable后,系统会自动为这个类添加一个属性:它表示"序列号"的信息
这个"序列号"会被写入到文件中;
当我们修改这个类的结构时,系统会自动更改此"序列号";所以这就导致了"类中的序列号"和"文件中的序列号"不匹配的问题;
建议:每个需要被"序列化"的类,都要显示的指定"序列号",并且由程序员自己维护序列号的信息

public class Test {
    public static void main(String[] args) throws Exception {
        // 序列化
        Student stu1 = new Student("张三", 20);
        // 实例化一个序列化流
        ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("1.txt"));
        objOut.writeObject(stu1);
        objOut.close();
        System.out.println("写入完毕!");

        // 反序列化
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("1.txt"));
        Object obj = in.readObject();// 会在内存中再产生一个Student对象
        Student stu = (Student) obj;
        System.out.println("反序列化:name = " + stu.getName() + ", age = " + stu.getAge());
    }
}

class Student implements Serializable {
    private String name;
    private int age;

    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

打印流

输出流在通过write()方法写数据时,只能输出字节或字符串类型的数据,如果希望输出其他类型的数据,例如输出一个基本数据类型int的值9,此时需要将数据线转换成字符串再上传,为此,Java提供了PrintStream

PrintStream打印流是输出信息最方便的类,主要包含字节打印流(PrintStream) 和字符打印流(PrintWriter) . 打印流提供了非常方便的打印功能,可以打印任何的数据类型,例如: 小数、整数、字符串等等。

public class Test {
    public static void main(String[] args) throws Exception {
        PrintWriter out = new PrintWriter("1.txt");
        out.write("你好");
        out.write("\r\n");
        out.write("Hello");
        out.write("\r\n");
        out.write("World");
        out.close();
    }
}

如何选择使用流

第一步:明确操作的数据类型,是文本还是其它二进制文件?
文本:Reader、Writer
其它二进制:InputStream、OutputStream

第二步:先明确要操作类型输入还是输出
源:InputStream Reader
目的:OutputStream Writer

第三步:明确是否需要额外的功能
         是否需要高效率?
          BufferedInputStream
          BufferedOuputStream

          BufferedWriter
          BufferedReader

总结

字节流

字节输入流 InputStream
FileInputStream 操作文件的字节输入流
BufferedInputStream高效的字节输入流

字节输出流 OutputStream
FileOutputStream 操作文件的字节输出流
BufferedOutputStream 高效的字节输出流

字符流

字符输入流 Reader
FileReader 操作文件的字符输入流
BufferedReader 高效的字符输入流
InputStreamReader 输入操作的转换流(把字节流封装成字符流)

字符输出流 Writer
FileWriter 操作文件的字符输出流
BufferedWriter 高效的字符输出流
OutputStreamWriter 输出操作的转换流(把字节流封装成字符流)

方法:

读数据方法:

read() 一次读一个字节或字符的方法
read(byte[] char[]) 一次读一个数组数据的方法
readLine() 一次读一行字符串的方法(BufferedReader类特有方法)
readObject() 从流中读取对象(ObjectInputStream特有方法)

写数据方法:

write(int) 一次写一个字节或字符到文件中
write(byte[] char[]) 一次写一个数组数据到文件中
write(String) 一次写一个字符串内容到文件中
writeObject(Object ) 写对象到流中(ObjectOutputStream类特有方法)
newLine() 写一个换行符号(BufferedWriter类特有方法)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值