Java 内容无法解析
在eclipse中构建包时:Java 内容无法解析,java,Java,在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
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中尝试了清理和刷新,您定义的内容变量错误。要在Java中定义变量,需要从类型开始,然后是变量名和初始化表达式(顺便说一下,这是可选的) 因此,内容必须定义如下:
StringBuffer contents = new StringBuffer();
编辑
如果不需要同步支持,则应使用StringBuilder类而不是StringBuffer类中的所有Java代码。另一方面,很难找到基于行号的行,没有看到任何现在我已经粘贴了完整的类没有你没有,只有方法…粘贴整个类以“public class…”开始好的,它表示此方法必须返回String类型的结果
publicstaticstringgetcontents(fileafile)
方法中缺少return语句。似乎您需要在方法末尾添加return contents.toString(),它就在第一个catchcontents.toString()之前代码>方法在任何情况下都必须返回一些值。在try块中添加return语句,这意味着在读取文件时没有错误时将调用它。但若出现错误,将调用catch块,并且这里并没有return语句。我建议您在方法的末尾编写一条return语句,并且在方法中有一条return语句。您的代码将编译并变得更可读