zoukankan      html  css  js  c++  java
  • javaIO-学习笔记

      1 package IOTest;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.io.IOException;
      7 import java.io.ObjectInput;
      8 import java.io.ObjectInputStream;
      9 import java.io.ObjectOutput;
     10 import java.io.RandomAccessFile;
     11 
     12 public class IOMain {
     13 
     14     public static void main(String[] args) throws IOException {
     15         
     16         /*
     17          * 1
     18          */
     19 //        try (FileReader fr = new FileReader("E:/Workspase/MyEclipse 2015 CI/Shulie/src/A2.java")) {
     20 //            char[] cbuf = new char[32];
     21 //            int hasRead = 0;
     22 //            while((hasRead = fr.read(cbuf))>0){
     23 //                System.out.println(new String(cbuf, 0, hasRead));
     24 //            }
     25 //            
     26 //        } catch (FileNotFoundException e) {
     27 //            // TODO Auto-generated catch block
     28 //            e.printStackTrace();
     29 //        }
     30         
     31         
     32         /*
     33          * 2 节点流
     34          */
     35 //        File f = new File("newfile.txt");
     36 //        
     37 //        try(
     38 //                FileInputStream fis = new FileInputStream("E:/Workspase/MyEclipse 2015 CI/Shulie/src/A2.java");
     39 //                FileOutputStream fos = new FileOutputStream(f))
     40 //        {
     41 //            byte[] buf = new byte[1024];
     42 //            int hasRead = 0;
     43 //            while((hasRead = fis.read(buf))>0){
     44 //                fos.write(buf, 0, hasRead);
     45 //            }
     46 //            
     47 //        }catch(Exception e){
     48 //            e.printStackTrace();
     49 //        }
     50         
     51         
     52         /*
     53          * 3  转换流
     54          */
     55 //        try (InputStreamReader reader = new InputStreamReader(System.in);
     56 //            BufferedReader bf = new BufferedReader(reader)    ){        
     57 //            String line = null;
     58 //            while((line = bf.readLine()) !=null){
     59 //                if("exit".equals(line)){
     60 //                    System.exit(1);
     61 //                }
     62 //                System.out.println(line);
     63 //            }
     64 //        } catch (Exception e) {
     65 //            // TODO Auto-generated catch block
     66 //            e.printStackTrace();
     67 //        }
     68         
     69         /*
     70          * 4 重定向输出
     71          */
     72 //        try (PrintStream ps = new PrintStream(new FileOutputStream("out.txt"))) {
     73 //            
     74 //            System.setOut(ps);
     75 //            System.out.println(new Scanner(System.in).next());
     76 //            System.out.print(new IOMain());
     77 //        } catch (Exception e) {
     78 //            // TODO Auto-generated catch block
     79 //            e.printStackTrace();
     80 //        }
     81 //        
     82 //        
     83         
     84         
     85         /*
     86          * 5 RandomAccessFile
     87          */
     88 //        
     89 //        try (RandomAccessFile raf = new RandomAccessFile("newfile.txt", "rw");) {
     90 //            System.out.println("指针初始位置:"+raf.getFilePointer());
     91 //            raf.seek(300);
     92 //            byte[] bbuff = new byte[1024];
     93 //            int hasRead = 0;
     94 //            while((hasRead = raf.read(bbuff)) > 0){
     95 //                System.out.println(new String(bbuff, 0, hasRead));
     96 //            }
     97 //            raf.seek(raf.length());
     98 //            raf.write("
    追加的内容!
    ".getBytes());
     99 //        } catch (Exception e) {
    100 //            // TODO Auto-generated catch block
    101 //            e.printStackTrace();
    102 //        }
    103 //        
    104 //        
    105 //        insert("out.txt", 3, "
    插入的内容
    ");
    106 //        FileInputStream fis = new FileInputStream(new File("out.txt"));
    107 //        byte[] bbuff = new byte[1024];
    108 //        int hasRead = 0;
    109 //        while((hasRead = fis.read(bbuff)) > 0){
    110 //            System.out.println(new String(bbuff, 0, hasRead));
    111 //        }
    112 //        
    113 //        
    114         /*
    115          * 6  序列化
    116          */
    117         
    118     
    119 //        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.txt"));) {
    120 //            Person p1 = new Person("Jeff", 23);
    121 //            oos.writeObject(p1);
    122 //        } catch (Exception e) {
    123 //            // TODO Auto-generated catch block
    124 //            e.printStackTrace();
    125 //        }
    126         
    127         try (ObjectInputStream oip = new ObjectInputStream(new FileInputStream("object.txt"));) {
    128             Person p2 = (Person) oip.readObject();
    129             System.out.println("name:"+p2.getName()+"
    "+"age:"+p2.getAge());
    130         } catch (ClassNotFoundException e) {
    131             // TODO Auto-generated catch block
    132             e.printStackTrace();
    133         }
    134         
    135         
    136         
    137         
    138         
    139 
    140     }
    141     public static void insert(String fileName, long pos, String insertContent) throws IOException{
    142         File temp = File.createTempFile("temp", null);
    143         temp.deleteOnExit();
    144         try (
    145         RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
    146         FileOutputStream fos = new FileOutputStream(temp);
    147         FileInputStream fis = new FileInputStream(temp);) {
    148             raf.seek(pos);
    149             byte[] buff = new byte[64];
    150             int hasRead = 0;
    151             while((hasRead = raf.read(buff))>0){
    152                 fos.write(buff, 0, hasRead);
    153             }
    154             raf.seek(pos);
    155             raf.write(insertContent.getBytes());
    156             while((hasRead = fis.read(buff))>0){
    157                 raf.write(buff, 0, hasRead);
    158             }
    159         } catch (Exception e) {
    160             // TODO Auto-generated catch block
    161             e.printStackTrace();
    162         }
    163     }
    164 
    165 }
    166 class Person implements java.io.Serializable{
    167     private String name;
    168     private int age;
    169     public Person(String name, int age){
    170         this.name = name;
    171         this.age = age;
    172     }
    173     public String getName() {
    174         return name;
    175     }
    176     public void setName(String name) {
    177         this.name = name;
    178     }
    179     public int getAge() {
    180         return age;
    181     }
    182     public void setAge(int age) {
    183         this.age = age;
    184     }
    185     
    186 }
    187 class PersonNew implements java.io.Externalizable{
    188     private String name;
    189     private int age;
    190     public PersonNew(String name, int age){
    191         this.name = name;
    192         this.age = age;
    193     }
    194     public String getName() {
    195         return name;
    196     }
    197     public void setName(String name) {
    198         this.name = name;
    199     }
    200     public int getAge() {
    201         return age;
    202     }
    203     public void setAge(int age) {
    204         this.age = age;
    205     }
    206     @Override
    207     public void writeExternal(ObjectOutput out) throws IOException {
    208         out.writeObject(new StringBuffer(name).reverse());
    209         out.writeInt(age);
    210         
    211     }
    212     @Override
    213     public void readExternal(ObjectInput in) throws IOException,
    214             ClassNotFoundException {
    215         this.name = ((StringBuffer)in.readObject()).reverse().toString();
    216         this.age = in.readInt();
    217     }
    218     
    219 }
  • 相关阅读:
    Codeforces758C
    Codeforces758B
    CodeForces758A
    数据库实习
    数据结构实习-迷宫(基于Qt实现)
    CodeForces757B
    CodeForces757A
    HDU3790
    数据结构实习
    Codeforces374B
  • 原文地址:https://www.cnblogs.com/steve-jiang/p/6078061.html
Copyright © 2011-2022 走看看