我创建了一个字符串数组String[] arr
来保存我文件中的所有内容:
String[] contentarr=file.openfile();
稍后当我尝试将此字符串数组转换回字符串时使用:
String contentstr=Arrays.toString(contentarr);
它一直给我错误arr
无法解析为变量。任何线索为什么会这样?
更新的代码(学习Java周1.尝试在线学习一些教程但不确定如何解决此错误):
package readText;
import java.io.IOException;
import java.util.Arrays;
public class FileData {
public static void main(String[] args) throws Exception {
String file_path1 = "F:\\CampusImages.txt";
String file_path2 = "F:\\CampusImageIDs.txt";
try{
ReadFileLocal file = new ReadFileLocal(file_path1);
String[] aryLines = file.openFile(); //Already wrote openFile function in another file.
int i;
for (i=0; i<aryLines.length; i++){
System.out.println(aryLines[i]);
}
}
catch(IOException e) {
System.out.println(e.getMessage());
}
String all=Arrays.toString(aryLines); //ERROR: aryLines cannot be resolved as a variable.
WriteFile content = new WriteFile( file_path2, true);
content.WriteToFile("Write from here: ");
System.out.println(content); // Also, is the writing part code right?
}
}
答案 0 :(得分:0)
Arylines不在范围内。
String file_path1 = "F:\\CampusImages.txt";
String file_path2 = "F:\\CampusImageIDs.txt";
String[] arylines;
try{
ReadFileLocal file = new ReadFileLocal(file_path1);
aryLines = file.openFile();
int i;
for (i=0; i<aryLines.length; i++){
System.out.println(aryLines[i]);
}
}
catch(IOException e) {
System.out.println(e.getMessage());
}
String all=Arrays.toString(aryLines); //now arylines is resolved
WriteFile content = new WriteFile( file_path2, true);
content.WriteToFile("Write from here: ");
System.out.println(content); // Also, is the writing part code righ