字节流和字符流的read方法

news/2024/7/3 7:00:17

字节流和字符流的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 <code>-1</code> 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


http://www.niftyadmin.cn/n/1996174.html

相关文章

diy

diy 什么是DIY? Do it yourself! 这不是一句简单的英文&#xff0c;他代表的是一种精神。 什么精神&#xff1f;自己去做&#xff0c;自己体验&#xff0c;挑战自我&#xff0c;享受其中的快乐。 这就叫做DIY。 简言之,就是"亲历亲为"。在《新词词典》中的解释是&am…

MongoDB 主从复制(Master-Slave) 副本集(Replica Set) 分片(Sharding)

MongoDB 分片集群介绍MongoDB 复制集原理深度分析 官方建议用副本集替代主从复制 MongoDB 中提供了复制(Replication)机制&#xff0c;通过该机制可以帮助我们很容易实现读写分离方案&#xff0c;并支持灾难恢复&#xff08;服务器断电&#xff09;等意外情况下的数据安全。 在…

重装系统后计算机无法联网,小编教你重装win10系统后电脑上不了网怎么办

我们常常在刚装完电脑系统的时候会碰到电脑不能上网的情况&#xff0c;这可能是在安装的过程中没有将网卡驱动安装好&#xff0c;导致电脑不能上网&#xff0c;只需要下载驱动人生网卡版或者驱动精灵万能网卡版修复网卡驱动即可解决电脑不能上网的问题。接下来我们看看具体的操…

我的Linux系统开始学习的过程

Linux系统&#xff0c;不知大家是否了解。接触计算机不多或对计算机不感冒的人可能对其比较陌生&#xff0c;曾经的我也是。上大学前的我的确对Linux一无所知&#xff0c;那时候接触面窄&#xff0c;都没有听说过此名字&#xff0c;上了大学后&#xff0c;身边的人有学习或者用…

用STL快速编写ini配置文件识别类

用STL快速编写ini配置文件识别类 作者&#xff1a;Winter 用STL快速编写ini配置文件识别类 1 设计需求&#xff1a; 2 设计实现&#xff1a; 3 具体使用 ini文件是技术人员经常用到的一种系统配置方法&#xff0c;如何读取和快速识别ini文件中的内容实现起来比较繁琐。STL强大的…

JavaScript 创建对象的方式

在JS中&#xff0c;创建对象&#xff08;Create Object&#xff09;并不完全是我们时常说的创建类对象&#xff0c;JS中的对象强调的是一种复合类型&#xff0c;JS中创建对象及对对象的访问是极其灵活的。 一、由一对大括号括起来 var emptyObj {};var myObj {id: 1, …

计算机视觉 图像分类 环境配置,图像分类综述

计算机视觉三个层次计算机视觉三层次1. 图像分类概述1.1图像分类是指根据一定的分类规则将图像自动分到一组预定义类别中的过程。1.2图像分类方法的划分十分多样。根据图像语义内容的不同层次可以将图像分类划分为&#xff1a;(1)对象分类 object categorization(2)场景分类 sc…

css写一个步骤条

效果图代码如下<!DOCTYPE html> <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><title>process-example</title><style>/* reset.css */body {font-size: 12px !important;f…