0

我在 String readFileIntoString(String path) 中遇到错误,因为表达式的非法开头有人可以告诉我该怎么做。

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 " ";
        }
    }
}  
4

2 回答 2

2

方法内部的方法被认为是illegal start of expression

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

移动

    public String readFileIntoString(String path) {} 

到外面main()

于 2013-09-06T18:51:45.640 回答
1
  1. 方法应该在外面main()
  2. 您需要在内部 try{} 之后有一个 catch{} 块或 finally{} 块

try{}块后应始终跟随catch{}finally{}

于 2013-09-06T18:53:58.757 回答