java IO流 笔记1

来源:互联网 发布:天天酷跑ios清除数据 编辑:程序博客网 时间:2024/06/07 09:14
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.BitSet;import org.junit.Test;/** * 开发者  站在 Jvm角度  看  Jvm与 磁盘 交互  理解   输入输出    *  * 所谓的输入读取 指的是 由 磁盘 向  JVm虚拟机    方向        * 所谓的输出 写出   指的是 由JVm虚拟机 向磁盘中  方向 *  *  *  * Io 流的分类 : * 分类的不同: * 1、   按数据照流向不同分:  输入流     输出流     * 2、   按处理数据单位不同: 字节流 (处理 图片 音频  视频等字节文件)     字符流 (只  处理 纯 文本文件  !!!)   * 3、  按照角色不同:节点流  处理流  *  * Io 的体系 : * 2对    4个抽象基类:                                      节点流(文件流) * InputStream         FileInputStream * OutputStream     FileOutputStream *  * Reader     FileReader * Writer       FileWriter *           *  *  *  *  *  *  *  * @author Administrator * */public class FIleInputOutputStream {/*** FileOutputStream   内容输出到 磁盘文件 。*/@Testpublic  void  testFileOutputStream(){// 创建一个 File对象 表示要操作的文件   ----写入时 此文件可以不存在,   如果文件存在  覆盖原来的内容  全新写入 。File file = new File("asd123.txt");// 创建一个 FileOutputStream 对象   把 File作为形参 传递到 构造器中。 FileOutputStream fos=null;try {fos = new FileOutputStream(file);// write(byte[] b);  向文件中  写入 byte内容 。  -----文件中以前的内容会被byte[]内容 全部覆盖fos.write(new String("this  is  output  to  txt !").getBytes());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(fos != null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} /*** 测试  输入流   多个字节   遍历 读取的  * *  fileInputStream.read(byte b[])  当最后没有字节  读入 b[]中时 返回 -1. 否则返回 每次读入 b[]中的字节个数 *  *  先读取若干字节到数组  b[]中*  再把数组分别读取 。*  注意遍历时 由于 最后一个数组可能存放不满 , 所以  每次遍历  数组  b[]时*   从 0个元素开始遍历 ,到 read(byte b[])返回的读取到数组的个数 为止 ,而不是读取每个数组自身的长度。*/@Testpublic  void  testFileInputStream2(){// 创建一个 File对象      表示要操作的文件对象 ---- 1、指名操作的文件对象。File  file = new File("abc.txt");// 创建一个 FileInputStream对象 , 把File对象当做形参放到  FileInputStream构造器中。-----------2、具体对 文件对象的内容进行操作  。FileInputStream  input =null;try {input =new FileInputStream(file);// 把内容读到数组里  ,用字节数组  接收再输入 一次可以读取5个字节     减少 文件和 jvm 虚拟机的交互次数  。byte[]  bs = new  byte[5];int len;while((len = input.read(bs))!=-1){// 方式一 :遍历字节数组 // for(int i=0 ;i<len; i++){// System.out.print((char)bs[i]);// }// 方式二: 直接byte[]数组转换成  String 字符串。  遍历数组的最后一个数组时  由于 最后剩余的字节个数 有时不能完全跟数组长度相等   ,// 此处需要 用 存入 byte[]实际的个数(每次 存入 byte[]的个数    : 即  fileInputStream.read(byte b[]) 的返回值。 ) 而不是byte[]数组的固定长度。 !!!!// bs数组   、数组的开始位置 0   实际存入数组的个数 len 。String st = new String(bs,0,len);System.out.println(st);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(input != null){try {input.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}/*** 测试  输入流     第一个测试      fileInputStream.read()  //单个字节 读取 */@Testpublic  void  testFileInputStream1(){// 加载文件对象 // 注意 在读取 时  如果文件不存在 则 报异常,  此处 必须 注意  文件要存在 ,  而 jvm想磁盘写入时 ,如果文件不存在 则 自动创建 。File  file =new File("abc.txt");// 在磁盘文件  和 jvm 虚拟机之间       创建节点管道FileInputStream  input=null;try {input =new FileInputStream(file);// 返回 一个字节   用 int 接收  int i; while((i=input.read()) !=-1){System.out.print((char)i);}// } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{// 最后关闭 Io流 if(input !=null){try {input.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

0 0
原创粉丝点击