I/O的一些简单操作

                            I  / O

流(stream)是供数据传输的通道

  1. 字节流(通用的)

 

inputStream(父类)

  FileInputStream

outputStream(父类)

  FileOutputStream

 

操作流的步骤

a)     产生字节流对象

b)     调用读写方法

c)     关闭流

2. public void copyImage(){

  1. 3.    FileInputStream fis=null;
  2. 4.    FileOutputStream fos=null;
  3. 5.    int i=0;
  4. 6.    try {
  5. 7.       //创建流对象
  6. 8.       fis=new FileInputStream("E:/PIC/11080901168c233be5b9173bc7.jpg");
  7. 9.       fos=new FileOutputStream("E:/LM.jpg");
  8.         //读取并写入
  9.         byte[] bt=new byte[1024];
  10.         while((i=fis.read(bt))!=-1){
  11.            fos.write(bt,0,i);
  12.         }
  13.       } catch (FileNotFoundException e) {
  14.         // TODO Auto-generated catch block
  15.         e.printStackTrace();
  16.       } catch (IOException e) {
  17.         // TODO Auto-generated catch block
  18.         e.printStackTrace();
  19.       }finally{
  20.         try {
  21.            fis.close();
  22.            fos.close();
  23.         } catch (IOException e) {
  24.            // TODO Auto-generated catch block
  25.            e.printStackTrace();
  26.         }
  27.        
  28.       }
  29.    }
  30.    public static void main(String[] args) {
  31.       // TODO Auto-generated method stub
  32.       TestIO tio=new TestIO();
  33.       tio.copyImage();

36. }

  1. 字符流

a)     父类

                        i.          Reader

  1. 子类    filereader

                       ii.          Writer

  1. 子类   filewriter

b)     ReadLine 读取一行是bufferedreader中的方法

c)     流链接

  1. public void buffRead(){
  2.       FileReader fr=null;
  3.       FileWriter fw=null;
  4.       BufferedReader br=null;
  5.       try {
  6.         //创建流
  7.         fr=new
    FileReader("E:/PIC/1.txt");
  8.         fw=new FileWriter("E:/新建文本文档 .doc");
  9.         br=new BufferedReader(fr);
  10.         String str=null;
  11.         //读取
  12.         while((str=br.readLine())!=null){
  13.            fw.write(str+"\n");
  14. //         System.out.println(str);
  15.         }
  16.       } catch (FileNotFoundException e) {
  17.         // TODO Auto-generated catch block
  18.         e.printStackTrace();
  19.       } catch (IOException e) {
  20.         // TODO Auto-generated catch block
  21.         e.printStackTrace();
  22.       }finally{
  23.         try {
  24.            fr.close();
  25.            fw.close();
  26.            br.close();
  27.         } catch (IOException e) {
  28.            // TODO Auto-generated catch block
  29.            e.printStackTrace();
  30.         }
  31.       }
  32.    }
  33.    public static void main(String[] args) {
  34.       // TODO Auto-generated method stub
  35.       TestIO tio=new TestIO();
  36. //    tio.copyImage();
  37.       tio.buffRead();

74. }

     //字节转换字符

public void inputStreamRead(){

      FileInputStream fis=null;//字节

      BufferedReader br=null;//字符

      try {

        fis=new FileInputStream("E:/PIC/1.txt");

       br=new BufferedReader(new InputStreamReader(fis));//InputStreamReader将字节流转换成字符流

        String str=null;

        while((str=br.readLine())!=null){

           System.out.println(str);

        }

      } catch (FileNotFoundException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

      } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

      }

   }

   public static void main(String[] args) {

      // TODO Auto-generated method stub

      TestIO tio=new TestIO();

//    tio.copyImage();

//    tio.buffRead();

      tio.inputStreamRead();

}

 

  1. 对象流

对象流的子类1. ObjectOutputStream 2. ObjectInputStream

把内存(堆)中的对象转换成二进制数据输出,叫对象的序列化

把二进制数据还原成内存当中的对象,叫对象的反序列化(产生对象的第二种方法,第一种使用关键字 new)

Serializable是标识性接口 (允许可序列化的标识)

让一个类可序列化必须时间标识接口

当一个类的属性值不想被序列化时可以属性前面加上transient 关键字

 

      public void testSeriallzable(){

        ObjectOutputStream oos=null;

        FileOutputStream fos=null;

        TestBean tb=new TestBean();

        tb.setAge(19);

        tb.setName("张三");

       

        try {

           fos=new FileOutputStream("date.sql");

           oos=new ObjectOutputStream(fos);

           oos.writeObject(tb);

        } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

        } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

        }finally{

           try {

              fos.close();

           } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

        }

      }

  

  

      public static void main(String[] args) {

        // TODO Auto-generated method stub

        TestIO tio = new TestIO();

        // tio.copyImage();

        // tio.buffRead();

        // tio.inputStreamRead();

//      tio.filedelete("E:/testfile");

        tio.testSeriallzable();

      }

 

 

 

 

 

 

 

File 类(文件和目录路径名的抽象表示)

 

      //递归

   public void filedelete(String path) {

      File file = new File(path);

      File[] fileSize = file.listFiles();

      for (int i = 0; i < fileSize.length; i++) {

        if (fileSize[i].isFile()) {

           fileSize[i].delete();

        } else {

           filedelete(fileSize[i].getAbsolutePath());

        }

      }

      file.delete();

   }

 

   public static void main(String[] args) {

      // TODO Auto-generated method stub

      TestIO tio = new TestIO();

      // tio.copyImage();

      // tio.buffRead();

      // tio.inputStreamRead();

      tio.filedelete("E:/testfile");

   }

posted @ 2014-05-07 22:31  就想穿板甲  阅读(199)  评论(0编辑  收藏  举报