每次调用函数时,从文件读取一行 [英] Read one line at time from a file when a function is called each time

查看:242
本文介绍了每次调用函数时,从文件读取一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  String time = Read_one_Line(); 

public static String Read_one_Line()抛出FileNotFoundException,IOException异常
{

FileInputStream fin = new FileInputStream(sample.txt);

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

str = br.readLine();

next_line = br.readLine();

return next_line;

每次从文本文件中读取一行称为sample.txt返回。下一次它应该返回下一行,等等....

sample.txt的内容是:

  Date:0 Year:0 Hour:0 Minute:0 Seconds:0 MilliSeconds:310 
$ b Date:0 Year:0 Hour :0分钟:0秒:0毫秒:0

日期:0年:0小时:0分钟:0秒:0毫秒:10

日期:0年: 0小时:0分钟:0秒:0毫秒:0

日期:0年:0小时:0分钟:0秒:0毫秒:380

日期:0年份:0小时:0分钟:0秒:10毫秒:-840

日期:0年份:0小时:0分钟:0秒:0毫秒:0

日期:0年份:0小时:0分钟:0秒:0毫秒:0

日期:0年份:0小时:0分钟:0秒:0毫秒:0

Date:0 Year:0 Hour:0 Minute:0 Seconds:0 MilliSeconds:0

但是而是每次只阅读和返回第一行..请告诉米e如何增加到下一行,并在下一次调用这个函数的时候返回它。

解决方案

你正在创建一个新的 FileInputStream 每次调用函数。因此,每次从头开始读取文件。



仅在函数之外创建 BufferedReader 并在每次调用时传递它,以便连续读取文件。

  public static String Read_one_Line(final BufferedReader br)throws IOException 
{
next_line = br.readLine();

return next_line;
}

使用情况将会像

  static void main(String args [])throws IOException {

FileInputStream fin = new FileInputStream(sample.txt);

try {

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

String line = Read_one_Line(br);
while(line!= null){
System.out.println(line);
line = Read_one_Line(br);
}

} finally {
fin.close(); //确保我们完成后关闭文件。






$ b注意在这种情况下 Read_one_Line 可以被忽略,并由一个简单的 br.readLine()替换。

<如果你只想每隔一行,你可以在每个迭代中读两行,比如

  String line = Read_one_Line BR); 
while(line!= null){
System.out.println(line);
String dummy = Read_one_Line(br);
line = Read_one_Line(br);
}


String time=Read_one_Line();

public static String Read_one_Line() throws FileNotFoundException, IOException
{

  FileInputStream fin=new FileInputStream("sample.txt");

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

  str=br.readLine();

  next_line=br.readLine();

  return next_line;
}

Each time it should read one line from text file called "sample.txt" and return it. next time it should return next line and so on....

Contents of sample.txt are:

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:310

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:0

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:10

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:0

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:380

Date:0   Year:0  Hour:0  Minute:0    Seconds:10  MilliSeconds:-840

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:0

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:0

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:0

Date:0   Year:0  Hour:0  Minute:0    Seconds:0   MilliSeconds:0

But rather its reading and returning first line only each time.. please tell me how to increment to next line and return it when i call this function next time.

解决方案

You are creating a new FileInputStream each time the function is called. Therefore, the file is read from the beginning each time.

Create the BufferedReader only once, outside of the function and pass it in on each call, so that the file is successively read.

public static String Read_one_Line(final BufferedReader br) throws IOException
{
  next_line=br.readLine();

  return next_line;
}

usage would be like

static void main(String args[]) throws IOException {

  FileInputStream fin=new FileInputStream("sample.txt");

  try {

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

    String line = Read_one_Line(br);
    while ( line != null ) {
      System.out.println(line);
      line = Read_one_Line(br);
    }

  } finally {
    fin.close(); // Make sure we close the file when we're done.
  }
}

Note that in this case, Read_one_Line could just be omitted and replaced by a simple br.readLine().

If you only want every other line, you can just read two lines in each iteration, like

String line = Read_one_Line(br);
while ( line != null ) {
  System.out.println(line);
  String dummy = Read_one_Line(br);
  line = Read_one_Line(br);
}

这篇关于每次调用函数时,从文件读取一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆