Java IO知识点总结二(字节流)

来源:互联网 发布:新歌声网络战队都有谁 编辑:程序博客网 时间:2024/05/16 17:43
在程序中所有数据都是以流的方式进行传输或者保存的,程序需要使用输入流或输出流对程序进行读写。

1.java操作文件的主要流程
(1)使用File打开一个文件
(2)通过字节流或字符流的子类指定输出位置
(3)进行读写操作
(4)关闭输入输出流
2.字节流概念
字节流主要操作byte类型的数据,以byte数组为准,主要操作类是OutputStream类和InputStream类
3.字节流输出:OutputStream
这里写图片描述
构造方法为:public FileOutputStream(File file) throw FileNotFoundException
4.字节流输出代码操作实例如下

package stringText;import java.io.*;public class OutputStreamText {    public static void main(String[] args) throws Exception {        /*         * 例1 向文件写入字符串         */        // 第一步:使用File类找到一个文件        File f = new File("d:\\test\\1.tx.t"); // 声明File对象        // 第二步:通过子类实例化父类对象        OutputStream out = null; // 准备好一个输出流对象        out = new FileOutputStream(f); // 通过多态进行实例化        // 第三步:进行写操作        String str = " Hello Word!!";// 准备一个字符串        byte b[] = str.getBytes();// 输出byte数组        out.write(b);// 将内容输出,保存文件        // 第四步:关闭输出流        out.close();// 关闭输出流        /*         * 例2 使用write(int t)的方式写入文件内容         */        // 第一步:使用File类找到一个文件        File f1 = new File("d:\\test\\2.txt"); // 声明File对象        // 第二步:通过子类实例化父类对象        OutputStream out1 = null; // 准备好一个输出流对象        out1 = new FileOutputStream(f1); // 通过多态进行实例化        // 第三步:进行写操作        String str1 = " Hello Word!!";// 准备一个字符串        byte b1[] = str1.getBytes();// 输出byte数组        for (int i = 0; i < b1.length; i++) {            out1.write(b1[i]); // 将内容输出,与上一例相比就此处不同        }        // 第四步:关闭输出流        out1.close();// 关闭输出流        /*         * 例3 追加新内容         */        // 第一步:使用File类找到一个文件        File f2 = new File("d:\\test\\2.txt"); // 声明File对象        // 第二步:通过子类实例化父类对象        OutputStream out3 = null; // 准备好一个输出流对象        out3 = new FileOutputStream(f2, true); // 通过多态进行实例化,表示在文件末追加内容        // 第三步:进行写操作        String str2 = " Hello Word!!";// 准备一个字符串        byte b2[] = str2.getBytes();// 输出byte数组        for (int j = 0; j < b1.length; j++) {            out3.write(b2[j]); // 将内容输出,与上一例相比就此处不同        }        // 第四步:关闭输出流        out3.close();// 关闭输出流    }}

5.字节输入流InputStream
这里写图片描述
6.输入流实例代码如下

package stringText;import java.io.*;public class InputStreamTest {    public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        /*         * 例1 读取字符串         */        File f=new File("d:\\test\\1.txt");//找到File文件        InputStream input=null;//准备输入流对象        input=new FileInputStream(f);  //实例化对象        byte[] b=new byte[1024];//将内容读到此数组中        int len=input.read(b);//将内容取出来,读到b数组中        System.out.println("内容为:"+new String(b,0,len));//将byte数组变为字符串输出        /*         * 例2 读取字符串(指定开辟的空间)         */        byte[] b1=new byte[(int)f.length()];//将内容读到此数组中,数组大小由文件的大小绝对        input.read(b1);//将内容取出来,读到b数组中        System.out.println("例二内容为:"+new String(b));//将byte数组变为字符串输出        /*         * 例3 读取字符串(循环读取)         */        File f1=new File("d:\\test\\1.txt");//找到File文件        InputStream input1=null;//准备输入流对象        input1=new FileInputStream(f1);  //实例化对象        byte[] b2=new byte[(int)f1.length()];//将内容读到此数组中        for(int i=0;i<b2.length;i++){            b[i]=(byte)input1.read();        }        input.close();        System.out.println("内容为:"+new String(b));//将byte数组变为字符串输出        /*         * 例4 读取字符串         */        File f2=new File("d:\\test\\1.txt");//找到File文件        InputStream input2=null;//准备输入流对象        input2=new FileInputStream(f2);  //实例化对象        int len2=0;        int temp=0;        byte[] b3=new byte[1024];//将内容读到此数组中        while((temp=input2.read())!=-1){            //每次的读取内容给temp变量,如果temp的值不是-1,则表示文件没有读完            b[len2]=(byte)temp;            len2++;        }        input2.close();        System.out.println("内容为:"+new String(b,0,len2));//将byte数组变为字符串输出    }}
原创粉丝点击