static void parse(String fileName) throws IOException{
FileReader fileReader=new FileReader((fileName));
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder=new StringBuilder();
String string;
StringBuilder myString = null;
while((string = bufferedReader.readLine()) != null) {
myString =stringBuilder.append(string);
String h=myString.toString();
**System.out.println(h);**
}
}
static void parse(String fileName) throws IOException{
FileReader fileReader=new FileReader((fileName));
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder=new StringBuilder();
String string;
StringBuilder myString = null;
while((string = bufferedReader.readLine()) != null) {
myString =stringBuilder.append(string);
String h=myString.toString();
}
**System.out.println(h);**
}
当我尝试代码的第二部分时,它什么也没有打印出来。如何在 while 循环之外获取整个 h 字符串?我是否必须将变量 h 声明为实例变量而不是局部变量?