【问题标题】:how to read next line in .txt file如何读取 .txt 文件中的下一行
【发布时间】:2012-08-07 04:15:31
【问题描述】:

我可以知道为什么我在上传 .txt 文件时无法读取下一行。 它发生在我尝试上传的所有 .txt 文件,然后是 system.out.println() 。 在我的文本文件中,它包含:cats,dogs,monkey(每个都在一行中).. 但输出的值是:

[Monkey][Monkey, null][Monkey, null, null][Monkey, null, null, null][Monkey, null, null, null, null][Monkey, null, null, null, null, null][Monkey, null, null, null, null, null, null]

在这方面需要帮助。 谢谢。

并想知道为什么我可以读取 .txt 文件而不是 .doc。也需要这方面的建议。

import java.awt.List;
import java.io.*;  
import java.util.ArrayList;

import javax.imageio.IIOException;
import javax.swing.JFileChooser;

public class searchforCapitalLetter {

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


          try {  

                // file chooser
              JFileChooser chooser=new  JFileChooser();
            int returnVal = chooser.showOpenDialog(null);{
            if(returnVal == JFileChooser.APPROVE_OPTION)

            {   File f = chooser.getSelectedFile();}
            FileInputStream fin = new FileInputStream(chooser.getSelectedFile());
            DataInputStream din = new DataInputStream(fin);    
       BufferedReader br = new BufferedReader(new InputStreamReader(din)); 

    ArrayList<String>list =new ArrayList<String> ();

    if ((br.readLine()) != null) {
    while (br.readLine() != " ") {
        list.add(br.readLine());
        System.out.print (list);
    } br.close() ;
    }//closes if statement
     } // closes method dialog
                } // closes try method

         catch (FileNotFoundException e) {
                    e.printStackTrace();
                }     // closes catch method
            } // closes method body

} // closes class method

【问题讨论】:

  • 请不要包含注释代码。
  • 另外,你应该真正养成一致缩进的习惯;它将为您节省大量调试时间。
  • 您甚至没有添加ActionListener 以在选择文件时进行监听。
  • 如果你想阅读文本,请不要使用DataInputStream,它比有用更混乱。

标签: java amazon-s3 amazon


【解决方案1】:

根据 bufferedreader api:

公共字符串 readLine() 抛出 IOException

读取一行文本。一行被认为是由换行符('\n')中的任何一个终止,a 回车 ('\r'),或回车后紧跟 换行。

返回:包含行内容的字符串,不包括 任何行终止字符,如果流的末尾有 已到达

抛出:IOException - 如果发生 I/O 错误

请注意,您的代码出现了一些错误:

1) 它调用 readline() 一次检查返回值并再次使用返回值。第一次调用(在 while 条件下)从缓冲区中删除当前值,因此您每隔一行删除一次。

2) 您使用了错误的比较来查看是否有剩余数据。 x != y,其中 x 和 y 都是对象,检查指针是否相等 - 是分配 x 的内存位置与分配 y 的内存位置相同。您真正想要的是将值存储在变量中。

例如:

BufferedReader in = ...;
String s = in.readLine();
while(s!=null){
    ...
    s = in.readLine();
}

【讨论】:

    【解决方案2】:

    您不能使用 .doc 的原因是 .doc 文件已格式化,因此,您的代码必须解析某些内容才能从中读取。

    就奇怪的打印而言,您甚至在打印之前阅读了两次代码(每次调用 .readLine,它都会将扫描仪移动到下一行)。试试下面的代码:

    ArrayList<String>list =new ArrayList<String> ();
    String currentLine;
    while ((currentLine = br.readLine()) != null) {
        list.add(currentLine);
        System.out.println(currentLine);
    } br.close() ;
    }
    

    这将跟踪变量中的当前行,而不是反复将扫描仪移动到下一行。另一个问题是到达文件末尾时它将为空,而不是“”

    【讨论】:

      【解决方案3】:

      用于从文件中读取内容的经典 BufferedReader。 这是我们用 Java 读取的文件:

      这是要写入文件的内容

      这是要写入文件的内容

      package com.stackoverflow;
      
      import java.io.BufferedReader;
      import java.io.FileReader;
      import java.io.IOException;
      
      public class ReadFile{
      
          private static final String FILENAME = "D:\\test\\filename.txt";
      
          public static void main(String[] args) {
      
              BufferedReader br = null;
              FileReader fr = null;
      
              try {
      
                  fr = new FileReader(FILENAME);
                  br = new BufferedReader(fr);
      
                  String sCurrentLine;
      
                  br = new BufferedReader(new FileReader(FILENAME));
      
                  while ((sCurrentLine = br.readLine()) != null) {
                      System.out.println(sCurrentLine);
                  }
      
              } catch (IOException e) {
      
                  e.printStackTrace();
      
              } finally {
      
                  try {
      
                      if (br != null)
                          br.close();
      
                      if (fr != null)
                          fr.close();
      
                  } catch (IOException ex) {
      
                      ex.printStackTrace();
      
                  }
      
              }
      
          }
      
      }
      

      输出:

      这是要写入文件的内容

      这是要写入文件的内容

      【讨论】:

        猜你喜欢
        • 2017-02-12
        • 2019-03-02
        • 1970-01-01
        • 2020-12-17
        • 2014-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        相关资源
        最近更新 更多