zoukankan      html  css  js  c++  java
  • Java-IO

     1 public class Member {
     2     private String name;  
     3     private int age;  
     4     public Member() {  
     5     }  
     6    public Member(String name, int age) {  
     7         this.name = name;  
     8         this.age = age;  
     9     }  
    10     public void setName(String name){  
    11         this.name = name;  
    12     }  
    13     public void setAge(int age) {  
    14         this.age = age;  
    15     }  
    16     public String getName() {  
    17         return name;  
    18     }  
    19     public int getAge() {  
    20         return age;  
    21     }  
    22 }
     1 import java.io.DataInputStream;
     2 import java.io.DataOutputStream;
     3 import java.io.FileInputStream;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 /**
     8  * DataOutputStream
     9  */
    10 public class DataStreamTest {
    11     public static void main(String[] args) {
    12         Member[] members = { new Member("Justin", 90), 
    13                              new Member("momor", 95), 
    14                              new Member("Bush", 88) };
    15         try {
    16             DataOutputStream dos = new DataOutputStream(new FileOutputStream("E:/member.txt"));
    17             for (Member member : members) {
    18                 // 写入UTF字符串
    19                 dos.writeUTF(member.getName());
    20                 // 写入int数据
    21                 dos.writeInt(member.getAge());
    22             }
    23             dos.flush();// 将流中的数据刷新到目的地
    24             dos.close();// 关闭流
    25 
    26             DataInputStream dis = new DataInputStream(new FileInputStream("E:/member.txt"));
    27             // 读出数据并还原为对象
    28             for (int i = 0; i < members.length; i++) {
    29                 String name = dis.readUTF();// 读出UTF字符串
    30                 int score = dis.readInt();// 读出int数据
    31                 members[i] = new Member(name, score);
    32             }
    33             dis.close();// 关闭流
    34 
    35             // 显示还原后的数据
    36             for (Member member : members) {
    37                 System.out.printf("%s	%d%n", member.getName(), member.getAge());
    38             }
    39         } catch (IOException e) {
    40             e.printStackTrace();
    41         }
    42     }
    43 }
     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 
     6 /**
     7  * FileInputStream  字节输入流
     8  */
     9 public class FileInputStreamTest {
    10     public static void main(String[] args) {
    11         InputStream in = null;//文件输入流  
    12          try{  
    13              in = new FileInputStream(new File("E:/fileInputStreamTest.txt"));  
    14              /*
    15               * FileInputStream是有缓冲区的,所以用完之后必须关闭,否则可能导致内存占满,数据丢失。 
    16               */  
    17              int i = 0;//接收读取的内容
    18              //streamReader.read()从文件读取一个字节,并当做int类型返回,如果遇到文件末尾,则返回-1
    19              while((i = in.read())!=-1) {
    20                  System.out.print((char)i);//打印读取的内容
    21              }  
    22          }catch (final IOException e) {  
    23              e.printStackTrace();  
    24          }finally{  
    25              try{  
    26                  in.close();  //关闭流
    27              }catch (IOException e) {  
    28                 e.printStackTrace();  
    29              }  
    30          }  
    31     }
    32 }
     1 import java.io.FileInputStream;
     2 import java.io.FileOutputStream;
     3 import java.io.IOException;
     4 
     5 /**
     6  * 文件拷贝
     7  */
     8 public class FileCopy {
     9     public static void main(String[] args) {
    10          byte[] b=new byte[512];   //一次取出的字节数大小,缓冲区大小  
    11          int i=0;  
    12          FileInputStream input=null;  
    13          FileOutputStream out =null;  
    14          try {  
    15             input=new FileInputStream("E:/fileInputStreamTest.txt");  
    16             out=new FileOutputStream("E:/copy.txt",true); //如果文件不存在会自动创建,true表示写在文件末尾  
    17              
    18             while ((i=input.read(b))!=-1) { //i的目的在于防止最后一次读取的字节小于b长度,否则会自动被填充0
    19                 //使用输出流写入读取的内容到指定文件
    20                out.write(b, 0, i);
    21             }  
    22          } catch (final IOException e) {  
    23             e.printStackTrace();  
    24          }finally{  
    25             try {  
    26                input.close();  
    27                out.close();  
    28             } catch (IOException e) {  
    29                e.printStackTrace();  
    30             }  
    31          }  
    32     }
    33 }
     1 import java.io.BufferedReader;
     2 import java.io.BufferedWriter;
     3 import java.io.File;
     4 import java.io.FileReader;
     5 import java.io.FileWriter;
     6 import java.io.IOException;
     7 
     8 /**
     9  * BufferedReader、BufferedWriter
    10  */
    11 public class FileWriterTest {
    12     public static void main(String[] args) throws IOException{
    13         BufferedReader br = new BufferedReader(new FileReader(new File("E:/printTest.txt")));
    14         BufferedWriter bw = new BufferedWriter(new FileWriter(new File("E:/writeCopy.txt")));
    15         
    16         String str = null;
    17         try {
    18             while((str = br.readLine())!=null) { //一次读一行
    19                 bw.write(str);
    20             }
    21         } catch (IOException e) {
    22             e.printStackTrace();
    23         }finally {
    24             br.close();
    25             bw.close();
    26         }
    27     }
    28 }
     1 import java.io.Serializable;
     2 
     3 public class Student implements Serializable{
     4     
     5     private static final long serialVersionUID = 1L;
     6     private String name;  
     7     private int age;  
     8     public Student() {}
     9     public String getName() {
    10         return name;
    11     }
    12     public void setName(String name) {
    13         this.name = name;
    14     }
    15     public int getAge() {
    16         return age;
    17     }
    18     public void setAge(int age) {
    19         this.age = age;
    20     }
    21     public Student(String name, int age) {  
    22          this.name = name;  
    23          this.age = age;  
    24     }  
    25     @Override  
    26     public String toString() {  
    27         return "Student [name=" + name + ", age=" + age + "]";  
    28     }  
    29 }
     1 import java.io.FileInputStream;
     2 import java.io.FileOutputStream;
     3 import java.io.IOException;
     4 import java.io.ObjectInputStream;
     5 import java.io.ObjectOutputStream;
     6 import java.util.ArrayList;
     7 import java.util.List;
     8 
     9 /**
    10  *    ObjectOutputStream
    11  */
    12 public class ObjectStream {
    13     public static void main(String[] args) {
    14         // 对象输入输出流
    15         ObjectOutputStream ow = null;
    16         ObjectInputStream or = null;
    17         List<Student> stuList = new ArrayList<Student>();
    18         Object obj = null;
    19         try {
    20             // 先将对象输出到文件中
    21             ow = new ObjectOutputStream(new FileOutputStream("E:/student.txt"));
    22             ow.writeObject(new Student("gg", 22));
    23             ow.writeObject(new Student("tt", 18));
    24             ow.writeObject(new Student("rr", 17));
    25             ow.writeObject(null);//添加一个null  是为了后续判断是否读取到末尾
    26             // 然后从该文件读取对象
    27             or = new ObjectInputStream(new FileInputStream("E:/student.txt"));
    28             
    29             while((obj = or.readObject())!=null) {
    30                 stuList.add((Student) obj);// 将读取的对象强转成Student对象
    31             }
    32             for (Student student : stuList) {
    33                 System.out.println(student.getName() + "-" + student.getAge());
    34             }
    35         } catch (IOException e) {
    36             e.printStackTrace();
    37         } catch (ClassNotFoundException e) {
    38             e.printStackTrace();
    39         } finally {
    40             try {
    41                 ow.close();
    42                 or.close();//关闭流
    43             } catch (IOException e) {
    44                 e.printStackTrace();
    45             }
    46         }
    47     }
    48 }
     1 import java.io.FileReader;
     2 import java.io.IOException;
     3 import java.io.PrintWriter;
     4 
     5 /**
     6  * PrintWriter
     7  */
     8 public class PrintTest {
     9     public static void main(String[] args) {
    10         FileReader reader=null;        //读取字符文件的流  
    11         PrintWriter writer=null;    //写字符到控制台的流  
    12         char[] b=new char[512];   //一次取出的字节数大小,缓冲区大小  
    13         int i=0;  
    14         try {  
    15            reader=new FileReader("E:/fileInputStreamTest.txt");  
    16            //writer=new PrintWriter(System.out);  //输出到控制台
    17            writer=new PrintWriter("E:/printTest.txt");  //输出指定文件,没有自动创建
    18            
    19            while ((i = reader.read(b))!=-1) {  
    20               writer.write(b, 0, i);  
    21            }  
    22         } catch (IOException e) {  
    23            e.printStackTrace();  
    24         }finally{  
    25            try {  
    26               reader.close();  
    27            } catch (IOException e) {  
    28               e.printStackTrace();  
    29            }  
    30            writer.close();
    31         }  
    32     }
    33 }
     1 import java.io.ByteArrayInputStream;
     2 import java.io.IOException;
     3 import java.io.PushbackInputStream;
     4 /**
     5  * 回退流:PushbackInputStream类继承了FilterInputStream类是iputStream类的修饰者。
     6  * 提供可以将数据插入到输入流前端的能力(当然也可以做其他操作)。
     7  * 简而言之PushbackInputStream类的作用就是能够在读取缓冲区的时候提前知道下一个字节是什么,
     8  * 其实质是读取到下一个字符后回退的做法,这之间可以进行很多操作,这有点向你把读取缓冲区的过程当成一个数组的遍历,
     9  * 遍历到某个字符的时候可以进行的操作,
    10  * 当然,如果要插入,能够插入的最大字节数是与推回缓冲区的大小相关的,插入字符肯定不能大于缓冲区吧!
    11  */
    12 public class PushBackInputStreamTest {
    13     public static void main(String[] args) throws IOException {
    14         String str = "hello,rollenholt";  
    15         PushbackInputStream push = null; // 声明回退流对象  
    16         ByteArrayInputStream bat = null; // 声明字节数组流对象  
    17         bat = new ByteArrayInputStream(str.getBytes());  
    18         push = new PushbackInputStream(bat); // 创建回退流对象,将拆解的字节数组流传入  
    19         int temp = 0;  
    20         while ((temp = push.read()) != -1) { // push.read()逐字节读取存放在temp中,如果读取完成返回-1  
    21            if (temp == ',') { // 判断读取的是否是逗号
    22               push.unread(temp); //回到temp的位置  
    23               temp = push.read(); //接着读取字节  
    24               System.out.print("(回退" + (char) temp + ") "); // 输出回退的字符  
    25            } else {  
    26               System.out.print((char) temp); // 否则输出字符  
    27            }  
    28         }  
    29     }
    30 }
     1 import java.io.BufferedOutputStream;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.SequenceInputStream;
     8 import java.util.Enumeration;
     9 import java.util.Vector;
    10 
    11 /**
    12  * SequenceInputStream
    13  */
    14 public class SequenceInputStreamTest {
    15      /** 
    16        *  SequenceInputStream合并流,
    17        *   将与之相连接的流集组合成一个输入流并从第一个输入流开始读取, 
    18        *   直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。 
    19        *   合并流的作用是将多个源合并合一个源。可接收枚举类所封闭的多个字节流对象。 
    20        */  
    21       public static void main(String[] args) {  
    22          doSequence();  
    23       }  
    24       private static void doSequence() {  
    25          // 创建一个合并流的对象  
    26          SequenceInputStream sis = null;  
    27          // 创建输出流。  
    28          BufferedOutputStream bos = null;  
    29          try {  
    30             // 构建流集合。  
    31             Vector<InputStream> vector = new Vector<InputStream>();  
    32             vector.addElement(new FileInputStream("E:/text1.txt"));  
    33             vector.addElement(new FileInputStream("E:/text2.txt"));  
    34             vector.addElement(new FileInputStream("E:/text3.txt"));  
    35             Enumeration<InputStream> e = vector.elements();  
    36        
    37             sis = new SequenceInputStream(e);  //将多个输入流合并到合并流
    38             bos = new BufferedOutputStream(new FileOutputStream("E:/text4.txt",true));//true追加末尾
    39             // 读写数据  
    40             byte[] buf = new byte[1024];  
    41             int len = 0;  
    42             while ((len = sis.read(buf)) != -1) {  
    43                bos.write(buf, 0, len);  
    44                bos.flush();
    45             }  
    46          } catch (FileNotFoundException e1) {  
    47             e1.printStackTrace();  
    48          } catch (IOException e1) {  
    49             e1.printStackTrace();  
    50          } finally {  
    51             try {  
    52                if (sis != null)  
    53                   sis.close();  
    54             } catch (IOException e) {  
    55                e.printStackTrace();  
    56             }  
    57             try {  
    58                if (bos != null)  
    59                   bos.close();  
    60             } catch (IOException e) {  
    61                e.printStackTrace();  
    62             }  
    63          }  
    64       }  
    65 }
  • 相关阅读:
    Atitit 引流矩阵与矩阵引流 推广方法 attilax总结
    Atitit 怎么阅读一本书 消化 分析 检索 attilax总结 1. 读书的本质 是数据的处理,大量的数据,处理能力有限的大脑 2 2. ETL数据清洗转换 摘要,缩小数据规模 2 2.1
    Atitit 为什么要读书,读书的好处是什么 attilax总结
    Atititi. naming spec 联系人命名与remark备注指南规范v5 r99.docx
    Atitit 安全规范 指南 常见五种意外防止规范 attilax总结
    数据安全 密码学原理与概论
    Atitit 理财之道分期与利率的比较列表 attilax总结
    Atitit 完整知识体系表 attilax总结 要读哪些书
    Atitit 为什么互联网机会这么大
    Atitit 建立新组织集团模型的框架基本制度与一些原则
  • 原文地址:https://www.cnblogs.com/wang1001/p/9756134.html
Copyright © 2011-2022 走看看