I/O系统 (输入/输出)

I/O系统

1:流:

    (1)判断到底是输入,还是输出;永远站在程序的立场上;

    (2)判断传递的到底是字节还是字符,从而决定管道的粗细;

       字节管道可以传递所有数据,字符管道专门用来传递文本数据(1个字符等于2个字节)

 

2:java流四大父类:

  流的分类:字符流;字节流(对象流)

    字节流(用于输出文件、影音等存储较大的输出方式“2进制输出全类型”,尽量不传输文本文件,):

      InputStream(输入)确定输入是字节管道,InputStream子类前面的单词作为节点(File:是文件的意思,作为数据源输出),通过查阅API文档;

      OutputStream(输出)确定输入是字节管道,OutputStream子类前面的单词作为节点(File:是文件的意思,作为数据源输入),通过查阅API文档;

    字符流(用于输出文本文件等存储较小的输出方式):

      Reader(输入)

           Writer(输出)

      输入流与输出流

          输入:InputStream、Reader

          输出:OutputStream、Writer

    提供父类,为所有的子类提供方法

         只要输入输出,就会伴随异常出现,不能直接new,先等于null在new出管道,确定两端的管道,确定后先做关闭管道(养成习惯),

  流的方向:

public static void main(String [] args){

    //文件的拷贝,这是可能在面试中出现的手工书写代码!

    //功能:将D:/test.avi 拷贝到F:/wudi.avi

    //    只要输入输出都有可能有编译时异常

    FileInputStream fis = null;

    FileOutputStream fos = null;

    try{

        //1.建立管道

       fis = new FileInputStream( “D:/test.avi”);

       fos = new FileInputStream( “F:/wudi.avi”);

      

       //2、操作管道

  //方法一:输出时间过久

       int b =0;//明明是读一个字节,为什么要用一个int来接?

       while((b = fis.read() )!=-1){

           fos.write(b);

       }

  // 方法二:会使输出文件字节变大

       byte[] b = new byte [1024];//[1024]可增大,加大输出,但范围尽量不要超过兆级!(通过生产测试机上测试确定)

       //1Byte = 8bit;      1KB = 1024B;  1MB = 1024KB;

       int length = 0;//记录读取了多少个有效字节数

       while((length = fis.read(b))!= -1){ //-1判断是否读到文件末尾

           fos.write(b,0,length);

           fos.fiush();//强制刷出缓冲区的内容

       }

    } catch(FileNotFoundException e){

       e.printStackTrace

} catch(IOException e){

           e.printStackTrace

    } finally{

       //3.关闭管道

       if(fis != null){

           fis.close();

       } catch(IOException e){

           e.printStackTrace

}

    }

    if(fos != null){

        try{

           fos.close();

       } catch(IOException e){

           e.printStackTrace

       }

3:对象的序列化/反序列化

  (学习分布式应用的基础“跨文件操作”):

  序列化:将内存当中的对象以二进制流的形式输出;

  反序列化:将输入的二进制对象流转换为内存中的一个对象;(第二种产生对象的方式:反序列化;“第一种:new;”)

        “强调将对象以二进制形式输出,但没有确定输出到什么地方”

          (接口章节)标示接口“Serializable”:允许实现他的类进行某项操作;往往不具备任何方法;

    应该实现Serializable 接口

  管道的对接:对象

Serializable——可序列化接口

  javaBean规范中的第三项:应该实现Serializable接口!

让StudentBean implements Serializable(io中)每个类的每次编译都应该重新生成;

使用transient关键字修饰的属性,其值不参与序列化;

 public static void main (String [] args){

    //对象序列化——将对象以二进制流的形式输出;

    StudentBean sb = new StudentBean(“zhang3”,24,true);

    ObjectOutputStream  oos = null;

//  FileOutputStream  fos = null;

    try{

        oos = new ObjectOutputStream(new FileOutputStream (“student.data”));//不用关闭管道;

       oos.writeObject(sb)

//      fos = new FileOutputStream (“student.data”); // (“student.data”)  文件的相对路径;

//     oos = new ObjectOutputStream(fos);

} catch(FileNotFoundException e){

       e.printStackTrace

} catch(IOException e){

           e.printStackTrace

    } finally{

       if(oos != null){

           oos.close();

       } catch(IOException e){

           e.printStackTrace

}

    }

    //对象反序列化——将输入的二机制流转换为内存中的对象

    ///反序列化是Java中第二种产生对象的方式

    StudentBean sb = null;

    ObjectInputStream ois = null;

    try{

       ois = new ObjectInputStream (new FileInputStream (“student.data”));

       sb = (StudentBean)ois.readObject();

} catch(FileNotFoundException e){

       e.printStackTrace

} catch(IOException e){

           e.printStackTrace

    } finally{

       if(ois != null){

           ois.close();

       } catch(IOException e){

           e.printStackTrace

}

    }

System.out.println(sb.getName);

}

file类(文件类):

    可以表示文件或文件夹(文件夹也是文件的一种)

public static void main(String[]   args){

    //file类——来表示操作系统的文件或文件夹对象

    File file = new File(“F:/wudi.avi”);

    File dir = new File(“F:/ppt”);

    //作为文件对象的常用方法

    String path1 = file.getAbsolutePath();//获取绝对路径

    Stringpath2 =file.getPath();//获取相对路径

    long space =file.length();//获取文件大小

    long time = file.lastModified();//最后修改时间

    System.out.println(path);

  System.out.println(space);

    System.out.println(new Date(time));

  System.out.println(file.isHidden());//是否是隐藏文件

  System.out.println(file.isFile());//是否是文件(true是,flo否)

  System.out.println(dir.isDirectory());//是否是文件夹

  //文件分隔符(File.pathSeparator)

  String path = “D:”+File.pathSeparator + ”ffdfd” + File.pathSeparator +”ffafa”;

  path = "D:" + System.getProperty("file.separator") + "fdfddfd" + System.getProperty("file.separator") + "fdfdfd";

  System.out.println(path);

    //作为文件夹对象的常用方法

  System.out.println("***************************************");//分割

    String[] subFileNames = dir.list();//的到文件夹下面的所有子文件或子文件夹的名字

    for(String subFileName : subFileNames){

       System.out.ptintln(subFileName());

    }

    File[]subFiles = dir.listFiles();//的到文件夹下面的所有子文件或文件夹的File对象

for(File subFile : subFiles){

       System.out.ptintln(subFile.getName());

    }

   System.out.println("***************************************");//分割

    // 面试体:给任意一个文件夹,打印出该文件夹下面所有的子文件的名字(无论有多少层)“使用递归”

       String inputDir = JOptionPane. showInputDialog(“请输入的不是一个文件夹。”);

       showAllSubFile(new File(inputDir));

    public static void showAllSubFile(File die){

       if(dir.isDirectory()){

           File[] subFiles = dir.listFiles();

           for(File subFile : subFiles){

              if(subFile.isDirectory()){

              showAllSubFile(subFile);

              }else {

                  System.out.println(subFile.getAbsolutePath());

              }

           }

       }else{

           JOptionPane.showMessageDialog(null,“您输入的不是一个文件夹。”);

       }

    }

}

原文地址:https://www.cnblogs.com/scxu28/p/6188168.html