zoukankan      html  css  js  c++  java
  • [总结] I/O输入,输出

    I/O输入,输出

    第一:先判断到底是输入还是输出,站在程序的立场

    第二:判断是传递字节,还是字符,决定管道粗细,字节流是最基本的数据输出管道。字符类型管道专门用来传送文本数据。

    Java流的四大父类:1.字节流(包含InputStream,OutputStream)
              2.字符流(包含Read,Write)
    文件的拷贝,这里可能是在面试中出现的手工书写的代码最多的题之一

    public class Testio {
    
        public static void main(String[] args) {
            FileReader fir = null;
            FileWriter fiw = null;
            try {
                fir = new FileReader("E:/a/test.txt");
                fiw = new FileWriter("E:/test.txt");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            char b [] = new char[1024];
            int length = 0;
            try {
            while((length = fir.read(b)) != -1 ){
                    fiw.write(b, 0, length);
                } 
                }catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            
            
            finally{
                if (fir != null) {
                    try {
                        fir.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (fiw != null) {
                    try {
                        fiw.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

    对象序列化和反序列化
    序列化:指将内存中的对象以二进制流的形式输出
    反序列化:将输入的二进制对象流转化成内存中的一个对象
    实现传输功能需要使用可序列化接口---Serializable
    JavaBean规范中德第三项:应该加上Serializable接口
    Transient 关键字修饰的属性,其值不参与序列化。

    public class TestSerializable {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            对象序列化---将对象以二进制流的形式输出
            StudentBean sb = new StudentBean("zhang3",24,true,new HomeAddress("成都市","建设北路","2-3号"));        
            ObjectOutputStream oos = null;
            
            try {
                oos = new ObjectOutputStream(new FileOutputStream("student.data"));
                oos.writeObject(sb);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                if(oos != null){
                    try {
                        oos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }   

            对象反序列化---将输入的二进制流转换为内存中的对象
            反序列化是Java当中第2种产生对象的方式

            StudentBean sb = null;    
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream("student.data"));
                sb = (StudentBean)ois.readObject();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                if(ois != null){
                    try {
                        ois.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(sb.getName());
            System.out.println(sb.getAge());
            System.out.println(sb.getMyHome().getCity());
        }
    
    }

    File.类
    表是文件或文件夹对象
    作为文件对象的常用方法
    .getAbsolutePath();获取绝对路径
    .getPath();获取相对路径
    .length();获取文件大小
    作为文件夹对象的常用方法
        String [] subFileName = dir.list();:得到文件夹下面的字文件,返回String数组

  • 相关阅读:
    基本指令
    javascript event(事件对象)详解
    Sass进阶之路,之二(进阶篇)
    Sass进阶之路,之一(基础篇)
    原型链进阶
    数据类型检测
    JavaScript引用类型和值类型
    i.mx6 Android6.0.1分析input子系统:测试
    (三)JNI常用示例
    (二)JNI方法总结
  • 原文地址:https://www.cnblogs.com/jrc2016/p/6184524.html
Copyright © 2011-2022 走看看