首页 > 其他 > 详细

流、文件及基于文本的应用

时间:2015-10-02 23:51:19      阅读:419      评论:0      收藏:0      [点我收藏+]

1 输入输出流

1.1 定义

  • 一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。
  • 按流的方向可以分为输入流、输出流
  • 按流的载体可以分为字节流、字符流
  • 按流的流向位置分为节点流、处理流

流的工作示意图

技术分享

输入流与输出流的类层次图

技术分享

1.2 字节流与字符流

  字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer

 

 

 

1.2.1 InputStream类

  • 逐字节地以二进制的原始方式读取数据
  •  public int read(); 读入一个字节,-1表示无
  •  public int read(byte b[]); 返回读入的字节数
  • public int read(byte[] b, int off, int len);

1.2.2 OutputStream类

  • 它的功能是将字节写入流中
  • public void write (int b);// 将参数b的低位字节写入到输出流
  • public void write (byte b[]);// 将字节数组b[]中的全部字节顺序写入到输出流
  • public void write(byte[] b, int off, int len);// 将字节数组b[]中从off开始的len个字 节写入到流中
  • public void flush (); 刷新缓存,实际写入到文件、网络
  • public void close(); 关闭流

1.2.3 Reader类

  • 与InputStream类相似,都是输入流 ,但差别在于Reader类读取的是字符(char),而不是字节。
  • public int read(); //需要将int转成char
  • public int read(char b[]);
  • public int read(char[] b, int off, int len);

1.2.4 Writer类

  • 与OutputStream类相似,都是输出流,但差别在于Writer类写入的是字符(char),而不是字节。
  • public void write (int b);// 将参数b的低两字节写入到输出流 
  • public void write (char b[]);// 将字符数组b[]中的全部字节顺序写入到输出流
  • public void write(char[] b, int off, int len);// 将字节数组b[]中从off开始的len个字节写入到流中
  • public void write( String s);// 将字符串写入流中
  • public void write( String s, int off, int len);// 将字符串写入流中, off为位置,len为长度
  • public void flush ();// 刷新流
  • public void close();// 关闭流

1.3 节点流和处理流

1.3.1 节点流

  • 定义:可以从或向一个特定的地方读写数据
  • 常见节点流
节点类型 字节流 字符流
File文件

FileInputStream/FileOutputStream

FileReader/FileWriter
Memory Array ByteArrayInputStream/ByteArrayOutputStream ByteArrayReader/ByteArrayWriter
Memory String   StringReader/StringWriter
Pipe PipeInputStream/PipeOutputStream PipeReader/PipeWriter

 

 

 

 

 

1.3.2 处理流

  • 定义:是对一个已存在的流的连接和封装(如缓冲、组装成对象,等等)
  • 常见处理流
处理类型 字节流 字符流
Buffering BufferedInputStream/BufferedOutputStream BufferedReader/BufferedWriter
Filtering FilterInputStream/FilterOutputStream FilterReader/FilterWriter
字节流转化为字符流   InputStreamReader/OutputStreamWriter
Object serialization ObjectInputStream/ObjectOutputStream  
基本数据类型转换 DataInputStream/DataOutputStream  
行号处理 LineNumberInputStream LineNumberReader
Peeking ahead可回退流 PushbackInputStream PushbackReader
printing 可显示处理 PrintStream PrintWriter

 

 

 

 

 

 

 

1.4 不同内容的读写

1.4.1标准输入与标准输出(从控制台输入与控制台输出)

  • 标准输入:System.in(InputStream类型)
  • 从标准输入中读取内容:为了使用方便,经常将System.in用各种处理流进行封装处理

    例如:BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );

       br.readLine();

  • 从控制台读取多字符用 int read() throws IOException----此处int需要强制转换为char
  • 从控制台读取字符串用 String readLine() throws IOException
  • 标准输出:System.out(PrintStream类),System.err(PrintStream类型)

1.4.2 二进制流读写

技术分享
import java.io.*;

public class Dump {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
        {
            dump( new FileInputStream("aaa.bmp"),
                  new FileOutputStream("bbb.bmp"));
        }
        catch(FileNotFoundException fex)
        {
            fex.printStackTrace();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
    
    public static void dump(InputStream src, OutputStream dest) throws IOException{
        BufferedInputStream input = new BufferedInputStream(src);
        BufferedOutputStream output = new BufferedOutputStream(dest);
        int length =-1;
        byte[] data = new byte[1024];
        while((length = input.read(data,0,1024)) != -1){
            output.write(data,0,length);
        }
        input.close();
        output.close();
    }

}
View Code

1.4.2 文件读写

  • FileInputStream一定要注意编码方式:UTF-8, ASCII, GB2312, 默认编码

流、文件及基于文本的应用

原文:http://www.cnblogs.com/penghuster/p/4852893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!