BufferedReader指针

发布于 2024-11-04 13:45:29 字数 379 浏览 0 评论 0 原文

我有以下代码,但我不明白如何将指针重置到起始位置:

BufferedReader inp=new BufferedReader(new FileReader(file));
Scanner leggi=new Scanner(inp);
for(int i=0;i<nwords;i++){
  while(leggi.hasNext()) 
    if(leggi.next().equals(args[i+2])) 
      occorrenze[i]=occorrenze[i]+1;
}
inp.close();

我尝试过

inp.mark(0);
inp.reset();

但没有结果。

I have the following code but I don't understand how I can reset the pointer to the starter position:

BufferedReader inp=new BufferedReader(new FileReader(file));
Scanner leggi=new Scanner(inp);
for(int i=0;i<nwords;i++){
  while(leggi.hasNext()) 
    if(leggi.next().equals(args[i+2])) 
      occorrenze[i]=occorrenze[i]+1;
}
inp.close();

I tried

inp.mark(0);
inp.reset();

with no results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2024-11-11 13:45:29

Paul,

我建议您通读这个旧线程:Java BufferedReader back to the top of a text file?

就我个人而言,我更喜欢乔恩·斯基特的回应,归根结底是“不要打扰[除非你必须]”。

干杯。基思.


编辑:此外,即使遇到异常,您也应该始终关闭该输入文件。 finally 块非常适合此目的。


EDIT2:

希望您仍然和我们在一起。

这是我的尝试,FWIW,您不需要重置输入文件,您只需要转置 whilefor 循环。

package forums;

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class WordOccurrenceCount
{
  public static void main(String[] args) {
    try {
      String[] words = { "and", "is", "a", "the", "of", "as" };
      int[] occurrences = readOccurrences("C:/tmp/prose.txt", words);
      for ( int i=0; i<words.length; i++ ) {
        System.out.println(words[i] + " " + occurrences[i]);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static final int[] readOccurrences(String filename, String... words) 
    throws IOException
  {
    int[] occurrences = new int[words.length];
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(filename));
      Scanner scanner = new Scanner(reader);
      while ( scanner.hasNext() ) {
        String word = scanner.next();
        for ( int i=0; i<words.length; i++ ) {
          if ( words[i].equals(word) ) {
            occurrences[i]++;
          }
        }
      }
    } finally {
      if(reader!=null) reader.close();
    }
    return occurrences;
  }
}

顺便说一句java.util.Map非常适合构建频率表...并行数组只是 90 年代的事情。 Map 的“默认”实现是 HashMap 类...默认情况下,我的意思是只要您需要 Map 就使用 HashMap,除非您有充分的理由使用其他东西,而这不会经常。 HashMap 通常是最好的全能执行器。

Paul,

I suggest you read through this old thread: Java BufferedReader back to the top of a text file?.

Personally I prefer Jon Skeet's response, which boils down to "Don't bother [unless you MUST]."

Cheers. Keith.


EDIT: Also you should ALLWAYS close that input file, even if you hit an Exception. The finally block is perfect for this.


EDIT2:

Hope you're still with us.

Here's my attempt, and FWIW, you DON'T need to reset the input-file, you just need to transpose your while and for loops.

package forums;

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class WordOccurrenceCount
{
  public static void main(String[] args) {
    try {
      String[] words = { "and", "is", "a", "the", "of", "as" };
      int[] occurrences = readOccurrences("C:/tmp/prose.txt", words);
      for ( int i=0; i<words.length; i++ ) {
        System.out.println(words[i] + " " + occurrences[i]);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static final int[] readOccurrences(String filename, String... words) 
    throws IOException
  {
    int[] occurrences = new int[words.length];
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(filename));
      Scanner scanner = new Scanner(reader);
      while ( scanner.hasNext() ) {
        String word = scanner.next();
        for ( int i=0; i<words.length; i++ ) {
          if ( words[i].equals(word) ) {
            occurrences[i]++;
          }
        }
      }
    } finally {
      if(reader!=null) reader.close();
    }
    return occurrences;
  }
}

And BTW, java.util.Map is perfect for building a frequency table... Parallel arrays are just SOOOOO 90's. The "default" implementation of Map is the HashMap class... By default I mean use HashMap whenever you need a Map, unless you've got a good reason to use something else, which won't be often. HashMap is generally the best allround performer.

药祭#氼 2024-11-11 13:45:29

您有两个选择:

  • 重置 FileReader (FieldReader.reset) 并创建一个新的 bufferedreader。
  • 使用标记功能(尝试 BufferedReader.markSupported )

我认为您尝试的是在读取输入后调用标记。相反,请执行以下操作:

inp.mark(readAheadLimit);    
// .... all your code processing input
inp.reset();

You have two options:

  • reset the FileReader (FieldReader.reset) instead and create a new bufferedreader.
  • use mark functionaliy (try BufferedReader.markSupported)

I think what you tried is to call mark after you read the input. Instead do the following:

inp.mark(readAheadLimit);    
// .... all your code processing input
inp.reset();
烂人 2024-11-11 13:45:29

mark(int readAheadLimit ) 接受一个 readAheadLimit 参数。

您不应将 readAheadLimit 设置为 0。尝试使用大于您在迭代中读取的字节数的有意义的数字。

mark(int readAheadLimit) takes a readAheadLimit parameter.

You should not set the readAheadLimit to 0. Try using a meaningful number that is larger than number of bytes you read in an iteration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文