java io读取文件操作代码实例

 更新时间:2019年11月20日 11:48:17   作者:闻窗  
这篇文章主要介绍了java io读取文件操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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+"t");
    };
    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))+"t");
    };
    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+"t");
    };
    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)+"t");
    };
    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+"t");
    };
    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)+"t");
    };
    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+"t");
    };
    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)+"t");
    };
    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+"t");
    };
    br.close();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • gateway、webflux、reactor-netty请求日志输出方式

    gateway、webflux、reactor-netty请求日志输出方式

    这篇文章主要介绍了gateway、webflux、reactor-netty请求日志输出方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 浅谈Java中对类的主动引用和被动引用

    浅谈Java中对类的主动引用和被动引用

    这篇文章主要介绍了浅谈Java中对类的主动引用和被动引用,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Java优雅的处理金钱问题(BigDecimal)

    Java优雅的处理金钱问题(BigDecimal)

    本文主要介绍了Java优雅的处理金钱问题(BigDecimal),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • 详解java中DelayQueue的使用

    详解java中DelayQueue的使用

    这篇文章主要介绍了java中DelayQueue的使用,帮助大家更好的理解和学习Java,感兴趣的朋友可以了解下
    2020-10-10
  • Java核心库实现AOP过程

    Java核心库实现AOP过程

    给大家分享一下利用Java核心库实现简单的AOP的经验分享和教学,需要的读者们参考下吧。
    2017-12-12
  • 关于Lambda表达式的方法引用和构造器引用简的单示例

    关于Lambda表达式的方法引用和构造器引用简的单示例

    这篇文章主要介绍了关于Lambda表达式的方法引用和构造器引用简的单示例,方法引用与构造器引用可以使 Lambda 表达式的代码块更加简洁<BR>,需要的朋友可以参考下
    2023-04-04
  • 通过Class类获取对象(实例讲解)

    通过Class类获取对象(实例讲解)

    下面小编就为大家带来一篇通过Class类获取对象(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • SpringBoot接口返回的数据时间与实际相差8小时问题排查方式

    SpringBoot接口返回的数据时间与实际相差8小时问题排查方式

    文章描述了在部署SpringBoot应用到容器中时遇到请求接口返回时间与实际相差8小时的问题,并详细分析了可能的原因及具体的排查步骤和解决方案,总结指出,环境初始时区未配置是根本原因,建议在应用部署前配置好时区
    2025-02-02
  • java多线程之Balking模式介绍

    java多线程之Balking模式介绍

    大家好,本篇文章主要讲的是java多线程之Balking模式介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • spring boot+ redis 接口访问频率限制的实现

    spring boot+ redis 接口访问频率限制的实现

    这篇文章主要介绍了spring boot+ redis 接口访问频率限制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01

最新评论