简体   繁体   English

Java语法错误缓冲的读取器

[英]Java syntax error buffered reader

I'm writing a class that just reads a text file and prints out the lines. 我正在编写一个只读取文本文件并打印出行的类。 I'm getting an error on the line containing BufferedReader rd = new BufferedReader(new FileReader("file.txt")); 我在包含BufferedReader的行上遇到错误rd = new BufferedReader(new FileReader(“ file.txt”)); saying that Syntax error on token ";", { expected after this token. 说令牌“;”上的语法错误,{应该在此令牌之后。 I've tried placing it within a method, and within a try catch block as it recommends but then I'm unable to resolve the rd variable. 我尝试将其放置在方法中,并按照建议将其放置在try catch块中,但随后我无法解析rd变量。 I'm using the acm package so some of the other syntax may look different but I receive no other errors. 我使用的是acm包,因此其他语法可能看起来有所不同,但没有收到其他错误。 Any help would be greatly appreciated =) 任何帮助将不胜感激=)

import acm.program.*;
import acm.util.*;
import java.io.*;
import java.util.*;

public class FileReading extends ConsoleProgram {   

BufferedReader rd = new BufferedReader(new FileReader("file.txt"));

try {
    while (true) {          
        String line = rd.readLine();
        if (line == null) {
            break;
        }
        println(line);
    }
    rd.close();
}
catch (IOException ex) {
    throw new ErrorException(ex);
    }
}
}

Code blocks like this should be embodied inside a method or a static clause. 此类代码块应包含在方法或静态子句中。 Something like: 就像是:

public class FileReading extends ConsoleProgram {   

   public void readFile(){
     BufferedReader rd = null;
     try {
         rd = new BufferedReader(new FileReader("file.txt"));
         while (true) {          
            String line = rd.readLine();
            if (line == null) {
                break;
            }
            println(line);
         }
     }catch (IOException ex) {
         throw new ErrorException(ex);
     }finally{
        try{
         rd.close();
        }catch (IOException ex) {
         throw new ErrorException(ex);
        }
     }
   }
}

As answered by others, you cant provide your code in the general part of the class, it has to be within a method or static block. 就像其他人回答的那样,您不能在类的常规部分中提供代码,它必须位于方法或静态块内。

By putting the code block in the constructor the problem went away. 通过将代码块放入构造函数中,问题就解决了。 See below for example. 参见以下示例。

import acm.program.*;
import acm.util.*;
import java.io.*;
import java.util.*;

public class FileReading extends ConsoleProgram {   
public FileReading()
{
BufferedReader rd = new BufferedReader(new FileReader("file.txt"));

try {
    while (true) {          
        String line = rd.readLine();
        if (line == null) {
            break;
        }
        println(line);
    }
    rd.close();
}
catch (IOException ex) {
    throw new ErrorException(ex);
    }
}
}
}

Create a method... and inside that do this... Not directly inside the class 创建一个方法...并在内部执行此操作...不直接在类内部

eg: 例如:

public void go()
{

    BufferedReader rd = new BufferedReader(new FileReader("file.txt"));

try {
    while (true) {          
        String line = rd.readLine();
        if (line == null) {
            break;
        }
        println(line);
    }
    rd.close();
}
catch (IOException ex) {
    throw new ErrorException(ex);
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM