1

我有一个包含数据的文本文件。该文件包含所有月份的信息。假设一月份的信息占据了 50 行。比二月开始,它占用了 40 多行。比我三月等...是否可以只读取文件的一部分?我可以说“从 X 行读到 Y 行”吗?还是有更好的方法来做到这一点?我只想打印一个月而不是所有文件的数据。这是我的代码

public static void readFile()
{
  try
  {
     DataInputStream inputStream = 
     new DataInputStream(new FileInputStream("SpreadsheetDatabase2013.txt"));

   while(inputStream.available() != 0)
   {
     System.out.println("AVAILABLE: " + inputStream.available());
     System.out.println(inputStream.readUTF());
     System.out.println(inputStream.readInt());
     for (int i = 0; i < 40; i++)
     {
       System.out.println(inputStream.readUTF());
       System.out.println(inputStream.readUTF());
       System.out.println(inputStream.readUTF());
       System.out.println(inputStream.readUTF());
       System.out.println(inputStream.readUTF());
       System.out.println(inputStream.readDouble());
       System.out.println(inputStream.readUTF());
       System.out.println(inputStream.readBoolean());
       System.out.println();
     }
   }// end while
   inputStream.close();
 }// end try
 catch (Exception e)
 {
   System.out.println("An error has occurred.");
 }//end catch
}//end method

感谢您的时间。

4

4 回答 4

0

根据你说你的数据是如何组织的,我建议你做这样的事情

ArrayList<String> temp = new ArrayList<String>();
int read = 0;
File file = new File(filePath);
if (file.exists()) {
    BufferedReader brin;
    brin = new BufferedReader(new FileReader(filePath));
    String line = brin.readLine();
    while (line != null) {
        if (!line.equals("")){
            if(line.equals("March"))
                read = 1;
            else if(line.equals("April"))
                break;
            else if(read == 1)
                temp.add(line);
        }
        line = brin.readLine();
    }
    brin.close();

自己试了一下,3月到4月的数据都录进去了。您可以根据需要调整它们或使它们成为变量。感谢 ngoa 的基础代码。信用到期的信用

于 2013-01-03T17:48:47.873 回答
0

我会使用扫描仪类

Scanner scanner = new Scanner(filename);

用于scanner.nextLine()获取文件的每一行。如果您只想从第 x 行到第 y 行,您可以使用 for 循环扫描您不需要的每一行,然后再通过扫描仪查找您需要的行。小心不要在不抛出异常的情况下遇到异常。

或者,您可以通过扫描仪并将每一行的字符串内容添加到 ArrayList。祝你好运。

于 2013-01-03T17:51:09.550 回答
0

如果你有 Java 7,你可以使用Files.readAllLines(Path path, Charset cs),例如

Path path = // Path to "SpreadsheetDatabase2013.txt"
Charset charset = // "UTF-8" or whatever charset is used
List<String> allLines = Files.readAllLines(path, charset);
List<String> relevantLines = allLines.subList(x, y);

其中x(inclusive) 和y(exclusive) 表示感兴趣的行号,请参见 List.subList(int fromIndex, int toIndex)

如 JavaDoc 中所述,此解决方案的一个好处是readAllLines()

此方法确保在读取所有字节或引发 I/O 错误或其他运行时异常时关闭文件。

于 2013-01-03T17:48:26.850 回答
0

我的方法是读取文本文件的全部内容并将其存储在 ArrayList 中,并仅读取请求月份的行。

例子:

使用此函数从文件中读取所有行。

/**
 * Read from a file specified by the filePath.
 * 
 * @param filePath
 *            The path of the file.
 * @return List of lines in the file.
 * @throws IOException
 */

public static ArrayList<String> readFromFile(String filePath)
        throws IOException {
    ArrayList<String> temp = new ArrayList<String>();
    File file = new File(filePath);
    if (file.exists()) {
        BufferedReader brin;
        brin = new BufferedReader(new FileReader(filePath));
        String line = brin.readLine();
        while (line != null) {
            if (!line.equals(""))
                temp.add(line);
            line = brin.readLine();
        }
        brin.close();
    }
    return temp;
}

然后从 ArrayList temp中只读取您需要的那些。

例子:

如果您想读取 2 月份的数据,假设它有 50 行数据并从第 40 行开始。

for(int i=40;i<90;i++)
{
    System.out.println(temp.get(i));
}

注意:这只是执行此操作的一种方式。我不确定是否还有其他方法!

于 2013-01-03T17:35:53.840 回答