读取文件第一行java_Java一行行地读文件

JAVA提供两种一行行读取文件内容的类 BufferReader和Scanner

BufferReader是标准,Scanner是JDK1.5引入,提供了很多方便方法,好处可以看如下代码。

============================================

/**

* 文件读取器

* User: Koala.he

* Date: 14-4-11

* Time: 下午1:34

*/

public class FileReader {

/**

* 利用BufferReader读文件

* @param in

*/

public static void readByBufferReander(InputStream in){

BufferedReader br=new BufferedReader(new InputStreamReader(in));

try{

String line=br.readLine();

while (line!=null){

System.out.println(line);

line=br.readLine();

}

}catch (IOException e){

e.printStackTrace();

}finally {

try {

br.close();

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 利用Scanner读取文件

* @param in

*/

public static void readByScaner(InputStream in){

Scanner scanner=new Scanner(in);

while (scanner.hasNextLine()){

System.out.println(scanner.nextLine());

}

scanner.close();

}

public static void main(String orgs[]){

String fileName="spring-config.xml";

InputStream fin=FileReader.class.getClassLoader().getResourceAsStream(fileName);

if(fin==null){

System.out.println("系统找不到指定的路径。");

return ;

}

System.out.println("开始读取文件。。。");

//1:bufferReader一行一行读数据

//readByBufferReander(fin);

//2:scanner一行一行读数据

readByScaner(fin);

System.out.println("结束读取文件。。。");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值