zoukankan      html  css  js  c++  java
  • 标准输入流(System in)和输出流PrintStream(System.out),Scanner扫描文件;序列化,序列化版本,transient,序列化对象,反序列化对象。输出流:DataInputStream/DataOutputStream

    package sc.IO;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    
    
    /*标准输入流
     * 源数据源是标准输入设备(键盘、鼠标、触摸屏)等输入设备。
     * 在java中用System.in 得到一个InputStream字节输入流。
         * */
    
    public class System01 {
    public static void main(String[] args) throws IOException {
        //需求:在控制台输入1个字母,然后原样输出
        //InputStream in=System.in;
        //int c=in.read();
        //System.out.println((char)c);
        //控制台输入字符进来,转换为字节流,因为是字母,所以再把得到编码强转为char
        
        
        
        // 需求:输入一句话,然原样输出
        InputStream in1 = System.in;//控台输入,电脑读取的是字节流
        byte [] buf=new byte[1024];//创建一个字节流数组
        int len;
        len=in1.read(buf);//把in1字节流放入buf数组,反回len字节数组长度
        String str=new String(buf,0,len);//没有写解码类型,平台默认编码
        System.out.println(Arrays.toString(buf));
        //[-76, -78, -57, -80, -61, -9, -44, -62, -71, -30, 13, 10, 0, 0,...]
        //13 和 10 是  回车和换行;    buf中自动包含回车和换行,所以len长度也不可靠
        System.out.println(str);//
        //输出结果:床前明月光
        
        
    
    
    
    
        // 需求:从控制台高效读取一行数据。把一首诗写入文件。
        InputStream in2 = System.in;//控台输入字节流
        InputStreamReader reader=new InputStreamReader(in2, "GBK");//把字节流通过GBK解码为字符流
        BufferedReader br=new BufferedReader(reader);//读取整行字符
        File file =new File("D:\gameproject\a.txt");//创建文件
        FileWriter whiter=new FileWriter(file);//把字符写入文件
        BufferedWriter bw = new BufferedWriter(whiter);//整行写入
        String end = "bye";
        while(true){
            String line=br.readLine();//整行读取放入数组
            if(line.equals(end)) {
                break;
            }
            bw.write(line); //整行写入
            bw.newLine();//换行
        }
        
        bw.flush();//刷新缓存区
        
        bw.close();//关闭通道
        whiter.close();//关闭通道
        
        

    注意:

    [1] 标准输入流以字节流流入内存,如果在控制台中输入字符,字符以默认编码(win简体:gbk)编码成字节进入标准输入流。

    package sc.IO;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    
    /*标准输出流(PrintStream)
     * 数据目的地是标准输出设备(显示器)等输出设备。
     * 在java中用System.out得到一个PrintStream 字节输出流(字节打印流)。
     * 提供了更强大的print     println
     * 打印方法用于打印各种数据类型。
     * */
    public class System02 {
    public static void main(String[] args) throws IOException {
        //需求:读取文件,显示到标准输出设备
        File file = new File("D:\gameproject\a.txt");
        FileReader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        PrintStream ps = System.out;
        String line;
        while( (line=br.readLine())!=null ) {
            ps.println(line);
        }
        
        reader.close();
        br.close();
        
        //需求:读取文件,显示到标准输出设备(utf8)
        File file1 = new File("D:\gameproject\aa.txt");
        FileInputStream in = new FileInputStream(file1);
        InputStreamReader reader1 = new InputStreamReader(in, "UTF-8");
        BufferedReader br1 = new BufferedReader(reader1);
        PrintStream ps1 = System.out;
        String str;
        while( (str=br1.readLine())!=null ) {
            ps.println(str);
        }
        
        in.close();
        reader.close();
        
        
        String str2="你好,中国。";
        byte[] buf = str2.getBytes("GBK");    
        PrintStream ps2 = System.out;
        ps2.write(buf);//输出结果:你好,中国。
    

    //PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。

    
        
        
    }
    }
    package sc.IO;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Dath01 {
    public static void main(String[] args) throws IOException {
        //通过scanner扫描文件字节流等    扫描平台默认编码的文件
        /*File file = new File("D:\gameproject\a.txt");
        Scanner sc = new Scanner(file);
        */
        
        // 扫描指定编码的文件
        Scanner sc = new Scanner(new FileInputStream(new File("D:\gameproject\aa.txt")), "UTF-8");
        
        String line;
        while (sc.hasNextLine()) {
            line = sc.nextLine();
            System.out.println(line);}
    }
    }
     
    package sc.IO;
    /*序列化
     * 把内存中的对象永久保存到硬盘的过程称为对象序列化,也叫做持久化。
     * 把硬盘持久化的内存恢复的内存的过程称为对象反序列化。
     * 
     * Serializable
     * 类通过实现 java.io.Serializable 接口以启用其序列化功能。
     * 未实现此接口的类将无法使其任何状态序列化或反序列化,并抛出异常:java.io.NotSerializableException
     * */
    import java.io.Serializable;
    //Serializable接口没有方法或字段,仅用于标识可序列化的语义(必须实现此接口才能序列化)
    public class Student implements Serializable {
        
        /*序列化版本
         * 当序列化完成后,后期升级程序中的类(Student),此时再反序列化时会出现异常:java.io.InvalidClassException
         * 异常原因:序列化流的serialVersionUID和升级后类的版本不匹配。
         * 解决方案:给Student类加序列化版本号,有两种方式:
         * default serial version ID 生成默认的serial version ID 一般值都是1L。
         * generated serial version ID 根据当前类的属性、方法生成一个唯一ID。
         * */
        private static final long serialVersionUID = -2014154968311606488L;
        /**
         * private static final long serialVersionUID = 1L;
         */
        
    
        
        
        //transient
        //开发过程中,如果想忽略某些字段不让其序列化时,可以使用transient修饰。
        //private transient String name;
        //看到结果 返回默认值
        private String id;
        private  String name;
        private int age;
        
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        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;
        }
        public Student(String id, String name, int age) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
        }
        public Student() {
            super();
        }
    
    }
    
    
    


    package
    sc.IO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Serializable01 { public static void main(String[] args) throws IOException, ClassNotFoundException { Student stu=new Student("110","小二",20); /**序列化对象 * ObjectOutputStream 继承于OutputStream,专门用于把对象序列化到本地。 * 提供了writeXXX writeObject() 用于写入一个对象 * * 方案1:取stu所有的属性,通过特定的字符串(-),把各个属性值连接起来 */ File file = new File("D:\gameproject\aaa.txt"); FileOutputStream out = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(out); oos.writeObject(stu); oos.close(); out.close(); /*反序列化对象 * ObjectInputStream 继承于InputStream ,专门用于把本地持久化内容反序列化到内存, * 提供了readXXX readObject() 用于读取一个序列化内容并返回一个对象。 * */ FileInputStream in = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(in); Student student = (Student) ois.readObject(); System.out.println(student.getId());//输出结果:110 System.out.println(student.getName());//输出结果:小二 System.out.println(student.getAge());//输出结果:20 ois.close(); in.close(); } }


    package sc.IO;
    
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /*DataInputStream/DataOutputStream
    DataOutputStream 继承OutputStream,专门用于把基本java数据类型写入输出流。
    提供了writeXXX 写入基本java数据类型。
    DataInputStream 继承于InputStream,允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。
    DataInputStream/DataOutputStream 特别适合读取/写入在网络传输过程中的数据流。*/
    public class Date02 {
    public static void main(String[] args) throws IOException {
        //写入数据
        File file = new File("D:\gameproject\b.txt");
        FileOutputStream out= new FileOutputStream(file);
        DataOutputStream dos = new DataOutputStream(out);
        dos.writeInt(10);
        dos.writeUTF("hello中国");
        
        dos.close();
        out.close();
        
        System.out.println("写入完成");
    }
    }
    
    
    

    package
    sc.IO; //DataInputStream 读取数据 import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Date03 { public static void main(String[] args) throws IOException { File file = new File("D:\gameproject\b.txt"); FileInputStream in = new FileInputStream(file); DataInputStream dis = new DataInputStream(in); int a = dis.readInt(); String str = dis.readUTF(); in.close(); dis.close(); System.out.println(a); System.out.println(str); } }
  • 相关阅读:
    jQuery之防止冒泡事件
    jQuery复制节点
    jQuery查找节点
    jQuery表单选择器
    jQuery之事件触发trigger
    jQuery样式操作
    为FLASH正名!HTML5前景分析
    iframe 高度自动调节,最简单解决
    Iframe和母版页(.net)
    表单遮住弹出层解决方法(select遮住DIV)
  • 原文地址:https://www.cnblogs.com/406070989senlin/p/10827815.html
Copyright © 2011-2022 走看看