Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java从文件加载带有嵌套ArrayList的ArrayList_Java_Arraylist_Nested_Filereader_Filewriter - Fatal编程技术网

使用Java从文件加载带有嵌套ArrayList的ArrayList

使用Java从文件加载带有嵌套ArrayList的ArrayList,java,arraylist,nested,filereader,filewriter,Java,Arraylist,Nested,Filereader,Filewriter,我有一个类似于arraylist的arraylist。我使用以下代码将主ArrayList保存在文本文件中 ArrayList<ArrayList<String>> ar2 = new ArrayList<>(); FileWriter writer = new FileWriter("path/to/the/text/file"); for(ArrayList<String> str: ar2) {

我有一个类似于
arraylist
的arraylist。我使用以下代码将主ArrayList保存在文本文件中

 ArrayList<ArrayList<String>> ar2 = new ArrayList<>();
 FileWriter writer = new FileWriter("path/to/the/text/file");
        for(ArrayList<String> str: ar2) {
            writer.write(str + System.lineSeparator());

        }
        writer.close();
ArrayList ar2=新的ArrayList();
FileWriter=newfilewriter(“path/to/the/text/file”);
用于(ArrayList str:ar2){
writer.write(str+System.lineSeparator());
}
writer.close();

现在,我想在每次新运行时将保存的数据从文件加载到相同的
ArrayList ar2
。我尝试了几种方法,但嵌套的ArrayList是作为字符串加载的。如何克服这个问题?

您可以解析字符串并使用迭代将其转换为数组,也可以使用一个库来完成这项工作


我建议您使用Jackson,您可以找到一个简单的教程。

您可以解析字符串并使用迭代将其转换为数组,也可以使用一个库为您完成此操作


我建议您使用Jackson,您可以找到一个简单的教程。

阅读文件内容,按行分隔符拆分,然后删除括号并再次拆分以获得列表项。差不多

File file = new File("path/to/the/text/file");
FileReader reader = new FileReader(file);

char[] chars = new char[(int) file.length()];
reader.read(chars);

String fileContent = new String(chars);
ArrayList<ArrayList<String>> readList = new ArrayList<>();

String[] splittedArr = fileContent.split(System.lineSeparator());
for(String list : splittedArr) {
    list = list.replace("[", "").replace("]", ""); //removing brackets as the file will have list as [a, b, c]
    List<String> asList = Arrays.asList(list.split(", "));//splitting the elements by comma and space
    readList.add(new ArrayList<>(asList));
}
reader.close();
File File=new文件(“path/to/the/text/File”);
FileReader=新的FileReader(文件);
char[]chars=new char[(int)file.length()];
读卡器,读卡器;
字符串fileContent=新字符串(字符);
ArrayList readList=新建ArrayList();
String[]splittedArr=fileContent.split(System.lineSeparator());
for(字符串列表:splittedArr){
list=list.replace(“[”,”).replace(“]”,”);//删除括号,因为文件的列表为[a,b,c]
List asList=Arrays.asList(List.split(“,”);//按逗号和空格拆分元素
add(newarraylist(asList));
}
reader.close();

读取文件内容,按行分隔符拆分,然后删除括号并再次拆分以获取列表项。差不多

File file = new File("path/to/the/text/file");
FileReader reader = new FileReader(file);

char[] chars = new char[(int) file.length()];
reader.read(chars);

String fileContent = new String(chars);
ArrayList<ArrayList<String>> readList = new ArrayList<>();

String[] splittedArr = fileContent.split(System.lineSeparator());
for(String list : splittedArr) {
    list = list.replace("[", "").replace("]", ""); //removing brackets as the file will have list as [a, b, c]
    List<String> asList = Arrays.asList(list.split(", "));//splitting the elements by comma and space
    readList.add(new ArrayList<>(asList));
}
reader.close();
File File=new文件(“path/to/the/text/File”);
FileReader=新的FileReader(文件);
char[]chars=new char[(int)file.length()];
读卡器,读卡器;
字符串fileContent=新字符串(字符);
ArrayList readList=新建ArrayList();
String[]splittedArr=fileContent.split(System.lineSeparator());
for(字符串列表:splittedArr){
list=list.replace(“[”,”).replace(“]”,”);//删除括号,因为文件的列表为[a,b,c]
List asList=Arrays.asList(List.split(“,”);//按逗号和空格拆分元素
add(newarraylist(asList));
}
reader.close();