IO

 
 
IO
 
  JAVA 的核心库JAVA.IO 提供了全面的 IO 接口,包括:文件读写,标准设备输出等等。 Java IO 是以流为基础进行输入输出的,所有数据被串行化写入输出流,或者从输入流读入。在具体使用中很多初学者对 Java.io 包的使用非常含糊,本文将详细解说关于 Java.io 的使用。

  一. Input和Output

   1. (stream) 代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在 JAVA IO 系统中,所有的流 (stream) (包括输入流 (Input stream) 和输出流 (Out stream) )都包括两种类型:字节流和字符流。

  字节流:包括输入字节流和输出字节流

  以字节为导向的 stream ,表示以字节为单位从 stream 中读取或往 stream 中写入信息。以字节为导向的 stream 包括下面几种类型: 
输入字节流(从字节流中读取):
   1) ByteArrayInputStream :把内存中的一个缓冲区作为 InputStream 使用

   2) FileInputStream :把一个文件作为 InputStream ,实现对文件的读取操作

   3) PipedInputStream :实现了 pipe 的概念,主要在线程中使用

   4) SequenceInputStream :把多个 InputStream 合并为一个 InputStream
 
5)DataInputStream :读二制文件

   6) StringBufferInputStream :把一个 String 对象作为 InputStream


 
输出字节流(向字节流中写入):

   1) ByteArrayOutputStream :把信息存入内存中的一个缓冲区中

   2) FileOutputStream :把信息存入文件中

   3) PipedOutputStream :实现了 pipe 的概念,主要在线程中使用

   4) SequenceOutputStream :把多个 OutStream 合并为一个 OutStream
 
5)DataOutputtStream :写二制文件

  字符流:包括输入字符流和输出字符流。

  以 Unicode 字符为导向的 stream ,表示以 Unicode 字符为单位从 stream 中读取或往 stream 中写入信息。以 Unicode 字符为导向的 stream 包括下面几种类型:

  输入字符流(从字符流中读取):

   1) CharArrayReader :与 ByteArrayInputStream 对应

   2) StringReader :与 StringBufferInputStream 对应

   3) FileReader :与 FileInputStream 对应

   4) PipedReader :与 PipedInputStream 对应
 
5) BufferedReader :通过字符流方式读取文件,并使用缓冲区,提高了读文件的效率。

  输出字符流(向字符流中写入):

   1) CharArrayWriter :与 ByteArrayOutputStream 对应

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

   3) FileWriter :与 FileOutputStream 对应

   4) PipedWriter :与 PipedOutputStream 对应
 
5) BufferedWriter :以字符流方式并通过缓冲区把数据写入文件,提高了读文件的效率。

   以字符为导向的 stream 基本上有与之相对应的以字节为导向的 stream 。两个对应类实现的功能相同,字是在操作时的导向不同。如 CharArrayReader :和 ByteArrayInputStream 的作用都是把内存中的一个缓冲区作为 InputStream 使用,所不同的 是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。
 

// 字节流读写示例:
         
// 字节流读操作
        BufferedReader stdin = new  BufferedReader( new  InputStreamReader(System.in));
        System.out.println(
" Enter a line " );
        String in
= stdin.readLine();
        
        File file
= new  File(in);
        System.out.println(in
+ " 的大小为:  " + file.length());
        
        
// 读取字节
        FileInputStream fis = new  FileInputStream(in);
        System.out.println(
" 可读取的字节:  " + fis.available());
        
char [] text = new   char [fis.available()];
        
for ( int  count = 0 ;count < fis.available();count ++ ) {
            text[count]
=((char)fis.read());
            System.out.print(text[count]);
        }

        fis.close();
        
        
// 字节流写操作
        FileOutputStream fos = new  FileOutputStream(in);
        String str
= " 好好学习Java " ;
        
byte [] bytes = str.getBytes();
        fos.write(bytes, 
0 , bytes.length);
        fos.close();
    }

// 二进制数据读写示例:
        
// 二进制数据读操作
        BufferedReader stdin = new  BufferedReader( new  InputStreamReader(System.in));
        System.out.println(
" Enter a line " );
        String in
= stdin.readLine();
        
        File file
= new  File(in);
        System.out.println(in
+ " 的大小为:  " + (file.length() / 1024 ) + " KB " );
        
        
// 读取二进制数据读
        
// 创建FileInputStream对象
        FileInputStream fis = new  FileInputStream(in);
        
        
// 通过dis创建DataInputStream对象
        DataInputStream dis = new  DataInputStream(fis);
        
        
// 得到二进制数据
         byte [] words = new   byte [dis.available()];
        dis.readFully(words, 
0 , dis.available());
        
for ( int  i = 0 ;i < words.length;i ++ ) {
            System.out.print(words[i]);
            
//得到的二进制数据转成字符
            System.out.println((char)words[i]);
        }

        fis.close();
        dis.close();
        
        
        
// 写二进制数据
        
// 把图片信息转成二进制数据.再存到log.class中
        FileInputStream fisp = new  FileInputStream( " c:/pic.ico " );
        DataInputStream disp
= new  DataInputStream(fisp);
        
        FileOutputStream fosp
= new  FileOutputStream(in);
        DataOutputStream dosp
= new  DataOutputStream(fosp);
        
int  temp;
        
while ((temp = disp.read()) !=- 1 ) {
            dosp.write(temp);
        }

        fisp.close();
        disp.close();
        fosp.close();
        dosp.close();
        
        
// 查看当前log.class大小
        File f = new  File(in);
        System.out.println(
" 当前log.class大小:  " + (f.length() / 1024 ) + " KB " );


// 字符流读写示例:
        
// 字符流读操作
        BufferedReader stdin = new  BufferedReader( new  InputStreamReader(System.in));
        System.out.println(
" Enter a line " );
        String in
= stdin.readLine();
        
        File file
= new  File(in);
        System.out.println(in
+ " 的大小为:  " + file.length());
        
        
// 读取字符
        
// 创建FileReader对象
        FileReader fr = new  FileReader(in);
        
        
// 通过fr创建BufferedReader对象
        BufferedReader br = new  BufferedReader(fr);
        
        
// 读取一行数据
        String line = br.readLine();
        
while (line != null ) {
            System.out.println(line);
            line
=br.readLine();
        }

        fr.close();
        br.close();
        
        
// 写数据
        FileWriter fw = new  FileWriter(in);
        BufferedWriter bw
= new  BufferedWriter(fw);
        
        
for ( int  i = 0 ;i < 10 ;i ++ ) {
            bw.write(
"值是: "+i);
            bw.newLine();
            bw.flush();
        }

        fw.close();
        bw.close();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值