io二

字节流,字符流转换流

在整个IO包中,实际上就是分为字节流和字符流,但是除了这两个流之外,还存在了一组字节流-字符流的转换类。

OutputStreamWriter:是Writer的子类,将输出的字符流变为字节流,即:将一个字符流的输出对象变为字节流输出对象。

InputStreamReader:是Reader的子类,将输入的字节流变为字符流,即:将一个字节流的输入对象变为字符流的输入对象。

public static void main(String[] args) throws Exception {
        //向本工程中a.txt 写数据
        String str ="aaaaaaa";
        File file = new File("a.txt");
        //字节输出流
        OutputStream out = new FileOutputStream(file);
        /*byte[] b=str.getBytes();
        out.write(b);*/
        //把字节流转成字符流,使用字符流中的方法
        Writer out1 = new OutputStreamWriter(out);
        out1.write(str);
        System.out.println("写入完成!");
        out1.flush();
        //把数据从a.txt中读取
        InputStream in = new FileInputStream(file);
        int i=0;
        byte[] bytes=new byte[1024];
        while((i=in.read(bytes))!=-1){
            System.out.println(new String(bytes,0,i));
        }
        
        in.close();
        //out.close();
//        out1.flush();//刷新流
        out1.close();//调用close方法执行时,会先执行flush(),再关闭流
    }

打印流 PrintWriter ,PrintStream 也是包装流

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

public static void main(String[] args) throws Exception {
        //数据
        String str ="abcdef";
//     PrintStream 字节输出流
        FileOutputStream out = new FileOutputStream(new File("b.txt"),true);//true追加
//        PrintStream p1 = new PrintStream(new File("b.txt"));
        PrintStream p1 = new PrintStream(out,true);//true自动刷新流,对print println都有效
        p1.println(str);
//       PrintWriter字符输出流
        FileWriter out1 = new FileWriter(new File("c.txt"));
        PrintWriter pw = new PrintWriter(out1,true);//true自动刷新流,只对println方法有效
        pw.println(str); 
        pw.print(str); //无效
//        pw.flush();
        
//        p1.close();
    }

System类的常量 (了解)

System表示系统类,此类在之前讲解JAVA常用类库的时候就已经为读者介绍过了,实际上在java中System类也对IO给予了一定的支持。

No.

System类的常量

描述

1

public static final PrintStream out

对应系统标准输出,一般是显示器

2

public static final PrintStream.err

错误信息输出

3

public static final InputStream in

对应着标准输入,一般是键盘

public static void main(String[] args) {

                  System.out.println("1111");

                  System.err.println("123");//展示异常信息

                  new Scanner(System.in);

         }

包装流,缓冲流

包装流最终包装其他流的目的:是使用包装流中的方法,使对文件的数据操作更方法!!!

 BufferedReader 

BufferedReader是从缓冲区之中读取内容,所有的输入的字节数据都将放在缓冲区之中。

BufferedReader(Reader in)
          创建一个使用默认大小输入缓冲区的缓冲字符输入流。

String

readLine()    读取一个文本行。

public static void main(String[] args) throws Exception {
        //包装流,简化读文件操作
        FileReader r = new FileReader("b.txt");
        BufferedReader br = new BufferedReader(r);
        String str = br.readLine();
        System.out.println(str);
        //关闭流
        br.close();
        r.close();
    }

文件编码

public static void main(String[] args) throws Exception {
        //D:\my.txt写数据
        PrintStream ps = new PrintStream(new FileOutputStream("d:\my.txt"),true,"gbk");
        ps.print("中华人民共和国");
        ps.close();
        //gb2312 gbk
        //读取
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\my.txt"), "GB2312"));
        System.out.println(br.readLine());
        br.close();

BufferedWriter

public static void main(String[] args) throws Exception {
     BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
     bw.write("abc");
     bw.newLine();//增加换行符
     bw.write("def");
     bw.newLine();
     bw.write("ghi");
     
     bw.close();

    }

读取整个文件数据

public static void main(String[] args) throws Exception {
        // 读取文件中的所有数据
        BufferedReader br = new BufferedReader(new FileReader("b.txt"));
//        StringBuffer sf = new StringBuffer();
        String line = "";//每行
        String str="";//整个文档数据
        while((line=br.readLine())!=null){
//            sf.append(line);
            str += line;
        }
        System.out.println(str);
//        System.out.println(sf.toString());
        br.close();
    

    }

对象序列化

java.io.NotSerializableException

对象不可序列化异常

public class Car implements Serializable {
//Serializable表示可以序列化
}
public class Serializable_test {

public static void main(String[] args) throws Exception {
//序列化
Car car1 = new Car("zzzz",333);
System.out.println(car1);

FileOutputStream out = new FileOutputStream("car_test");
ObjectOutputStream obj = new ObjectOutputStream(out);
obj.writeObject(car1);
out.close();

//反序列化
ObjectInputStream in = new ObjectInputStream(new FileInputStream("car_test"));
Car car2 = (Car) in.readObject();
System.out.println(car2);
in.close();
}



}

transient

当使用Serializable接口实现序列化操作时,如果一个对象中的某个属性不希望被序列化的话,则可以使用transient关键字进行声明

 

 

原文地址:https://www.cnblogs.com/taozizainali/p/10881571.html