`
foxjj123
  • 浏览: 5854 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论
阅读更多
IO(Input/Output)是计算机输出/输出的接口。Java中IO是以流为基础进行输入输出的,所有数据被串行化写入输出流,或者从输入流读入。此外,Java也对块传输提供支持,在核心库java.nio中采用的便是块IO。Java的IO模型设计使用Decorator模式,体系分Input/Output和Reader/Writer两类,区别在于Reader/Writer在读写文本时能自动转换内码。


java的IO包类分层结构:

java.lang.Object

┝java.io.File

┝java.io.InputStream

│ ┝java.io.FileInputStream

│ ┕java.io.FilterInputStream

│       ┝java.io.BufferedInputStream

│       ┕java.io.DataInputStream

┝java.io.OutputStream

│ ┝java.io.FileOutputStream

│ ┝java.io.FilterOutputStream

│ │    ┝java.io.BufferedOutputStream

│ │    ┕java.io.DataOutputStream

│ ┕java.io.PrintStream

┝java.io.RandomAccessFile

┝java.io.Reader

│ ┝java.io.BufferedReader

│ ┕java.io.InputStreamReader

│       ┕java.io.FileReader

┕java.io.Writer

    ┝java.io.BufferedWriter

    ┝java.io.OutputStreamWriter

    │   ┕java.io.FileWriter

    ┕java.io.PrintWriter


从输入流读数据的过程一般如下:
open a stream
while more information
    read information
close the stream

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


先来看看I/O应用的几个例子
import java.io.*;
public class TestIO{
public static void main(String[] args) throws IOException{
//以行为单位从一个文件读取数据
/*当读取文件时,先把文件内容读到缓存中,当调用in.readLine()时,
再从缓存中以字符的方式读取数据(以下简称“缓存字节读取方式”)。
*/
BufferedReader in = new BufferedReader(new FileReader("F:\\java\\TestIO.java"));
String s1, s2 = new String();
while((s1 = in.readLine()) != null)
s2 += s1 + "\n";
in.close();

// 接收键盘的输入
/*由于想以缓存字节读取方式从标准IO(键盘)中读取数据,所以要
先把标准IO(System.in)转换成字符导向的stream,再进行BufferedReader封装。
*/
BufferedReader kbin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(kbin.readLine());


// 从一个String对象中读取数据
/*
要以字符的形式从一个String对象中读取数据,所以要产生一个StringReader类型的stream。
*/
StringReader in = new StringReader(s);
int c;
while((c = in.read()) != -1)
System.out.println((char)c);
in.close();


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

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


// 输出到文件
/*对String对象s读取数据时,先把对象中的数据存入缓存中,再从缓冲中进行读取;对TestIO.out文件进行操作时,
先把格式化后的信息输出 到缓存中,再把缓存中的信息输出到文件中。
*/
try{
BufferedReader in = new BufferedReader(new StringReader(s));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("F:\\java\\ TestIO.out")));
int lineCount = 1;
while((s = in.readLine()) != null)
out.println(lineCount++ + ":" + s);
out.close();
in.close();
} catch(EOFException ex){
ystem.out.println("End of stream");
}


// 数据的存储和恢复
/*对Data.txt文件进行输出时,是先把基本类型的数据输出屋缓存中,再把缓存中的数据输出到文件中;对文件进行读取操作时,先把文件中的数据读取到缓存中,再从缓存中以基本类型的形式进行读取。注意in5.readDouble()这一行。因为写入第一个writeDouble(),所以为了正确显示。也要以基本类型的形式进行读取。
*/
try{
DataOutputStream out = new DataOutputStream(new BufferedOutputStream( new FileOutputStream("F:\\java\\ Data.txt")));
out.writeDouble(3.1415926);
out.writeChars("\nThas was pi:writeChars\n");
out.writeBytes("Thas was pi:writeByte\n");
out.close();
DataInputStream in = new DataInputStream( new BufferedInputStream(new FileInputStream("F:\\java\\ Data.txt")));
BufferedReader br = new BufferedReader( new InputStreamReader(in));
System.out.println(in.readDouble());
System.out.println(br.readLine());
System.out.println(br.readLine());
} catch(EOFException e){
System.out.println("End of stream");
}


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

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

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

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


InputStream的方法有:
read() 从流中读入数据有3中方式:
int read() 一次读一个字节
int read(byte[]) 读多个字节到数组中
int read(byte[],int off,int len) 指定从数组的哪里开始,读多长
skip() 跳过流中若干字节
available() 返回流中可用字节数,但基于网络时无效,返回0
markSupported() 判断是否支持标记与复位操作
mark() 在流中标记一个位置,要与markSupported()连用
reset() 返回标记过的位置
close() 关闭流


OutputStream 的方法:
write(int) 写一个字节到流中
write(byte[]) 将数组中的内容写到流中
write(byte[],int off,int len) 将数组中从off指定的位置开始len长度的数据写到流中
close() 关闭流
flush() 将缓冲区中的数据强制输出


File 类
File 可以表示文件也可以表示目录,File 类控制所有硬盘操作
构造器:
File(File parent,String child) 用父类和文件名构造
File(String pathname) 用绝对路径构造
File(String parent,String child) 用父目录和文件名构造
File(URI uri) 用远程文件构造
常用方法:
boolean createNewFile();
boolean exists();

//从本地文件D:/dzh2/data/sz/day.dat读取数据写入本地文件12.txt 
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.MalformedURLException;  
 
 
public class AsciiChart {  
 
     public static void main(String[] args) throws IOException {  
           
           //输入流  
            InputStream in =new FileInputStream("D:/dzh2/data/sz/day.dat");  
            //输出流  
            OutputStream out =new FileOutputStream("D:/12.txt",true);  
 
            try {  
                byte[] buffer=new byte[1024];  
                while(true){  
                    int byteRead=in.read(buffer);  
                    if(byteRead==-1)break;  
                    out.write(buffer,0,byteRead);  
                }               
            }  
              
            catch (MalformedURLException ex) {  
              System.err.println(args[0] + " is not a URL Java understands.");  
            }  
            finally {  
              if (in != null) in.close( );  
              if (out != null){  
                  out.close( );  
              }  
                    
            }  
          
          }  
 
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics