1

我是 Java 新手,这一切都是自学的。我喜欢使用代码,这只是一种爱好,所以,我没有接受过任何关于这个主题的正规教育。

我现在正在学习从文本文件中读取。我得到的代码不正确。当我硬编码确切的行数时它可以工作,但是如果我使用“for”循环来感知有多少行,它就不起作用。

我已经对它进行了一些修改。这是我现在的位置:

这是我的主要课程

package textfiles;

import java.io.IOException;

public class FileData {

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

    String file_name = "C:/Users/Desktop/test.txt";


        ReadFile file = new ReadFile(file_name);
        String[] aryLines = file.OpenFile();
        int nLines = file.readLines();
        int i = 0;            

    for (i = 0; i < nLines; i++) {
        System.out.println(aryLines[i]);
      }
    }    
  }

这是我的课程,它将读取文本文件并感知行数

package textfiles;

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

public class ReadFile {

private String path;

public ReadFile(String file_path) {
    path = file_path;
}
int readLines() throws IOException {

  FileReader file_to_read = new FileReader(path);
  BufferedReader bf = new BufferedReader(file_to_read);

  int numberOfLines = 0;
  String aLine;

  while ((aLine = bf.readLine()) != null) {
  numberOfLines++;
}
bf.close();
return numberOfLines;
}

public String[] OpenFile() throws IOException {

  FileReader fr = new FileReader(path);
  BufferedReader textReader = new BufferedReader(fr);

  int numberOfLines = 0;

  String[] textData = new String[numberOfLines];

  int i;

 for (i = 0; i < numberOfLines; i++) {
    textData[i] = textReader.readLine();
  }

textReader.close();
return textData;
 }
}

请记住,我是自学成才的;我可能没有正确缩进,或者我可能会犯一些简单的错误,但不要粗鲁。有人可以看看这个,看看为什么它没有检测到行数(int numberOfLines)以及为什么除非我硬编码方法中的行数,否则它不会工作readLines()

4

3 回答 3

2

问题是,您将要读取的行数设置为零int numberOfLines = 0;

我宁愿建议对行使用列表,然后将其转换为数组。

public String[] OpenFile() throws IOException {

  FileReader fr = new FileReader(path);
  BufferedReader textReader = new BufferedReader(fr);

  //int numberOfLines = 0; //this is not needed

  List<String> textData = new ArrayList<String>(); //we don't know how many lines are there going to be in the file

  //this part should work akin to the readLines part
  String aLine;
  while ((aLine = bf.readLine()) != null) {
      textData.add(aLine); //add the line to the list
  }

  textReader.close();
  return textData.toArray(new String[textData.size()]); //convert it to an array, and return
 }
}
于 2013-09-09T14:22:29.673 回答
0

这是我不久前写的一门课程,我认为您可能会觉得有帮助。

public class FileIO {
  static public String getContents(File aFile) {
    StringBuilder contents = new StringBuilder();
    try {
        //use buffering, reading one line at a time
        //FileReader always assumes default encoding is OK!
        BufferedReader input = new BufferedReader(new FileReader(aFile));
        try {
            String line = null; //not declared within while loop
            /*
             * readLine is a bit quirky :
             * it returns the content of a line MINUS the newline.
             * it returns null only for the END of the stream.
             * it returns an empty String if two newlines appear in a row.
             */
            while ((line = input.readLine()) != null) {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
    }
    return contents.toString();
}

static public File OpenFile()
{
    return (FileIO.FileDialog("Open"));
}

static private File FileDialog(String buttonText) 
{
    String defaultDirectory = System.getProperty("user.dir");
    final JFileChooser jfc = new JFileChooser(defaultDirectory);
    jfc.setMultiSelectionEnabled(false);
    jfc.setApproveButtonText(buttonText);
    if (jfc.showOpenDialog(jfc) != JFileChooser.APPROVE_OPTION) 
    {
        return (null);
    }
    File file = jfc.getSelectedFile();
    return (file);
}
}

它用于:

  文件文件 = FileIO.OpenFile();

它是专门为读取文件而设计的,因此希望它可以成为您学习中的有用示例。

于 2013-09-09T14:24:21.530 回答
0
int numberOfLines = 0;
String[] textData = new String[numberOfLines];

textData是一个空数组。下面的for循环不会做任何事情。

另请注意,这不是逐行读取文件的最佳方式。这是有关如何从文本文件中获取行的正确示例:

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
ArrayList<String> list = new ArrayList<String>();
while ((line = br.readLine()) != null) {
   list.add(line);
}
br.close();

我还建议您阅读有关面向对象概念的教程。

于 2013-09-09T14:17:32.967 回答