java IO

来源:互联网 发布:java 配置文件冲突 编辑:程序博客网 时间:2024/06/05 07:44
 

流:一组有序的数据序列。

File类:文件管理

(1)File f = new File("...");    

(2)f.createNewFile();    创建文件

(3)f.isDirectory();        是否是目录

(4)f.getCanonicalPath();    返回路径

(5)f.exists();        是否存在

(6)f.isFile();        f是否是文件

(7)File.separator;    分隔符

(8)f.mkdir();    如果f是目录,则创建这个目录

(9)f.listFiles();     返回子目录的文件对象

(10)f.delete();     删除文件

代码示例:遍历某个目录的所有子目录:

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2. public class StreamDemo05{  
  3.     public static void main(String args[])throws Exception{  
  4.         File f = new File("E:"+File.separator+"Core Java 1 practice"+File.separator+"StreamProject");  
  5.         list(f);  
  6.     }  
  7.   
  8.     public static void list(File f){  
  9.         File[] files = f.listFiles();  
  10.         for(File file:files){  
  11.             System.out.println(file);   
  12.             if(file.isDirectory()){  
  13.                 list(file);  
  14.             }  
  15.               
  16.         }  
  17.   
  18.     }  
  19. }  


InputStream和OutputStream:所有字节流的父类

InputStream提供了以下常用方法:

(1)int read();                        读取1个字节数据,然后返回0-255范围的int值,读取完为-1

注意:这里read方法中的byte转换成int和一般类型转换的byte转int是不同的,这里的int范围一定要在0-255之间。

(2)int read(byte[]b);                读取流中数据写入字节数组

(3)int read(byte[]b,int off,int len)    读取len长度的字节数,写入b的off开始的字节数组中

注意:如果考虑效率方面,则可以使用(2)(3)的方法,因为能够批量读取。

(4)void close();        关闭流

(5)int available();    返回此时在流中可读的字节数

(6)void skip(long n);    跳过n个字节

OutputStream提供了如下方法:

(1)write(int b);            将b先转型成byte即截取低八位字节,并写入输出流,例如如果b=257,则实际写入的只是1

(2)write(byte[]b);

(3)write(byte[]b,int off,int len);

(4)void flush();

(5)void close();

view plaincopy to clipboardprint?
  1. /* 
  2. 本程序测试的是InputStream和OutputStream提供的read和write方法 
  3. write(int ) 
  4. */  
  5.   
  6. import java.io.FileOutputStream;  
  7. import java.io.FileInputStream;  
  8. public class StreamDemo02{  
  9.     public static void main(String args[])throws Exception{  
  10.         FileOutputStream out = new FileOutputStream("1.txt");  
  11.         int i = 257;  
  12.         out.write(i);  
  13.         out.close();  
  14.         FileInputStream in = new FileInputStream("1.txt");  
  15.         int j = in.read();  
  16.         System.out.println(j);   
  17.         in.close();  
  18.     }  
  19. }  


ByteArrayInputStream和ByteArrayOutputStream:字节数组输入流

数据源是字节数组,

ByteArrayInputStream(byte[] buf, int offset, int length)
ByteArrayInputStream(byte[] buf)
以上的buf就是这个输入流的数据源。

ByteArrayOutputStream out = newByteArrayOutputStream();

byte[] b = out.toByteArray();获得输出流的字节数组。

当然然后可以通过:

ByteArrayInputStream in = new ByteArrayInputStream(b);把写入的字节数组导入输出流

 

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2. public class  ByteArrayInputStreamDemo01{  
  3.     public static void main(String args[])throws Exception{  
  4.         byte[]b = {-1,0,1};  
  5.         ByteArrayInputStream in = new ByteArrayInputStream(b);  
  6.         int data=0;  
  7.         while((data=in.read())!=-1){  
  8.             byte tmp = (byte)data;  
  9.             System.out.println(tmp);   
  10.         }  
  11.         in.close();  
  12.     }  
  13. }  

PipedInputStream和PipedOutputStream:管道流

管道输入流从特定的管道输出流中读取数据,read( )方法在没有数据可读的情况下会阻塞并等待,直到有数据可读才继续读。

PipedOutputStream out = new PipedOutputStream();

PipedInputStream in  = new PipedInputStream(out);

就建立好了连接。

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2. import java.util.*;  
  3. public class PipedInputStreamDemo{  
  4.     public static void main(String args[]){  
  5.         Sender sender = new Sender();  
  6.         Receiver receiver = new Receiver(sender);  
  7.         Thread t1 = new Thread(sender);  
  8.         Thread t2 = new Thread(receiver);  
  9.         t1.start();  
  10.         t2.start();  
  11.     }  
  12. }  
  13. class Sender implements Runnable{  
  14.     private PipedOutputStream out = new PipedOutputStream();  
  15.     public void run(){  
  16.         try{  
  17.             for(int i=-127;i<=128;i++){  
  18.                 out.write(i);  
  19.                 Thread.sleep(2000);  
  20.             }  
  21.             out.close();  
  22.         }  
  23.         catch(Exception e){  
  24.             e.printStackTrace();  
  25.         }  
  26.     }  
  27.     public PipedOutputStream getStream(){  
  28.         return out;  
  29.     }  
  30.   
  31. }  
  32. class Receiver implements Runnable{  
  33.     private PipedInputStream in;  
  34.     public Receiver(Sender sender){  
  35.         try{  
  36.             in = new PipedInputStream(sender.getStream());  
  37.         }  
  38.         catch(Exception e){  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  
  42.     public void run(){  
  43.         try{  
  44.             int data;  
  45.             while((data=in.read())!=-1){  
  46.                 System.out.println(data);  
  47.             }  
  48.             in.close();  
  49.         }  
  50.         catch(Exception e){  
  51.             e.printStackTrace();  
  52.         }  
  53.     }  
  54. }  


SequenceInputStream:顺序流

将多个输入流合并作为一个输入流。

SequenceInputStream(InputStream in1,InputStream in2); 按照in1和in2的顺序读取,外界看来好像一个输入流。

 

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2.   
  3. public class SequenceInputStreamDemo01{  
  4.     public static void main(String args[])throws Exception{  
  5.         ByteArrayInputStream in1 = new ByteArrayInputStream("x".getBytes());  
  6.         ByteArrayInputStream in2 = new ByteArrayInputStream("zg".getBytes());  
  7.         SequenceInputStream in = new SequenceInputStream(in1,in2);  
  8.         int data = 0;  
  9.         while((data=in.read())!=-1){  
  10.             System.out.println(data);   
  11.         }  
  12.         in.close();  
  13.   
  14.     }  
  15. }  

FileInoutStream和FileOutputStream:文件流

(1)  FileInputStream in = new FileInputStream("1.txt");
(2)  FileOutputStream out = new FileOutputStream("1.txt");

特点:在InputStream和OutputStream的基础上增加了文件读取,但是只能读取字节或字节数组。

还可以设置追加数据。

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2.   
  3. public class StreamDemo03{  
  4.     public static void main(String args[])throws Exception{  
  5.         FileOutputStream fos = new FileOutputStream("1.txt");  
  6.         DataOutputStream out = new DataOutputStream(fos);  
  7.         out.writeBytes("abc");  
  8.         fos.close();  
  9.         out.close();  
  10.   
  11.         FileInputStream fin = new FileInputStream("1.txt");  
  12.         DataInputStream in = new DataInputStream(fin);  
  13.         byte[]b = new byte[1024];  
  14.         int len = in.read(b);  
  15.         String str = new String(b,"UTF-8");  
  16.         String result = str.trim();  
  17.         System.out.println(result);   
  18.         in.close();  
  19.         fin.close();  
  20.     }  
  21. }  

代码2:拷贝文件

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2. public class Copy{  
  3.     public static void main(String args[])throws Exception{  
  4.         FileInputStream in = null;  
  5.         FileOutputStream out = null;  
  6.         in = new FileInputStream(args[0]);  
  7.         out = new FileOutputStream(args[1]);  
  8.         int tmp = 0;  
  9.         while((tmp=in.read())!=-1){  
  10.             out.write(tmp);  
  11.         }  
  12.         out.close();  
  13.         in.close();  
  14.     }  
  15. }  


FilterInputStream:过滤输入流

主要应用装饰器类,用于增加一些功能。

BufferedInputStream和BufferedOutputStream:缓冲流

作为过滤流的中间层,用以手动构建缓冲区。

DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("1.txt")));

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("1.txt")));

Output:先将数据写入缓冲区,当缓冲区满时,写入数据汇。

因此必须使用完就close。

DataInputStream和DataOutputStream:读取数值类型流

实现了DataInput和DataOutput接口的类。 

DataInput提供了如下方法:

(1)Xxx readXxx();读取基本数据类型

(2)int read(byte[]b);读取至字节数组中,返回实际读取的长度

(3)readChar()读取一个字符即两个字节

(4)String readUTF();

注意:不能使用readLine()方法!因为已过时。

DataOutput提供了如下方法:

(1)void wrtieXxx(Xxx )写入基本数据类型

(2)void writeBytes(String)能够以字节方式写入String,随后可以用read读取。

(3)void writeChars(String)以字符方式入,一个字符是两个字节

(4)void writeUTF(String );    

Unicode统一采用2个字节编码,UTF-8是Unicode的改进,原本ASCII码的字符还是一个字节。

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2.   
  3. public class DataInputStreamDemo01{  
  4.     public static void main(String args[])throws Exception{  
  5.         DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));  
  6.         out.writeUTF("我ai中国");  
  7.         out.close();  
  8.         DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data.txt")));  
  9.         String str = in.readUTF();  
  10.         System.out.println(str);  
  11.         in.close();  
  12.   
  13.     }  
  14. }  


PrintStream:打印流    //装饰器

PrintStream out = new PrintStream(OutputStream o,boolean autoflush);

out.print(Xxx);

out.println(Xxx);

PushBackInputStream:具有回退的功能。

ObjectInputStream和ObjectOutputStream:对象流

实现了DataInput和DataOutput接口

对于对象:

(1)writeObject(Object);

(2)Object readObject();    读取时需要强制类型转换

对于基本类型:使用readXxx和writeXxx方法

过滤流 :分层思想

由于一种单一的流的功能是有限的,因此如果能够把多个流结合起来,则会拓宽功能。

RandomAccessFile:随机存取

实现了DataInput和DataOutput接口。

提供了如下方法:

(1)RandomAccessFile raf = new RandomAccessFile("file","r或者rw");

(2)raf.seek(long pos);        把文件指针设定在pos处

(3)long raf.getFilePointer():返回文件指针

(4)long raf.length();返回长度

(5)raf.skipBytes(long);

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2. import java.util.*;  
  3. public class StreamDemo04{  
  4.     public static void main(String args[])throws Exception{  
  5.         RandomAccessFile out = new RandomAccessFile("1.txt","rw");  
  6.         Employee[]e = new Employee[3];  
  7.         e[0] = new Employee("张三",1000.0,1991,2,1);  
  8.         e[1] = new Employee("xiazdong",2000.0,1991,8,12);  
  9.         e[2] = new Employee("李四",3000.0,1889,3,2);  
  10.         e[0].writeData(out);  
  11.         e[1].writeData(out);  
  12.         e[2].writeData(out);  
  13.         out.close();  
  14.         RandomAccessFile in = new RandomAccessFile("1.txt","r");  
  15.         in.seek(20*2+8+12);   
  16.         Employee result = new Employee();  
  17.         result.readData(in);  
  18.         System.out.println(result);   
  19.         in.close();  
  20.     }  
  21. }  
  22.   
  23. class Employee{  
  24.     /* 
  25.         name = 20; 
  26.         salary = 8; 
  27.         hireDay = 12; 
  28.     */  
  29.     private String name;  
  30.     private double salary;  
  31.     private Date hireDay;  
  32.     private static final int NAME_LENGTH = 20;  
  33.   
  34.     public Employee(){  
  35.       
  36.     }  
  37.     public Employee(String name,double salary,int year,int month,int day){  
  38.         while(name.length()<NAME_LENGTH){  
  39.             name +="\u0000";  
  40.         }  
  41.         if(name.length()>NAME_LENGTH){  
  42.             name = name.substring(0,NAME_LENGTH);  
  43.         }   
  44.         this.name = name;  
  45.         this.salary = salary;  
  46.         GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);  
  47.         this.hireDay = calendar.getTime();  
  48.     }  
  49.     public String getName(){  
  50.         return name;  
  51.     }  
  52.     public double getSalary(){  
  53.         return salary;  
  54.     }  
  55.     public Date getHireDay(){  
  56.         return hireDay;  
  57.     }  
  58.     public String toString(){  
  59.         return "name="+name.trim()+",salary="+salary;  
  60.     }  
  61.     public void writeData(DataOutput out)throws Exception{  
  62.         GregorianCalendar calendar = new GregorianCalendar();  
  63.         calendar.setTime(hireDay);  
  64.         int y = calendar.get(Calendar.YEAR);  
  65.         int m = calendar.get(Calendar.MONTH);  
  66.         int d = calendar.get(Calendar.DAY_OF_MONTH)+1;   
  67.         out.writeChars(name);  
  68.         out.writeDouble(salary);    //8   
  69.         out.writeInt(y);  
  70.         out.writeInt(m);  
  71.         out.writeInt(d);  
  72.     }  
  73.     public void readData(RandomAccessFile in) throws Exception{  
  74.           
  75.         StringBuffer n = new StringBuffer();  
  76.         for(int i=0;i<NAME_LENGTH;i++){  
  77.             n.append(in.readChar());  
  78.         }  
  79.         double s = in.readDouble();  
  80.         int y = in.readInt();  
  81.         int m = in.readInt();  
  82.         int d = in.readInt();  
  83.         String str = n.toString();  
  84.         System.out.println(str);   
  85.         GregorianCalendar calendar = new GregorianCalendar(y,m-1,d);  
  86.         this.hireDay = calendar.getTime();  
  87.         this.name = str;  
  88.         this.salary = s;  
  89.     }  
  90. }  


Reader和Writer:字符流的父类

一个字符是2个字节,在字符流中,每次读取的最小单位就是一个字符。

Writer:把内存中的Unicode字符转换成其他编码类型的字符,然后写到输出流

(1)write(String)    写入字符串

 

Reader:将输入流中采用其他编码类型的字符转换成Unicode字符。

(1)int read(char[]ch);    读取字符数组 然后用String.valaueOf(ch);转换成String

(2)char ch = (char)in.read();    每次读取一个字符

CharArrayReader和CharArrayWriter

CharArrayReader:字符数组为数据源

CharArrayReader in = new CharArrayReader(char[]ch);

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2.   
  3. public class CharArrayReaderDemo01{  
  4.     public static void main(String args[])throws Exception{  
  5.         char[]ch = {'a','b','我'};  
  6.         CharArrayReader in = new CharArrayReader(ch);  
  7.         int data;  
  8.         while((data=in.read())!=-1){  
  9.             System.out.println((char)data);   
  10.         }  
  11.     }  
  12. }  


CharArrayWriter:字符数组为数据汇

CharArrayWriter out = new CharArrayWriter();

out.write();

char[]ch = out.toCharArray();

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2. public class CharArrayWriterDemo{  
  3.     public static void main(String args[]){  
  4.         CharArrayWriter out = new CharArrayWriter();  
  5.         out.write('你');  
  6.         out.write('好');  
  7.         char[]ch = out.toCharArray();  
  8.         System.out.println(ch);   
  9.     }  
  10. }  


StringReader 和StringWriter

StringReader :字符串为数据源   

StringReader in = new StringReader(String);

StringWriter:以字符串为数据汇

StringWriter out = new StringWriter();

String str = out.toString();

InputStreamReader和InputStreamWriter

InputStreamReader:以字节流为数据源

InputStreamReader in2 = new InputStreamReader(new FileInputStream("Reader.txt"),"UTF-8");

以上语句指定输入流FileInputStream为数据源,并假定数据源是UTF-8编码,每次读取一个UTF-8字符,转换成Unicode字符。

通常InputStreamReader被BufferedReader包装。

InputStreamReader in1 = new InputStreamReader(new FileInputStream("writer.txt"),"UTF-8");
BufferedReader in = new BufferedReader(in1);

in.readLine();

OutputStreamWriter:以字节流为数据汇

把Unicode编码转换成特定的编码。

通常OutputStreamWriter被BufferedWriter,PrintWriter包装。

OutputStreamWriter out1 = new OutputStreamWriter(new FileOutputStream("writer.txt"),"UTF-8");

BufferedWriter bw = new BufferedWriter(out1);
PrintWriter out = new PrintWriter(bw,true);

out.println():

FileReader:InputStreamReader的子类,但是只能读取默认编码的数据文件。

FileReader in = new FileReader(String);

FileWriter:OutputStreamWriter的子类,只能写入默认编码的数据文件。

BufferedReader:让Reader能够有缓冲区

BufferedReader in = new BufferedReader(Reader i);
in.readLine();

PrintWriter和Scanner

PrintWriter写入文本数据

构造方法:

(1)PrintWriter(Writer out, boolean autoFlush) 
常用方法:

(1)print(X)    参数可以是基本数据类型,String

(2)println(X)    参数可以是基本数据类型,String

代码示例:写入和读取Employee对象

view plaincopy to clipboardprint?
  1. import java.util.Calendar;  
  2. import java.util.GregorianCalendar;  
  3. import java.util.Date;  
  4. import java.io.PrintWriter;  
  5. import java.io.BufferedReader;  
  6. import java.io.Writer;  
  7. import java.io.Reader;  
  8. import java.io.FileWriter;  
  9. import java.io.FileReader;  
  10. import java.util.StringTokenizer;  
  11.   
  12.   
  13. public class StreamDemo01{  
  14.     public static void main(String args[])throws Exception{  
  15.         Poxy p = new Poxy();  
  16.         p.write();    
  17.         p.read();  
  18.     }  
  19. }  
  20. class Poxy{   
  21.     private Employee[] emp;  
  22.       
  23.     public Poxy() throws Exception{  
  24.         Employee e[] = new Employee[3];  
  25.         System.out.println("***********创建人物***********");   
  26.         e[0] = new Employee("张三",1000.0,1991,8,12);  
  27.         e[1] = new Employee("李四",2000.0,1992,3,1);  
  28.         e[2] = new Employee("王五",3000.0,1990,1,1);   
  29.         emp = e;  
  30.     }  
  31.     public void write()throws Exception{  
  32.         PrintWriter out = null;  
  33.         out = new PrintWriter(new FileWriter("data.txt"),true);  
  34.         System.out.println("*********开始写入数据*********");   
  35.         emp[0].writeData(out);  
  36.         emp[1].writeData(out);  
  37.         emp[2].writeData(out);  
  38.         System.out.println("***********写入完毕**********");  
  39.         out.close();  
  40.     }  
  41.     public void read()throws Exception{  
  42.         System.out.println("**********开始读取***********");   
  43.         BufferedReader in = null;  
  44.         in = new BufferedReader(new FileReader("data.txt"));  
  45.         Employee[] result = new Employee[3];  
  46.         result[0] = new Employee();  
  47.         result[1] = new Employee();  
  48.         result[2] = new Employee();  
  49.         result[0].readData(in);  
  50.         result[1].readData(in);  
  51.         result[2].readData(in);  
  52.         System.out.println(result[0]);  
  53.         System.out.println(result[1]);  
  54.         System.out.println(result[2]);   
  55.         System.out.println("**********读取完毕***********");  
  56.         in.close();  
  57.     }  
  58.   
  59. }  
  60. class Employee{  
  61.     private String name;  
  62.     private double salary;  
  63.     private Date hireDay;  
  64.     public Employee(){  
  65.       
  66.     }  
  67.     public Employee(String name,double salary,int year,int month,int day){  
  68.         this.name = name;  
  69.         this.salary = salary;  
  70.         GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);  
  71.         this.hireDay = calendar.getTime();  
  72.     }  
  73.     public String getName(){  
  74.         return name;  
  75.     }  
  76.     public double getSalary(){  
  77.         return salary;  
  78.     }  
  79.     public Date getHireDay(){  
  80.         return hireDay;  
  81.     }  
  82.     public String toString(){  
  83.         return "name="+name+",salary="+salary;  
  84.     }  
  85.     public void writeData(PrintWriter out)throws Exception{  
  86.         GregorianCalendar calendar = new GregorianCalendar();  
  87.         calendar.setTime(getHireDay());  
  88.         int year = calendar.get(Calendar.YEAR);  
  89.         int month = (calendar.get(Calendar.MONTH)+1);  
  90.         int day = calendar.get(Calendar.DAY_OF_MONTH);  
  91.         out.println(this.getName()+"|"+this.getSalary()+"|"+year+"|"+month+"|"+day);  
  92.     }  
  93.     public void readData(BufferedReader in) throws Exception{  
  94.         String line = null;  
  95.         line = in.readLine();  
  96.         StringTokenizer token = new StringTokenizer(line,"|");   
  97.         this.name = token.nextToken();  
  98.         this.salary = Double.parseDouble(token.nextToken());  
  99.         int y = Integer.parseInt(token.nextToken());  
  100.         int m = Integer.parseInt(token.nextToken());  
  101.         int d = Integer.parseInt(token.nextToken());  
  102.         GregorianCalendar cal = new GregorianCalendar(y,m-1,d);  
  103.         this.hireDay = cal.getTime();  
  104.     }  
  105. }  


Scanner读取文本数据

构造方法:

(1)Scanner(InputStream source)
(2)Scanner(File source,String charsetName)

常用方法:

(1)nextXxx();读取基本类型

(2)nextLine(); 读取一行

(3)读取直到出现标记:

            useDelimiter(String str);用str作为标记

            String next();读取到下一个标记为止

字节流和字符流的区别:

字符流使用了缓存,所有内容存入缓冲区,需要刷新缓冲区,把所有内容输出。

字节流没有使用缓存,在字节流操作中,即使没有关闭,最终也会输出。

在所有硬盘上保存文件或是进行传输的时候都是以字节的方式进行的,包括图片都是字节完成,字符只有在内存中才会形成,所以字节是最多的。

边读边写的开发方式。

装饰器设计模式:

如果想要扩展一个流的功能一般采用继承的方式扩展,但是这种方式会使得流的层次更加复杂,因此有没有什么好办法呢?

装饰器的设计模式就解决了这个问题,只需要单单把扩展功能放在装饰器中,再提供了Decorator d = new Decorator(InputStream in)的构造方法即可。这样可以提高代码的重用性。

String类的常用方法:

getBytes("encode");比如getBytes("UTF-8");

String str = new String(byte[],"encode");

标准IO:

特点:是由jVM创造出来的,因此存在于程序的整个生命周期中。

(1)System.in:标准输入,默认为键盘。

(2)System.out:标准输出,默认为console.

(3)System.err:错误输出。

System.out和System.err的区别:

System.out打印的信息是专门给用户看的,而System.err的信息属于后台信息,不应该给用户看。

设置标准输入输出:

System.setIn(InputStream);

System.setOut(PrintStream);

InputStream in = new FileInputStream("test.txt");

PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("out.txt")));

 

view plaincopy to clipboardprint?
  1. import java.io.*;  
  2. public class Redirector{  
  3.     public static void main(String args[])throws Exception{  
  4.         InputStream standardIn = System.in;  
  5.         PrintStream standardOut = System.out;  
  6.         InputStream in = new FileInputStream("test.txt");  
  7.         PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("out.txt")));  
  8.         redirect(in,out);  
  9.         copy();  
  10.         in.close();  
  11.         out.close();  
  12.     }  
  13.     public static void redirect(InputStream in,PrintStream out){  
  14.         System.setIn(in);  
  15.         System.setOut(out);  
  16.     }  
  17.     public static void copy()throws Exception{  
  18.         InputStreamReader isr = new InputStreamReader(System.in);  
  19.         BufferedReader in = new BufferedReader(isr);    //包装System.in  
  20.         String line;  
  21.         while((line=in.readLine())!=null){  
  22.             System.out.println(line);   
  23.         }  
  24.         in.close();  
  25.     }  
  26. }  

 

原创粉丝点击