zoukankan      html  css  js  c++  java
  • JAVA FILE or I/O学习

      1 public class IOStreamKnow
      2 {
      3     /*********************************文件读写方式:字节流************************************/
      4     /**
      5      * 方式一:基本方式,文件读写方式的基础
      6      */
      7     public void name()
      8     {
      9         try
     10         {
     11             //创建输入流,输入流用来读取文件字节信息
     12             //参数表示读取的文件对象
     13             FileInputStream input = new FileInputStream(new File("d:/a"));
     14             //创建输入流,将信息进行输出
     15             //参数表示输出的文件目标
     16             FileOutputStream output = new FileOutputStream(new File("e:/a"));
     17             //读取输入流中的信息
     18             //读取的字节
     19             int data;
     20             
     21             while((data = input.read()) != -1)
     22             {
     23                 //将读取的信息通过输出流完成输出
     24                 output.write(data);
     25             }
     26             //清空输出流
     27             output.flush();
     28             //关闭输出流
     29             output.close();
     30             //关闭输入流
     31             input.close();
     32             
     33         } catch (FileNotFoundException e)
     34         {
     35             e.printStackTrace();
     36         } catch (IOException e)
     37         {
     38             e.printStackTrace();
     39         }
     40     }
     41     /**
     42      * 文件读写缓冲流:推荐方式一
     43      * 该种方式需要依据FileInputStream为基础
     44      */
     45     public void name1()
     46     {
     47         try
     48         {
     49             //缓冲字节流依赖于字节流来构建
     50             BufferedInputStream buffInput = new BufferedInputStream(new FileInputStream(new File("d:/back.jpg")));
     51             //缓冲输出流
     52             BufferedOutputStream buffOutput = new BufferedOutputStream(new FileOutputStream(new File("e:/back.jpg")));
     53             int data;
     54             while((data = buffInput.read()) != -1)
     55             {
     56                 buffOutput.write(data);
     57             }
     58             buffOutput.flush();
     59             buffOutput.close();
     60             buffInput.close();
     61         } catch (FileNotFoundException e)
     62         {
     63             e.printStackTrace();
     64         } catch (IOException e)
     65         {
     66             e.printStackTrace();
     67         }
     68     }
     69     /**
     70      * 文件读写缓冲流:推荐方式二:常用方式
     71      * 该种方式需要依据FileInputStream为基础,并且可以自行定制读写的量
     72      */
     73     public void name2()
     74     {
     75         try
     76         {
     77             FileInputStream input = new FileInputStream(new File("d:/back.jpg"));
     78             FileOutputStream output = new FileOutputStream(new File("e:/back.jpg"));
     79             //缓冲字节数组
     80             byte[] buffer = new byte[1024];
     81             //将输入流读取的文件信息读入到缓冲区中,返回读取的长度
     82             int len;
     83             while((len = input.read(buffer)) != -1)
     84             {
     85                 
     86                 output.write(buffer,0,len);
     87             }
     88             output.flush();
     89             output.close();
     90             input.close();
     91         } catch (FileNotFoundException e)
     92         {
     93             e.printStackTrace();
     94         } catch (IOException e)
     95         {
     96             e.printStackTrace();
     97         }
     98     }
     99     /**
    100      * 特定文件读取方式:按照指定的数据源类型,进行读取
    101      */
    102     public void name3()
    103     {
    104         //data可以在读写的时候按照特定的java格式进行数据的读写
    105         try
    106         {
    107             DataInputStream input = new DataInputStream(new FileInputStream(new File("d:/back.jpg")));
    108             DataOutputStream output = new DataOutputStream(new FileOutputStream(new File("e:/back.jpg")));
    109             int data;
    110             while((data = input.read()) != -1)
    111             {
    112                 output.write(data);
    113             }
    114             output.flush();
    115             output.close();
    116             input.close();
    117         } catch (FileNotFoundException e)
    118         {
    119             e.printStackTrace();
    120         } catch (IOException e)
    121         {
    122             e.printStackTrace();
    123         }
    124     }
    125     /*********************************对象的存储、读入方式:序列化、反序列化************************************/
    126     /**
    127      * 序列化写入(存储)对象数据
    128      * 反序列化读取对象数据
    129      */
    130     public void name4()
    131     {
    132         User user = new User(1001, "tom");
    133         
    134         //通过序列化保存数据
    135         try
    136         {
    137             ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("d:/temp.txt")));
    138             output.writeObject(user);
    139             output.flush();
    140             output.close();
    141             
    142         } catch (FileNotFoundException e)
    143         {
    144             e.printStackTrace();
    145         } catch (IOException e)
    146         {
    147             e.printStackTrace();
    148         }
    149         
    150         //通过反序列化对之前保存的数据进行对象的转换
    151         try
    152         {
    153             ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("d:/temp.txt")));
    154             //读取文件中的对象
    155             User user1 = (User)input.readObject();
    156             input.close();
    157             System.out.println(user1.getUserId());
    158             System.out.println(user1.getUserName());
    159         } catch (FileNotFoundException e)
    160         {
    161             e.printStackTrace();
    162         } catch (IOException e)
    163         {
    164             e.printStackTrace();
    165         } catch (ClassNotFoundException e)
    166         {
    167             e.printStackTrace();
    168         }
    169     }
    170     /*********************************文件读写方式:字符流,主要用于读取文本文件************************************/
    171     /**
    172      * 方式二:由字节流衍生而来,字符流读写方式的基础
    173      */
    174     public void name5()
    175     {
    176         //方式一:基本方式,文件读写方式的基础
    177         try
    178         {
    179             //创建字符输入流,字符流的构建依赖于字节流,InputStreamReadder是字节流通向字符流的桥梁
    180             //不能使用字符流读取使用字节描述的文件信息,如图片,音频文件,视频文件
    181             InputStreamReader reader = new InputStreamReader(new FileInputStream(new File("d:/test.txt")));
    182             //创建字符输出流
    183             OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File("e:/test.txt")));
    184             int data;
    185             while((data = reader.read()) != -1)
    186             {
    187                 writer.write(data);
    188                 //System.out.println(data);
    189             }
    190             writer.flush();
    191             writer.close();
    192             reader.close();
    193             
    194         } catch (FileNotFoundException e)
    195         {
    196             e.printStackTrace();
    197         } catch (IOException e)
    198         {
    199             e.printStackTrace();
    200         }
    201     }
    202     /**
    203      * 字符缓冲流:由字符流读写方式的基础而来
    204      */
    205     public void name6()
    206     {
    207         try
    208         {
    209             BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("d:/集合项目.txt"))));
    210             
    211             String str = "";
    212             //每次读取一行
    213             while((str = reader.readLine()) != null)
    214             {
    215                 System.out.println(str);
    216             }
    217             reader.close();
    218         } catch (FileNotFoundException e)
    219         {
    220             e.printStackTrace();
    221         } catch (IOException e)
    222         {
    223             e.printStackTrace();
    224         }
    225         try
    226         {
    227             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("d:/my.txt"))));
    228             writer.write("自定义文本");
    229             //换行
    230             writer.newLine();
    231             writer.write("hello niit");
    232             writer.flush();
    233             writer.close();
    234         } catch (FileNotFoundException e)
    235         {
    236             e.printStackTrace();
    237         } catch (IOException e)
    238         {
    239             e.printStackTrace();
    240         }
    241     }
    242 }
  • 相关阅读:
    KMP字符串匹配算法
    彩色图像处理之色彩学基础
    Dijkstra单源最短路径算法
    论文笔记 [6] 图像幻构中的 Feature Enhancement
    论文笔记 [5] SRCNN
    论文笔记 [4] ARCNN(Artifacts Reduction CNN)
    论文笔记 [3] CNN去compression artifacts
    LeetCode小白菜笔记[11]:Search Insert Position
    论文笔记[2] 基于深度学习的CNN图像质量评估和预测
    Geophysics背景知识(3)
  • 原文地址:https://www.cnblogs.com/Wfei/p/3332770.html
Copyright © 2011-2022 走看看