第四篇 IO流技术(四)

package com.zzp.demo01;

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

/**
 * 
 * 第一个程序 理解操作步骤
 * 1、创建源
 * 2、选择流
 * 3、操作
 * 4、释放资源
 * @author java
 *
 */
public class IOTest01 {
    public static void main(String[] args) {
        //1、创建源
        File src = new File("1.txt");        
        try {
            //2、选择流
            InputStream is = new FileInputStream(src);
            //3、操作
            int read1 = is.read();
            int read2 = is.read();
            int read3 = is.read();
            int read4 = is.read();//如果存在第四个字符就打印第四个,不存在打印-1
            System.out.println((char)read1);
            System.out.println((char)read2);
            System.out.println((char)read3);
            System.out.println(read4);
            //4、释放资源
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }         
    }
}

1.txt

package com.zzp.demo01;

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

/**
 * 
 * 第一个程序 理解操作步骤
 * 1、创建源
 * 2、选择流
 * 3、操作
 * 4、释放资源
 * @author java
 *
 */
public class IOTest02 {
    public static void main(String[] args) {
        //1、创建源
        File src = new File("1.txt");//相对路径
        //2、选择流
        InputStream is = null;
        try {
            is = new FileInputStream(src);
            //3、操作
            int temp;
            while((temp = is.read())!= -1){
                System.out.println((char)temp);//如果存在第四个字符就打印第四个,不存在打印-1
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null != is){
                //4、释放资源
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }         
    }
}

 

package com.zzp.demo01;

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

/**
 * 
 * 四个步骤:分段读取(文件字节输入流)
 * 1、创建源
 * 2、选择流
 * 3、操作
 * 4、释放资源
 * @author java
 *
 */
public class IOTest03 {
    public static void main(String[] args) {
        //1、创建源
        File src = new File("1.txt");//相对路径
        //2、选择流
        InputStream is = null;
        try {
            is = new FileInputStream(src);
            //3、操作  分段读取
            byte[] flush = new byte[1024*10]; //10k一读取
            int len = -1;//设置默认长度为-1
            while((len = is.read(flush))!= -1){
                String str = new String(flush, 0, len);//将字节转化为字符串
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null != is){
                //4、释放资源
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }         
    }
}

 

posted on 2018-08-26 23:47  奋斗的小刀001  阅读(103)  评论(0编辑  收藏  举报

导航