读取文件时非法开始表达

时间:2013-09-06 18:50:01

标签: java netbeans bufferedreader

我在String readFileIntoString(字符串路径)中收到错误,因为有人告诉我该做什么,非法启动表达式。

public static void main(String[] args) {

//public class PreProcess {

    // Read a file into a string. Takes file path, returns string

    /**
     *
     * @param path
     * @return
     */
    public String readFileIntoString(String path) {
        char[] line = new char[1024];
        StringBuilder dataString;
        dataString = new StringBuilder(5000);

        try {
            try (BufferedReader input = new BufferedReader(new FileReader(path))) {
                while (true) {
                    int readLength = input.read(line);
                    if (readLength == -1)
                        break;
                    dataString.append(line, 0, readLength);
                }
            }
            return dataString.toString();
        }
        catch (IOException e) {
            return " ";
        }
    }
}  

2 个答案:

答案 0 :(得分:2)

方法内部方法被视为illegal start of expression

public static void main(String[] args) {
    public String readFileIntoString(String path) {
    }
}

移动

    public String readFileIntoString(String path) {} 

main()

的一边

答案 1 :(得分:1)

  1. 该方法应在main()之外。
  2. 在内部尝试{}
  3. 之后,您需要有一个catch {}块或一个finally {}块

    try{}块应始终跟在catch{}finally{}

    之后