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

1 输入输出流

1.1 定义

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

流的工作示意图

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

1.2 字节流与字符流

 字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

 

 

 

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 ArrayByteArrayInputStream/ByteArrayOutputStreamByteArrayReader/ByteArrayWriter
Memory String StringReader/StringWriter
PipePipeInputStream/PipeOutputStreamPipeReader/PipeWriter

 

 

 

 

 

1.3.2 处理流

  • 定义:是对一个已存在的流的连接和封装(如缓冲、组装成对象,等等)
  • 常见处理流
处理类型字节流字符流
BufferingBufferedInputStream/BufferedOutputStreamBufferedReader/BufferedWriter
FilteringFilterInputStream/FilterOutputStreamFilterReader/FilterWriter
字节流转化为字符流 InputStreamReader/OutputStreamWriter
Object serializationObjectInputStream/ObjectOutputStream 
基本数据类型转换DataInputStream/DataOutputStream 
行号处理LineNumberInputStreamLineNumberReader
Peeking ahead可回退流PushbackInputStreamPushbackReader
printing 可显示处理PrintStreamPrintWriter

 

 

 

 

 

 

 

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, 默认编码

1.5 与输入输出相关的的类

1.5.1 文件类(File类)

  • java.io包中定义与数据输入、输出功能有关的类,包括提供文件操作功能的File类
  •  创建File类对象 有2种方法:f = new File("Test.java"); f = new File("E:\\ex\\","Test.java");
  •  在Java中,将目录(directory, 文件夹)也当作文件处理
  • File类中提供了实现目录管理功能的方法 File path = new File("E:\\ex\\"); File f = new File(path, "Test.java");

1.5.2 RandomAccessFile类

  • 类似于C语言的文件操作
  • RandomAccessFile,可以实现对文件的随机读写操作
  •  构造方法:RandomAccessFile(String name,String mode);RandomAccessFile(File f,String mode);
  • 定位方法 ppublic void seek(long pos);
  • 读写方法 preadBealoon(),readChar(),readInt(),readLong(),readFloat(),readDouble(), readLine(),readUTF()等 ;writeBealoon(),writeChar(),writeInt(),writeLong(),writeFloat(),writeDouble(), writeLine(),writeUTF()等

1.5.3 遍历整个目录

  • 新建目录:boolean mkdir();返回目录字符串:String[] list()
  • 示例:使用递归列出所有文件
import java.io.*;


public class ListAllFiles {
    public static void main(String[] arg){
        ListFiles( new File( "E:\\博客图片"));
    }
    
    public static void ListFiles(File dir){
        if( !dir.exists() || ! dir.isDirectory() ) return;
        String[] files = dir.list();
        for(int i=0; i<files.length; i++){
            File file = new File(dir, files[i]);
            if(file.isFile()){
                System.out.println(dir + "\\" + file.getName() + "\t" + file.length());
            }
            else{
                System.out.println(dir + "\\" + file.getName() + "\t<dir>" );
                ListFiles(file);
            }
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/penghuster/p/4852893.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值