首页 > 其他 > 详细

IO流

时间:2014-12-10 17:55:51      阅读:234      评论:0      收藏:0      [点我收藏+]

1 流的分类:

按照数据刘翔的不同:输入流 输出流

按照处理数据的单位的不同:字节流 字符流(处理的文本文件)

按照角色的不同:节点流(直接作用于文件)处理流

package lianxi1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

public class TestFileInputOutputStream {
    @Test //要读取的文件一定要存在
   public void test1(){
    FileInputStream fis = null;
    try {
            File file1 = new File("d:\\io\\helloworld.txt");
            fis = new FileInputStream(file1);
            int b = fis.read();
            while(b!=-1){
                System.out.print((char)b);
                b = fis.read();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    @Test
    // 要读取的文件一定要存在
    public void test2() {
        FileInputStream fis = null;
        try {
            File file1 = new File("d:\\io\\helloworld.txt");
            fis = new FileInputStream(file1);
            byte[] b = new byte[6];
            int len;
//            int len = fis.read(b);
//            while (len != -1) {
//                for(int i=0;i<len;i++){
//                   System.out.print((char)b[i]);
//                }
//                len = fis.read(b);
//            }
            while ((len=fis.read(b))!= -1) {
                for(int i=0;i<len;i++){   //是len,不是b.length
                   System.out.print((char)b[i]);
                }
            
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

IO流

原文:http://www.cnblogs.com/yjtm53/p/4155868.html

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