1 java8读取文本文件
  2 
  3         
  4     public static void java8ReadFileLines(String fileName) throws IOException {
  5         List lineList = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
  6 
  7         for(String line:lineList){
  8             System.out.println(line);
  9         }
 10     }    
 11     
 12         
 13         
 14 一行一行地读取文件
 15 
 16 
 17     public static void readFileByLines(String fileName) {
 18         File file = new File(fileName);
 19         BufferedReader reader = null;
 20         try {
 21             reader = new BufferedReader(new FileReader(file));
 22             String line = null;
 23             while((line=reader.readLine())!=null ) {
 24                 System.out.println(line);
 25             }
 26         }catch (IOException e) {
 27 
 28         }finally {
 29             if(reader!=null) {
 30                 try{
 31                     reader.close();
 32                 }catch (IOException e) {
 33                     ;
 34                 }
 35             }
 36         }
 37     }    
 38     
 39     
 40     
 41 一次读取多个字符
 42 
 43 
 44     public static void readFileByMultiChars(String fileName) {
 45         File file = new File(fileName);
 46         try{
 47             InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
 48 
 49             char[] tempChars = new char[30];
 50 
 51             int readCount = 0;
 52             while((readCount=inputStreamReader.read(tempChars))!=-1) {
 53                 if(readCount==tempChars.length) {
 54                     System.out.println(tempChars);
 55                 }else{
 56                     System.out.println(Arrays.copyOf(tempChars, readCount));
 57                 }
 58             }
 59 
 60 
 61             inputStreamReader.close();
 62         }catch(Exception e) {
 63             e.printStackTrace();
 64         }
 65     }    
 66 
 67 
 68         
 69 一个字符一个字符地读取
 70 
 71 
 72     public static void readFileByChars(String fileName) {
 73         File file = new File(fileName);
 74         try{
 75             InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
 76 
 77             int tempChar;
 78 
 79             while((tempChar=inputStreamReader.read())!=-1) {
 80                 System.out.println((char)tempChar);
 81             }
 82 
 83 
 84             inputStreamReader.close();
 85         }catch(Exception e) {
 86             e.printStackTrace();
 87         }
 88     }        
 89 
 90 
 91         
 92 java8读取字节 超级简单
 93 
 94 
 95     public static byte[] java8ReadBytes(String fileName) throws IOException {
 96         return Files.readAllBytes(Paths.get(fileName));
 97     }
 98 
 99 
 
 一个字节一个字节地读取
 
 
    public static void readFileByOneByte(String fileName) {
         File file = new File(fileName);
         InputStream inputStream = null;
 
         try{
             inputStream = new FileInputStream(file);
             int tempByte;
             while( (tempByte=inputStream.read())!=-1) {
                 System.out.println(tempByte);
             }
 
             inputStream.close();
         }catch (IOException e) {
             System.out.println(e);
         }
 
     }
     
     
  
 一个字节一个字节读取到ByteBuffer
 
 
     public static byte[] readFileByOneByteToBuffer(String fileName) {
 
 
         ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
 
         File file = new File(fileName);
         InputStream inputStream = null;
 
         try{
             inputStream = new FileInputStream(file);
             int tempByte;
             while( (tempByte=inputStream.read())!=-1) {
                 byteBuffer.put((byte)tempByte);
             }
 
             inputStream.close();
         }catch (IOException e) {
             System.out.println(e);
         }
 
         byteBuffer.flip();
         System.out.println("one limit:" + byteBuffer.limit());
         byte[] result = new byte[byteBuffer.limit()];
         byteBuffer.get(result);
 
         return result;
 
 
     }
     
     
     
 多个字节进行读取
 
 
     public static void readFileByMultiBytes(String fileName) {
 
         File file = new File(fileName);
         InputStream inputStream = null;
 
         try {
             byte[] bytes = new byte[50];
             int byteRead = 0;
             inputStream = new FileInputStream(fileName);
 
             while( (byteRead = inputStream.read(bytes))!=-1 ) {
                 System.out.println(byteRead);
             }
         }catch(IOException e) {
             System.out.println(e);
         }finally {
             if(inputStream!=null) {
                 try{
                     inputStream.close();
                 }catch(IOException e){
                     System.out.println("iput stream close exception" +  e);
                 }
             }
         }
     }
     
     
   
 读取多个字节到ByteBuffer
 
 
     public static byte[] readFileByMultiBytesToBuffer(String fileName) {
 
         ByteBuffer byteBuffer = ByteBuffer.allocate(1024*1024);
         InputStream inputStream = null;
 
         try {
             byte[] bytes = new byte[50];
             int byteRead = 0;
             inputStream = new FileInputStream(fileName);
 
             int count = 0;
             while( (byteRead = inputStream.read(bytes))!=-1 ) {
                 byteBuffer.put(bytes, 0, byteRead);
                 count+=byteRead;
             }
 
             System.out.println("readCount:"+count);
         }catch(IOException e) {
             System.out.println(e);
         }finally {
             if(inputStream!=null) {
                 try{
                     inputStream.close();
                 }catch(IOException e){
                     System.out.println("iput stream close exception" +  e);
                 }
             }
         }
 
         byteBuffer.flip();
         System.out.println("multi limit:" + byteBuffer.limit());
         byte[] result = new byte[byteBuffer.limit()];
         byteBuffer.get(result);
 
         return result;
     }            
         

java读取文件的更多相关文章

  1. [Java]读取文件方法大全(转)

    [Java]读取文件方法大全   1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**     ...

  2. Java 读取文件的内容

    Java 读取文件的内容 1) CLASS_NAME: 换成自己真实的类名 2) /page/test.json: 换成自己真实的page 3) FileUtils: 来自于org.apache.co ...

  3. Java读取文件-BufferedReader/FileReader/InputStreamReader/FileInputStream的关系和区别

    一.Java读取和存储文件数据流 Java读取文件,实际是将文件中的字节流转换成字符流输出到屏幕的过程   这里面涉及到两个类:InputStreamReader和OutputStreamWriter ...

  4. 使用java读取文件夹中文件的行数

    使用java统计某文件夹下所有文件的行数 经理突然交代一个任务:要求统计某个文件夹下所有文件的行数.在网上查了一个多小时没有解决.后来心里不爽就决定自己写一个java类用来统计文件的行数,于是花了两个 ...

  5. Java读取文件的几种方式

    package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  6. java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)

    1.实现目标 读取文件,将文件中的数据一行行的取出. 2.代码实现 1).方式1: 通过BufferedReader的readLine()方法. /** * 功能:Java读取txt文件的内容 步骤: ...

  7. Java 读取文件到字符串

    Java的io操作比较复杂 package cn.outofmemory.util; import java.io.BufferedReader; import java.io.FileInputSt ...

  8. java读取文件夹下所有文件并替换文件每一行中指定的字符串

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  9. Java读取文件方法和给文件追加内容

    本文转载自:http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文 ...

  10. java读取文件方法总结

    由于最近在做一个关于从手机本地读取格式化的txt文件中的内容,并且把内容放在listview中显示.这样问题来了,就是如何能够遍历已经获取到特定的map中就是一个问题,在网上找了一些资料,找到了一个很 ...

随机推荐

  1. VS2012 error C2664: “std::make_pair”:无法将左值绑定到右值引用

    在vs2012(c++)make_pair()改动: C++: template <class T1, class T2> pair<V1, V2> make_pair(T1& ...

  2. Linux下开发常用 模拟 Http get和post请求

    1.get请求 curl "http://www.baidu.com"      如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "htt ...

  3. HTTP2.0介绍

    HTTP2.0的所有通信都是在一个TCP连接上完成的. 1.信息传输术语 1.1 流 虚拟信道,可以承载双向消息,每个流都有一个唯一的整数标识符(1.2....N); 1.2 消息 逻辑上的HTTP消 ...

  4. 解决 linux下编译make文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录” 问题

    PS背景:我在公司做sdk 的pc端开发,所以经常会在win下编译通过之后跑到linux下再运行一次已确保能支持多平台. 今儿在win下跑完一程序,然后放到linux下跑的时候,我用指令: [plai ...

  5. TFS2012 安装 配置笔记

    TFS2012安装   具体请看文档..   http://yunpan.cn/cmt4X6S7TjEgq  访问密码 464e     TFS2012配置 环境:VS2012  SQL2008  T ...

  6. json_decode

    <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...

  7. Mongodb 资源

    一.资源 1.C# 驱动下载地址 https://github.com/mongodb/mongo-csharp-driver/releases 2. Mongodb 管理工具 mongochef 下 ...

  8. XMLBEANS的使用总结

    在本文中,我们将详细了解最好的数据对象XMLBean.从传统角度来说,在Java应用程序中使用XML,就是在从 XML文档向Java导入数据的技术或从数据模型层向XML导出数据技术之间架起了一座桥梁. ...

  9. JQuery中==与===、$("#")与$("")的区别

    首先,== equality 等同,=== identity 恒等.==, 两边值类型不同的时候,要先进行类型转换,再比较.===,不做类型转换,类型不同的一定不等. 下面分别说明:先说 ===,这个 ...

  10. underflow 、overflow 下溢和上溢

    在strtoull函数返回值中,就提到上溢和下溢的问题,现在把这俩个概念拿出来涨涨见识! 上溢  Overflow 是当一个超长的数据进入到缓冲区时,超出部分被写入上级缓冲区,上级缓冲区存放的可能是数 ...