欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java-IO

程序员文章站 2022-06-02 23:29:01
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; ... ......
 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\t%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 }