JAVA中的IO操作

来源:互联网 发布:炫浪网络在线阅读 编辑:程序博客网 时间:2024/04/30 14:52

java的常用输入、输出流
    其实都是继承自4个抽象类,分别是
    基于单字节的InputStream,OutputStream类
    基于双字节的Unicode代码单元的 Reader, Writer类
    一旦打开输入流后,程序就可从输入流串行地读数据。
从输入流读数据的过程一般如下:
open a stream
while more information
    read information
close the stream

类似地,程序也能通过打开一个输出流并顺序地写入数据来将信息送至目的端。
往输出流写数据的过程一般如下:
open a stream
while more information
    write information
close the stream

    java.io包中的stream类根据它们操作对象的类型是字符还是字节可分为两大类: 字符流和字节流。 
    InputStream,OutputStream类仅仅读取和写入单个的字节和字节数组,它们没有读取和写入字符串和数值的方法。 由于以字节为单位的流处理存储为Unicode码的信息很不方便(Unicode的每个代码单元使用了两个字节),所以有了一个专门的类层次来处理Unicode字符,这些类继承于抽象类Reader和Writer。

1.1 以字节为导向的stream 
    字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种类型:
1.inputstream:
1) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
2) StringBufferInputStream:把一个String对象作为InputStream   ---已过时。 此类未能正确地将字符转换为字节。从 JDK 1.1 开始,从字符串创建流的首选方法是通过 StringReader 类进行创建。
3) FileInputStream:把一个文件作为InputStream,实现对文件的读取操作
4) PipedInputStream:实现了pipe的概念,主要在线程中使用
5) SequenceInputStream:把多个InputStream合并为一个InputStream
2.Outstream
1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中
2) FileOutputStream:把信息存入文件中
3) PipedOutputStream:实现了pipe的概念,主要在线程中使用
4) SequenceOutputStream:把多个OutStream合并为一个OutStream
1.2 以Unicode字符为导向的stream
     以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。以Unicode字符为导向的stream包括下面几种类型:
1.InputStream
1) CharArrayReader:与ByteArrayInputStream对应
2) StringReader:与StringBufferInputStream对应
3) FileReader:与FileInputStream对应
4) PipedReader:与PipedInputStream对应
2.OutStream
1) CharArrayWrite:与ByteArrayOutputStream对应
2) StringWrite:无与之对应的以字节为导向的stream
3) FileWrite:与FileOutputStream对应
4) PipedWrite:与PipedOutputStream对应
    以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,字是在操作时的导向不同。如CharArrayReader和ByteArrayInputStream的作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。
1.3 两种不限导向的stream之间的转换
    InputStreamReader和OutputStreamReader:把一个以字节为导向的stream转换成一个以字符为导向的stream。
InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集
OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。

 

2. stream添加属性
2.1 “为stream添加属性”的作用
运用上面介绍的Java中操作IO的API,我们就可完成我们想完成的任何操作了。但通过FilterInputStream和FilterOutStream的子类,我们可以为stream添加属性。下面以一个例子来说明这种功能的作用。
如果我们要往一个文件中写入数据,我们可以这样操作:
FileOutStream fs = new FileOutStream(“test.txt”);
然后就可以通过产生的fs对象调用write()函数来往test.txt文件中写入数据了。但是,如果我们想实现“先把要写入文件的数据先缓存到内存中,再把缓存中的数据写入文件中”的功能时,上面的API就没有一个能满足我们的需求了。但是通过FilterInputStream和FilterOutStream的子类,为FileOutStream添加我们所需要的功能。

2.2 FilterInputStream的各种类型
2.2.1 用于封装以字节为导向的InputStream
1) DataInputStream:从stream中读取基本类型(int、char等)数据。
2) BufferedInputStream:使用缓冲区
3) LineNumberInputStream:会记录input stream内的行数,然后可以调用getLineNumber()和setLineNumber(int)
4) PushbackInputStream:很少用到,一般用于编译器开发
2.2.2 用于封装以字符为导向的InputStream
1) 没有与DataInputStream对应的类。除非在要使用readLine()时改用BufferedReader,否则使用DataInputStream
2) BufferedReader:与BufferedInputStream对应
3) LineNumberReader:与LineNumberInputStream对应
4) PushBackReader:与PushbackInputStream对应
2.3 FilterOutStream的各种类型
2.2.3 用于封装以字节为导向的OutputStream
1) DataIOutStream:往stream中输出基本类型(int、char等)数据。
2) BufferedOutStream:使用缓冲区
3) PrintStream:产生格式化输出
2.2.4 用于封装以字符为导向的OutputStream
1) BufferedWrite:与BufferedOutStream对应
2) PrintWrite:与 PrintStream对应


3. RandomAccessFile
1) 可通过RandomAccessFile对象完成对文件的读写操作
2) 在产生一个对象时,可指明要打开的文件的性质:r,只读;w,只写;rw可读写
3) 可以直接跳到文件中指定的位置

4. I/O应用的一个例子
import java.io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1.以行为单位从一个文件读取数据

BufferedReader in = new BufferedReader(new FileReader("F:\\java\\TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
in.close();

//1b. 接收键盘的输入

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());

//2. 从一个String对象中读取数据

StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();

//3. 从内存取出格式化输入
//把内存中的一个缓冲区作为DataInputStream使用

try{
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.println((char)in3.readByte());
}catch(EOFException e){
System.out.println("End of stream");
}

//4. 输出到文件

try{
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("F:\\java\\ TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
} catch(EOFException ex){
ystem.out.println("End of stream");
}

//5. 数据的存储和恢复

try{
DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream( new FileOutputStream("F:\\java\\ Data.txt")));
out2.writeDouble(3.1415926);
out2.writeChars("\nThas was pi:writeChars\n");
out2.writeBytes("Thas was pi:writeByte\n");
out2.close();
DataInputStream in5 = new DataInputStream( new BufferedInputStream(new FileInputStream("F:\\java\\ Data.txt")));
BufferedReader in5br = new BufferedReader( new InputStreamReader(in5));
System.out.println(in5.readDouble());
System.out.println(in5br.readLine());
System.out.println(in5br.readLine());
} catch(EOFException e){
System.out.println("End of stream");
}

//6. 通过RandomAccessFile操作文件
//通过RandomAccessFile类对文件进行操作。
RandomAccessFile rf =new RandomAccessFile("F:\\java\\ rtest.dat", "rw");
for(int i=0; i<10; i++)
rf.writeDouble(i*1.414);
rf.close();

rf = new RandomAccessFile("F:\\java\\ rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();

rf = new RandomAccessFile("F:\\java\\ rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();

rf = new RandomAccessFile("F:\\java\\ rtest.dat", "r");
for(int i=0; i<10; i++)
ystem.out.println("Value " + i + ":" + rf.readDouble());
rf.close();
}
}

 

再一个例子:

 

package com.mengya.TestIO;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;

public class TestIO {

 
 
 public void CreateFile(){
  File f=new File("e:\\io.txt");
  if(!f.exists()){
   try {
    f.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 
 public void set1(){
  try {
   FileOutputStream f=new FileOutputStream("e:\\io.txt");
   String str="我的未来不是梦";
   byte[] b=str.getBytes();
   try {
    f.write(b);
    f.flush();
    f.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  
 }
 
 public void get1(){
  try {
   FileInputStream f=new FileInputStream("e:\\io.txt");
   byte[] b=new byte[200];
   try {
    int n=f.read(b);
    String str=new String(b,0,n);
    System.out.println(str);
   } catch (IOException e) {
    e.printStackTrace();
   }
  
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  
 }
 
 public void set2(){
  try {
   FileOutputStream f=new FileOutputStream("e:\\io.txt",true);//如果没true则复盖原来的文件,加了true则添加原文件后面
   BufferedOutputStream ff=new BufferedOutputStream(f);
   String str=new String("我的心跟着希望在动");
   try {
    ff.write(str.getBytes());
    ff.flush();
    ff.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 
 }
 
 public void get2(){
  try {
   FileInputStream f=new FileInputStream("e:\\io.txt");
   BufferedInputStream ff=new BufferedInputStream(f);
   byte[] b=new byte[200];
   try {
    int n = ff.read(b);
    String str=new String(b,0,n);
    System.out.println(str);
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 
 
 public void set3(){
  try {
   FileWriter f=new FileWriter("e:\\io.txt",true);
   f.write("\r\n我在佛前苦苦求了几千前");//\r\n表示换行
   f.flush();
   f.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public void get3(){
  try {
   FileReader f=new FileReader("e:\\io.txt");
   char[] c=new char[200];
   try {
    int n = f.read(c);
    String str=new String(c,0,n);
    System.out.println(str);
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 public void set4(){
  try {
   FileWriter f=new FileWriter("e:\\io.txt",true);
   BufferedWriter ff=new BufferedWriter(f);
   ff.write("希望能感动上天");
   ff.flush();
   ff.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 
 }
 public void get4(){
  try {
   FileReader f=new FileReader("e:\\io.txt");
   BufferedReader ff=new BufferedReader(f);
   char[] c=new char[200];
   int n;
   try {
    n = ff.read(c);
    String str=new String(c,0,n);
    System.out.println(str);
    ff.close();
    f.close();
   } catch (IOException e) {
    e.printStackTrace();
    
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 
 }
 
 public void h1(){
  try {
   FileOutputStream f=new FileOutputStream("e:\\io.txt",true);
   OutputStreamWriter ff=new OutputStreamWriter(f);
   try {
    ff.write("\r\n希望你能够出现在我面前");
    ff.flush();
    ff.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 public void h2(){
  try {
   FileInputStream f=new FileInputStream("e:\\io.txt");
   InputStreamReader ff=new InputStreamReader(f);
   char[] c=new char[200];
   int n;
   try {
    n = ff.read(c);
    String str=new String(c,0,n);
    System.out.println(str);
    ff.close();
    f.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 
 public void hh(){
  try {
   FileReader f=new FileReader("e:\\io.txt");
   BufferedReader ff=new BufferedReader(f);
   while(ff.ready()){
    System.out.println(ff.readLine());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {
  TestIO test=new TestIO();
  test.CreateFile();
  //test.set1();
  //test.get1();
  //test.set2();
  //test.get2();
  //test.set3();
  //test.get3();
  //test.set4();
  //test.get4();
  //test.h1();
  //test.h2();
  test.hh();
 }

}

原创粉丝点击