博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java read file
阅读量:4029 次
发布时间:2019-05-24

本文共 1277 字,大约阅读时间需要 4 分钟。

This code will read the MyFile.txt and print its content on the console. It reads the file line by line in the form of DataInputStream.

package  MyProject
import  java.io.BufferedInputStream;
import  java.io.DataInputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileNotFoundException;
import  java.io.IOException;
/**
  * This program reads a text file line by line and print to the console. It uses
  * FileOutputStream to read the file.
 
  */
public class  FileInput  {
   public static  void  main ( String []  args ) {
     File file =  new  File ( "C://MyFile.txt" ) ;
     FileInputStream fis =  null ;
     BufferedInputStream bis =  null ;
     DataInputStream dis =  null ;
     try  {
       fis =  new  FileInputStream ( file ) ;
       // Here BufferedInputStream is added for fast reading.
       bis =  new  BufferedInputStream ( fis ) ;
       dis =  new  DataInputStream ( bis ) ;
       // dis.available() returns 0 if the file does not have more lines.
       while  ( dis.available ()  !=  0 ) {
       // this statement reads the line from the file and print it to
         // the console.
         System.out.println ( dis.readLine ()) ;
       }
       // dispose all the resources after using them.
       fis.close () ;
       bis.close () ;
       dis.close () ;
     catch  ( FileNotFoundException e ) {
       e.printStackTrace () ;
     catch  ( IOException e ) {
       e.printStackTrace () ;
     }
   }
}

转载地址:http://uiqbi.baihongyu.com/

你可能感兴趣的文章
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
网页设计里的浮动 属性
查看>>
Maximum Subsequence Sum
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>