javaio读取文件操作代码实例

这篇文章主要介绍了java io读取文件操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

成都创新互联公司是一家专注于成都网站制作、成都网站建设、外贸营销网站建设与策划设计,沛县网站建设哪家好?成都创新互联公司做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:沛县等地区。沛县做网站价格咨询:13518219792

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

这里直接上代码,读取文件的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();
  }
}

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


名称栏目:javaio读取文件操作代码实例
标题路径:http://bzwzjz.com/article/psspej.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 营销网站建设 高端网站建设 成都网站建设 做网站设计 成都网站建设 营销型网站建设 上市集团网站建设 成都网站制作 成都商城网站制作 重庆手机网站建设 定制级高端网站建设 重庆网站制作 重庆电商网站建设 四川成都网站制作 成都网站建设 网站制作报价 攀枝花网站设计 成都品牌网站设计 成都网站建设流程 温江网站设计 成都网站设计 成都网站设计