zoukankan      html  css  js  c++  java
  • java -io 读取文件操作

    主要分为字节读取和字符读取,字节读取可以一个一个读取和字节数组读取,字符读取同样之,字符读取适合文本读取,字节读取皆可以

    这里直接上代码,读取文件的9个小demo

    
    
    package com.io;

    import org.junit.Test;

    import java.io.*;

    public class FileTest {
    //1、字节流字节一个一个读取
    @Test
    public void test() throws IOException{
    InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"));
    int len;
    //按字节一个一个读取
    while ((len = fis.read())!=-1){
    System.out.print((char)len+" ");
    };
    fis.close();
    }
    //输出结果h    e    l    l    o    w    o    r    l    d   

    //2、字节流字节数组读取
    @Test
    public void test1() throws IOException{
    InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"));
    byte[] bytes = new byte[2];
    int len ;
    //按字节数组读取 bytes存储的是读取的数据
    while ((len = fis.read(bytes))!=-1){
    System.out.print((new String(bytes))+" ");
    };
    fis.close();
    }
    //输出结果 he    ll    ow    or    ld   

    //3、缓冲字节流字节一个一个读取
    @Test
    public void test2() throws IOException{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
    int len ;
    while ((len = bis.read())!=-1){
    System.out.print((char)len+" ");
    };
    bis.close();
    }
    //输出结果h    e    l    l    o    w    o    r    l    d  

    //4、缓冲字节流字节数组读取
    @Test
    public void test3() throws IOException{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
    byte[] bytes = new byte[3];
    int len ;
    //按字节数组读取 bytes存储的是读取的数据
    while ((len = bis.read(bytes))!=-1){
    System.out.print(new String(bytes)+" ");
    };
    bis.close();
    }
    //输出结果hel    low    orl    drl   

    //5、字符流一个一个读取
    @Test
    public void test4() throws IOException{
    InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
    int len ;
    //按字节数组读取 bytes存储的是读取的数据
    while ((len = isr.read())!=-1){
    System.out.print((char)len+" ");
    };
    isr.close();
    }


    //6、字符流字符数组读取
    @Test
    public void test5() throws IOException{
    InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
    char[] chars = new char[3];
    int len ;
    //按字节数组读取 bytes存储的是读取的数据
    while ((len = isr.read(chars))!=-1){
    System.out.print(new String(chars)+" ");
    };
    isr.close();
    }

    //7、缓冲字符流字符一个一个读取
    @Test
    public void test6() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))));
    int len ;
    //按字节数组读取 bytes存储的是读取的数据
    while ((len = br.read())!=-1){
    System.out.print((char)len+" ");
    };
    br.close();
    }

    //8、缓冲字符流字符数组读取
    @Test
    public void test7() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))));
    char[] chars = new char[3];
    int len ;
    //按字节数组读取 bytes存储的是读取的数据
    while ((len = br.read(chars))!=-1){
    System.out.print(new String(chars)+" ");
    };
    br.close();
    }

    //9、缓冲字符流按行读取
    @Test
    public void test8() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))));
    //按字节数组读取 bytes存储的是读取的数据
    String str;
    while ( (str = br.readLine())!=null){
    System.out.print(str+" ");
    };
    br.close();
    }
    }
    待更。。。。。。。。。。。。。。
  • 相关阅读:
    MongoDB创建数据库并授权
    nginx反向代理tomcat后,登录提示验证码错误,直接通过ip访问tomcat服务就可以
    Centos7 为es安装kibana
    ssh登录很慢解决方法
    nginx反向代理后端服务,访问时页面空白
    Error: rpmdb open failed
    centos7虚拟内存设置
    CentOS7利用docker安装MySQL5.7
    uniapp实现顶部tab选项卡
    top使用方法总结
  • 原文地址:https://www.cnblogs.com/tdyang/p/10834577.html
Copyright © 2011-2022 走看看