【转】输入输出流(三)

上一篇 / 下一篇  2017-05-24 16:46:45 / 个人分类:Java


4.文件输出流:FileOutputStream类

   作用:用来处理以文件作为数据输出目的数据流;或者说是从内存区读数据入文件

      FileOutputStream类用来处理以文件作为数据输出目的数据流;一个表示文件名的字符串,也可以是File或FileDescriptor对象。 
  创建一个文件流对象有两种方法: 
  方式1: 
   File   f=new  File (“d:/myjava/write.txt ");
        FileOutputStream  out= new FileOutputStream (f);
  方式2: 
  FileOutputStream ut=new FileOutputStream(“d:/myjava/write.txt "); 
  方式3:构造函数将 FileDescriptor()对象作为其参数。 
  FileDescriptor() fd=new FileDescriptor(); 
  FileOutputStream f2=new FileOutputStream(fd); 
  方式4:构造函数将文件名作为其第一参数,将布尔值作为第二参数。 
  FileOutputStream f=new FileOutputStream("d:/abc.txt",true); 
  注意: (1)文件中写数据时,若文件已经存在,则覆盖存在的文件;(2)的读/写操作结束时,应调用close方法关闭流。 

程序举例:使用键盘输入一段文章,将文章保存在文件write.txt中

  1. import java.io.IOException;  
  2. import java.io.FileOutputStream;  
  3. public class TestFile {  
  4.     public static void main(String args[]) throws IOException {  
  5.         try {  
  6.             System.out.println("please Input from      Keyboard");  
  7.             int count, n = 512;  
  8.             byte buffer[] = new byte[n];  
  9.             count = System.in.read(buffer);  
  10.             FileOutputStream wf = new FileOutputStream("d:/myjava/write.txt");  
  11.             wf.write(buffer, 0, count);  
  12.             wf.close(); // 当流写操作结束时,调用close方法关闭流。  
  13.             System.out.println("Save to the write.txt");  
  14.         } catch (IOException IOe) {  
  15.             System.out.println("File Write Error!");  
  16.         }  
  17.     }  
  18.   
  19. }  

5. FileInputStream流和FileOutputStream的应用

利用程序将文件file1.txt 拷贝到file2.txt中。
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.FileInputStream;  
  5.   
  6. public class TestFile {  
  7.     public static void main(String args[]) throws IOException {  
  8.         try {  
  9.             File inFile = new File("copy.java");  
  10.             File outFile = new File("copy2.java");  
  11.             FileInputStream finS = new FileInputStream(inFile);  
  12.             FileOutputStream foutS = new FileOutputStream(outFile);  
  13.             int c;  
  14.             while ((c = finS.read()) != -1) {  
  15.                 foutS.write(c);  
  16.             }  
  17.             finS.close();  
  18.             foutS.close();  
  19.         } catch (IOException e) {  
  20.             System.err.println("FileStreamsTest: " + e);  
  21.         }  
  22.     }  
  23.   
  24. }  

6. 缓冲输入输出流 BufferedInputStream/ BufferedOutputStream

        
      

       计算机访问外部设备非常耗时。访问外存的频率越高,造成CPU闲置的概率就越大。为了减少访问外存的次数,应该在一次对外设的访问中,读写更多的数据。为此,除了程序和流节点间交换数据必需的读写机制外,还应该增加缓冲机制。缓冲流就是每一个数据流分配一个缓冲区,一个缓冲区就是一个临时存储数据的内存。这样可以减少访问硬盘的次数,提高传输效率。

BufferedInputStream:当向缓冲流写入数据时候,数据先写到缓冲区,待缓冲区写满后,系统一次性将数据发送给输出设备。

BufferedOutputStream :当从向缓冲流读取数据时候,系统先从缓冲区读出数据,待缓冲区为空时,系统再从输入设备读取数据到缓冲区。

1)将文件读入内存:

BufferedInputStreamFileInputStream相接

  FileInputStream in=new  FileInputStream( “file1.txt ” );

  BufferedInputStream bin=new  BufferedInputStreamin); 

2)将内存写入文件:

BufferedOutputStream FileOutputStream相接

FileOutputStreamout=new FileOutputStream(“file1.txt”);

BufferedOutputStream  bin=new BufferedInputStream(out);


3)键盘输入流读到内存
BufferedReader标准的数据流相接 
 InputStreamReader sin=new InputStreamReader (System.in) ;
BufferedReader bin=new             BufferedReader(sin);
  1. import java.io.*;  
  2.   
  3. public class ReadWriteToFile {  
  4.     public static void main(String args[]) throws IOException {  
  5.         InputStreamReader sin = new InputStreamReader(System.in);  
  6.         BufferedReader bin = new BufferedReader(sin);  
  7.         FileWriter out = new FileWriter("myfile.txt");  
  8.         BufferedWriter bout = new BufferedWriter(out);  
  9.         String s;  
  10.         while ((s = bin.readLine()).length() > 0) {  
  11.             bout.write(s, 0, s.length());  
  12.         }  
  13.   
  14.     }  
  15. }  
程序说明:
从键盘读入字符,并写入到文件中BufferedReader类的方法:String readLine()
作用:读一行字符串,以回车符为结束。
BufferedWriter类的方法:bout.write(String s,offset,len)
作用:从缓冲区将字符串s从offset开始,len长度的字符串写到某处。

8. 字符流Writer/Reader

        Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。

1. Reader抽象类

    用于读取字符流的抽象类。子类必须实现的方法只有 read(char[], int, int) 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。

       

        1) FileReader :与FileInputStream对应  
           主要用来读取字符文件,使用缺省的字符编码,有三种构造函数: 
      (1)将文件名作为字符串 :FileReader f=new FileReader(“c:/temp.txt”); 
      (2)构造函数将File对象作为其参数。 
              File f=new file(“c:/temp.txt”); 
              FileReader f1=new FileReader(f); 
     (3)  构造函数将FileDescriptor对象作为参数 
            FileDescriptor() fd=new FileDescriptor() 
            FileReader f2=new FileReader(fd); 
               (1) 用指定字符数组作为参数:CharArrayReader(char[]) 
               (2) 将字符数组作为输入流:CharArrayReader(char[], int, int) 
          读取字符串,构造函数如下: public StringReader(String s); 
        2) CharArrayReader:与ByteArrayInputStream对应  
  3) StringReader : 与StringBufferInputStream对应 
  4) InputStreamReader 
        从输入流读取字节,在将它们转换成字符:Public inputstreamReader(inputstream is); 
  5) FilterReader: 允许过滤字符流 
        protected filterReader(Reader r); 
  6) BufferReader :接受Reader对象作为参数,并对其添加字符缓冲器,使用readline()方法可以读取一行。 
     Public BufferReader(Reader r); 

      主要方法:

      (1)  public int read() throws IOException; //读取一个字符,返回值为读取的字符 

  (2)  public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/ 
  (3)  public abstract int read(char cbuf[],int off,int len) throws IOException; 
  /*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/ 

2. Writer抽象类

     写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。 其子类如下:

    

      1) FileWrite: 与FileOutputStream对应  
  将字符类型数据写入文件,使用缺省字符编码和缓冲器大小。 
  Public FileWrite(file f); 
  
2)  chararrayWrite:与ByteArrayOutputStream对应 ,将字符缓冲器用作输出。 
      Public CharArrayWrite(); 
  3) PrintWrite:生成格式化输出 
      public PrintWriter(outputstream os); 
  4) filterWriter:用于写入过滤字符流 
      protected FilterWriter(Writer w); 
  5) PipedWriter:与PipedOutputStream对应   

      6) StringWriter:无与之对应的以字节为导向的stream  

      主要方法:

  (1)  public void write(int c) throws IOException; //将整型值c的低16位写入输出流 
  (2)  public void write(char cbuf[]) throws IOException; //将字符数组cbuf[]写入输出流 
  (3)  public abstract void write(char cbuf[],int off,int len) throws IOException; //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流 
  (4)  public void write(String str) throws IOException; //将字符串str中的字符写入输出流 
  (5)  public void write(String str,int off,int len) throws IOException; //将字符串str 中从索引off开始处的len个字符写入输出流 
  (6)  flush( ) //刷空输出流,并输出所有被缓存的字节。 
  (7)close()    关闭流 public abstract void close() throws IOException

3 .InputStream与Reader差别 OutputStream与Writer差别

InputStream和OutputStream类处理的是字节流,数据流中的最小单位是字节(8个bit)
Reader与Writer处理的是字符流,在处理字符流时涉及了字符编码的转换问题

  1. import java.io.*;  
  2. public class EncodeTest {  
  3.     private

TAG:

 

评分:0

我来说两句

Open Toolbar