Java文本输入:如何忽略以某些字符开头的行?

Java文本输入:如何忽略以某些字符开头的行?,java,lines,readfile,textreader,Java,Lines,Readfile,Textreader,我基本上想忽略某些带有字符的行,比如如果有一行 // hello, i'm bill 我想在阅读时忽略该行,因为它包含字符“/”。我该怎么做?我尝试了方法skip(),但它给了我错误 public String[] OpenFile() throws IOException { FileReader reader = new FileReader(path); BufferedReader textReader = new BufferedReader(reader); int

我基本上想忽略某些带有字符的行,比如如果有一行

// hello, i'm bill
我想在阅读时忽略该行,因为它包含字符“/”。我该怎么做?我尝试了方法skip(),但它给了我错误

public String[] OpenFile() throws IOException {

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

  int numberOfLines = readLines();
  String[] textData = new String[numberOfLines];
  int i;

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

  // close the line-by-line reader and return the data
  textReader.close();
  return textData;
}

int readLines() throws IOException {
  FileReader reader = new FileReader(path);
  BufferedReader textReader = new BufferedReader(reader);
  String line;
  int numberOfLines = 0;

  while ((line = textReader.readLine()) != null) { 
    // I tried this:
    if (line.contains("//")) {
      line.skip();  
    }
    numberOfLines++;       
  }    
  reader.close();  
  return numberOfLines;
}

正如Andrew Thompson指出的,最好将文件逐行读取到ArrayList中。伪代码:

 For Each Line In File
   If LineIsValid()
     AddLineToArrayList()
 Next
更新以修复实际代码:

try{
 ReadFile files = new ReadFile(file.getPath());
 String[] anyLines = files.OpenFile();
 }
public String[] OpenFile() throws IOException {

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

  int numberOfLines = readLines();
  String[] textData = new String[numberOfLines];
  int BufferIndex = 0;
  String line;

  while ((line = textReader.readLine()) != null) {
    if (line.trim().startsWith("//")) {
      // Don't inject current line into buffer
    }else{
       textData[BufferIndex] = textReader.readLine();
       BufferIndex = BufferIndex + 1;
    }      
  }

  // close the line-by-line reader and return the data
  textReader.close();
  return textData;
}
while ((line = textReader.readLine()) != null) {
    if (line.trim().startsWith("//")) {
      // do nothing
    }else{
      numberOfLines++;
    }      
}
在ReadLines()函数中:

try{
 ReadFile files = new ReadFile(file.getPath());
 String[] anyLines = files.OpenFile();
 }
public String[] OpenFile() throws IOException {

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

  int numberOfLines = readLines();
  String[] textData = new String[numberOfLines];
  int BufferIndex = 0;
  String line;

  while ((line = textReader.readLine()) != null) {
    if (line.trim().startsWith("//")) {
      // Don't inject current line into buffer
    }else{
       textData[BufferIndex] = textReader.readLine();
       BufferIndex = BufferIndex + 1;
    }      
  }

  // close the line-by-line reader and return the data
  textReader.close();
  return textData;
}
while ((line = textReader.readLine()) != null) {
    if (line.trim().startsWith("//")) {
      // do nothing
    }else{
      numberOfLines++;
    }      
}
基本上,你在正确的轨道上

注意:您可能对

请注意,
continue
可能看起来有点像过去时,容易受到批评


编辑以下内容(注意,这不需要countLines方法)

public String[]OpenFile()引发IOException{
FileReader=新的FileReader(路径);
BufferedReader textReader=新的BufferedReader(读卡器);
List textData=new LinkedList();//避免realloc的链表
弦线;
而((line=textReader.readLine())!=null){
如果(!line.包含(“/”)textData.add(line);
}
//关闭逐行读卡器并返回数据
textReader.close();
返回textData.toArray(新字符串[textData.size()]);
}

在这种情况下批评
goto
的人不理解为什么
goto
被认为是有害的。Dijkstra反对无限制的
goto
+1我个人喜欢
继续。我不认为它比《代码》中的《转到》更糟糕。我认为它更像是
break
,它肯定不那么容易受到批评,因为它绝对是在中盘结束循环的最佳方式。
Continue
是100%完全有效的,将其与
goto
进行比较是一种伤害
Continue
break
@lrvillnius非常相似,它确实回答了提出的问题,并且在不使用字符串的虚构方法的情况下工作。如果你得到错误的输出,那是因为你不明白自己在做什么,也不知道如何预测行为。我确实说过,可能在
continue
方面,它不起作用-我想跳过以这个符号开头的行-//并在其中包含文本。我尝试了你的方法,但是那些行仍然被输出。你在上面发布了你的实际代码吗?另外,您能解释一下代码的用途吗?是否要计算行数、输出行数、将行转储到缓冲区中?你说的“输出”是什么意思?您的代码示例不包含任何字符串输出。如果@hamlin11或@ratchet的代码不适用于您,则说明您对其进行了错误的调整。在我的主要方法中,我通过创建一个数组,使用System.out.println输出字符串。我现在谈论的是我创建的一个文件读取类。我能够修复导致代码中出现问题的问题——这在粘贴完整代码时成为可能。幸运的是,这是编译吗?我在String类中找不到“skip”方法。您知道在编辑后粘贴的代码中,文件被读取了两次吗?
readLines()
方法更准确地称为
countLines()
countnonmentlines()
。通过将行读入可根据需要扩展的结构(如
ArrayList
),整个练习将更简洁、更快,因为您似乎只使用计数来确定
String[]
的大小。它不读取文件两次,只计算文件的行数并输出它们(在我的主方法中)@Irvillius:你读了两遍。首先计算行数(发布的解决方案不会按您的意愿计算
/
行数),然后返回主函数并输出所有行(发布的解决方案未应用)。但是,行数计算错误,因此只显示前N行,其中N是非
/行数。这就解释了你的“切断”。