字节流读取和写入

字节流读取:一个一个字节地读取

package com.sxt.io;

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 
 *
 */
public class IOTest01 {

    public static void main(String[] args) {
        //1、创建源
        File src = new File("abc.txt");
        //2、选择流
        try {
            InputStream  is =new FileInputStream(src);
            //3、操作 (读取)
            int data1 = is.read(); //第一个数据s
            int data2  = is.read(); //第二个数据x
            int data3  = is.read(); //第三个数据t
            int data4  = is.read(); //????不是数据,文件的末尾返回-1
            System.out.println((char)data1);
            System.out.println((char)data2);
            System.out.println((char)data3);
            System.out.println(data4);
            //4、释放资源
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
View Code
package com.sxt.io;

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 
 *
 */
public class IOTest02 {

    public static void main(String[] args) {
        //1、创建源
        File src = new File("abc.txt");
        //2、选择流
        InputStream  is =null;
        try {
            is =new FileInputStream(src);
            //3、操作 (读取)
            int temp ;
            while((temp=is.read())!=-1) {
                System.out.println((char)temp);
            }        
        
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4、释放资源
            try {
                if(null!=is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
View Code

字节流分段读取:

package com.sxt.io;

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 
 *
 */
public class IOTest03 {

    public static void main(String[] args) {
        //1、创建源
        File src = new File("abc.txt");
        //2、选择流
        InputStream  is =null;
        try {
            is =new FileInputStream(src);
            //3、操作 (分段读取)
            byte[] flush = new byte[1024*10]; //缓冲容器
            int len = -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 {
            //4、释放资源
            try {
                if(null!=is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
View Code

字节流写入:

package com.sxt.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 文件字节输出流
 *1、创建源
 *2、选择流
 *3、操作(写出内容)
 *4、释放资源
 *  @author 
 *
 */
public class IOTest04 {

    public static void main(String[] args) {
        //1、创建源
        File dest = new File("dest.txt");
        //2、选择流
        OutputStream os =null;
        try {
            os = new FileOutputStream(dest,true);
            //3、操作(写出)
            String msg ="IO is so easy\r\n";
            byte[] datas =msg.getBytes(); // 字符串-->字节数组(编码)
            os.write(datas,0,datas.length);
            os.flush();
        }catch(FileNotFoundException e) {        
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            //4、释放资源
            try {
                if (null != os) {
                    os.close();
                } 
            } catch (Exception e) {
            }
        }
    }

}
View Code

猜你喜欢

转载自www.cnblogs.com/jiefangzhe/p/11303935.html
今日推荐