博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字节流和字符流的read方法
阅读量:6320 次
发布时间:2019-06-22

本文共 4119 字,大约阅读时间需要 13 分钟。

字节流和字符流的read方法

public class Test {    public void fileOutput() throws Exception {        File file = new File("D:/2.txt");        FileOutputStream fileOutputStream = new FileOutputStream(file);        String s = "abcdefg";        fileOutputStream.write(s.getBytes());        fileOutputStream.close();    }    /**     * fileInputStream.read()是一个字节一个字节的读,返回值为根据ascii码表转成的int值     * 输出结果     97     a     98     b     99     c     100     d     101     e     102     f     103     g     *     * @throws Exception     */    public void fileInput() throws Exception {        File file = new File("D:/2.txt");        FileInputStream fileInputStream = new FileInputStream(file);        int a;        while ((a = fileInputStream.read()) != -1) {            System.out.println(a);            System.out.println((char)a);        }        fileInputStream.close();    }    /**     * 输出结果:     * 97     * 98     * 99     * 100     * 101     * 102     * 103     * [B@5a8e6209     * abcdefg     *     * @throws Exception     */    public void fileInput2() throws Exception {        File file = new File("D:/2.txt");        FileInputStream fileInputStream = new FileInputStream(file);        int a;        int[] b = new int[10];        byte[] c = new byte[10];        int len = 0;        while ((a = fileInputStream.read()) != -1) {            b[len] = a;            c[len] = (byte) a;            len++;        }        for (int i = 0; i < len; i++) {            System.out.println(b[i]);        }        System.out.println(c.toString());        System.out.println(new String(c));        fileInputStream.close();    }    /**     * 带参数read(byte[] b)方法,读取参数b字节大小     * 其返回值为int类型,the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.     * 输出结果:     * 7     * -1     * abcdefg     *     * @throws Exception     */    public void fileInput3() throws Exception {        File file = new File("D:/2.txt");        FileInputStream fileInputStream = new FileInputStream(file);        byte[] b = new byte[10];        byte[] bb = new byte[5];        int a = fileInputStream.read(b);        int c = fileInputStream.read(bb);        System.out.println(a);        System.out.println(c);        System.out.println(new String(b));        fileInputStream.close();    }    /**     * 输出结果     * 7     * abcdefg     *     * @throws Exception     */    public void fileInput4() throws Exception {        File file = new File("D:/2.txt");        FileInputStream fileInputStream = new FileInputStream(file);        byte[] b = new byte[10];        int a = fileInputStream.read(b, 0, new Long(file.length()).intValue());        System.out.println(a);        System.out.println(new String(b));        fileInputStream.close();    }    /**     * 输出结果:     * 97     * 98     * 99     * 100     * 101     * 102     * 103     * @throws Exception     */    public void fileReader() throws Exception {        File file = new File("D:/2.txt");        FileReader fileReader = new FileReader(file);        int a;        while ((a=fileReader.read())!=-1){            System.out.println(a);        }        fileReader.close();    }    /**     * 输出结果:     * abcdefg     * @throws Exception     */    public void fileReader2() throws Exception {        File file = new File("D:/2.txt");        FileReader fileReader = new FileReader(file);        int a;        int len=0;        byte[] b=new byte[10];        while ((a=fileReader.read())!=-1){            b[len]=(byte) a;            len++;        }        System.out.println(new String(b));        fileReader.close();    }    /**     * 输出结果:     * abcdefg     * @throws Exception     */    public void fileReader3() throws Exception {        File file = new File("D:/2.txt");        FileReader fileReader = new FileReader(file);        char[] cbuf=new char[10];        fileReader.read(cbuf);        System.out.println(new String(cbuf));        fileReader.close();    }    public static void main(String[] args) throws Exception {        Test test = new Test();        test.fileInput();             }}

 

转载于:https://www.cnblogs.com/BonnieWss/p/10910931.html

你可能感兴趣的文章
我的友情链接
查看>>
我的友情链接
查看>>
JavaScript函数eval()
查看>>
Linux LTP 测试框架
查看>>
log4j 每次运行生成文件
查看>>
“经常加班”有误区
查看>>
jquery各种事件触发实例
查看>>
我的友情链接
查看>>
MY TroubleShooting
查看>>
鸟哥学习笔记---DNS
查看>>
Linux 常用目录管理命令(cd pwd mkdir rmdir)
查看>>
java程序员菜鸟进阶(四)oracle基础详解(四)oracle开启和关闭服务程序——解决安装oracle占用大量内存...
查看>>
Flask_学习笔记_09: Flask中的继承
查看>>
Mahout源码目录说明
查看>>
我的友情链接
查看>>
Java学习日志(17-2-集合框架工具类Arrays及其他特性)
查看>>
HTTP响应头和请求头信息对照表
查看>>
Chrome完美屏蔽优酷广告及黑屏教程
查看>>
一份不错的php面试题(附答案)
查看>>
前端工程资源发布、优化
查看>>