2016-09-25 57 views
-3

并在Eclipse构建包:内容不能得到解决

public static String getContents(File aFile) 
    { 
    contents = new StringBuffer(); 
    BufferedReader input = null; 
    try 
    { 
     input = new BufferedReader(new FileReader(aFile)); 
     String line = null; 
     while ((line = input.readLine()) != null) { 
     contents.append(line); 
     } 
     return contents.toString(); 
    } 
    catch (FileNotFoundException ex) 
    { 
     ex.printStackTrace(); 
    } 
    catch (IOException ex) 
    { 
     ex.printStackTrace(); 
    } 
    finally 
    { 
     try 
     { 
     if (input != null) { 
      input.close(); 
     } 
     } 
     catch (IOException ex) 
     { 
     ex.printStackTrace(); 
     } 
    } 
    } 

我们得到了以下三个错误:

  • 内容不能被解析(上线10号)

  • 内容无法解析(在线数12)

  • 的内容不能被解析为一个变量(在3号线)

我们使用的是Eclipse霓虹灯(4.6.0)和Java jdk1.8.0_102

已经尝试过干净,在Eclipse

刷新
+0

Java中的所有代码都应该在类中。在一个侧面说明,很难找到基于行号的行,没有看到任何 – eavidan

+0

现在我已经粘贴完整的类 – Aly

+0

不,你没有,只有方法...粘贴ENTIRE类开始'公共类.. ' – pczeus

回答

0

您定义内容变量错误。要在Java中定义变量,你需要从一个类型开始,然后是变量名和初始化表达式(这是可选的)。

所以内容必须定义如下:

StringBuffer contents = new StringBuffer(); 

编辑

如果你不需要的同步支持,你应该使用StringBuilder的代替的StringBuffer

+0

好的尝试了,它说这个方法必须返回一个String类型的结果'public static String getContents(File aFile)' – Aly

+0

你在方法中遗漏了return语句。看来你需要在方法的末尾添加返回contents.toString() –

+0

它刚好在第一个catch'contents.toSring();' – Aly

相关问题