Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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_Java_Arraylist - Fatal编程技术网

Java 从文件读取到arraylist

Java 从文件读取到arraylist,java,arraylist,Java,Arraylist,我有一个文件,其中包括这些数字,用空格和线分隔 1 11 23 1 18 9 15 23 5 11 1 18 1 20 5 11 1 我想把它们放在ArrayList中,一行放在一个ArrayList中(没有空格,只有数字),另一行放在另一个ArrayList中 我尝试了很多方法,将数字加载到Char数组,再加载到String,然后加载到Char数组(但十进制数被分隔),等等 我的代码: Scanner plik1 = new Scanner( new File("plik1.txt") );

我有一个文件,其中包括这些数字,用空格和线分隔

1 11 23 1 18 9 15 23 5
11 1 18 1 20 5 11 1
我想把它们放在ArrayList中,一行放在一个ArrayList中(没有空格,只有数字),另一行放在另一个ArrayList中

我尝试了很多方法,将数字加载到Char数组,再加载到String,然后加载到Char数组(但十进制数被分隔),等等

我的代码:

Scanner plik1 = new Scanner( new File("plik1.txt") );

ArrayList<Integer> list = new ArrayList<Integer>();

    while (plik1.hasNext()){
        list.add(plik1.nextInt());
    }

    System.out.print("strin1 : " + list);
但是,我希望它看起来像:

strin1 : [1, 11, 23, 1, 18, 9, 15, 23, 5]
strin2 : [11, 1, 18, 1, 20, 5, 11, 1]

您可以创建一个列表,如下所示:

Scanner plik1 = new Scanner( new File("plik1.txt") );

ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

while (plik1.hasNextLine()){
    String temp = plik1.nextLine();
    String[] splitString = temp.split(" ");
    ArrayList<Integer> tempList = new ArrayList<Integer>();
    list.add(tempList);
    for(int i = 0; i < splitString.length; i++)
    {
       tempList.add(Integer.parseInt(splitString[i]));
    }
}

System.out.print("strin1 : " + list);
Scanner plik1=新扫描仪(新文件(“plik1.txt”);
ArrayList=新建ArrayList();
while(plik1.hasNextLine()){
字符串temp=plik1.nextLine();
字符串[]拆分字符串=临时拆分(“”);
ArrayList tempList=新的ArrayList();
添加(圣殿骑士);
for(int i=0;i
您的方法的问题在于您只创建了一个列表

实现所需的一种简单方法是使用从文件中读取行。 然后,对于读取的每一行,创建并填写一个列表。 您可以在另一个列表中维护这些列表以供参考

List<List<String>> myLists = new ArrayList<List<String>>();
String line = null;
BufferedReader br = null;
try {
    br = new BufferedReader(new FileReader("plik1.txt"));
    while ((line = br.readLine()) != null) {
        myLists.add(Arrays.asList(line.split(" ")));      
    } 
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}      
// I've left it out for brevity but don't forget to close resources !

// print the lists in the format you need
int counter = 1;
for(List<String> list : myLists) {
    System.out.println("strin" + counter + " : " + list);
}
List mylist=new ArrayList();
字符串行=null;
BufferedReader br=null;
试一试{
br=新的BufferedReader(新的文件读取器(“plik1.txt”);
而((line=br.readLine())!=null){
添加(Arrays.asList(line.split(“”));
} 
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}      
//为了简洁起见,我省略了它,但别忘了关闭资源!
//以您需要的格式打印列表
int计数器=1;
用于(列表:MyList){
System.out.println(“strin”+计数器+”:“+列表);
}

您可以逐行读取文件,并使用新的
扫描仪分析每一行:

Scanner plik1 = new Scanner( new File("plik1.txt") );

ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();

// read first line
Scanner scan = new Scanner(plik1.nextLine());
while (scan.hasNext()){
    list.add(scan.nextInt());
}

// read second line
scan = new Scanner(plik1.nextLine());
while (scan.hasNext()){
    list2.add(scan.nextInt()); // second list
}

System.out.print("strin1 : " + list);
System.out.print("strin2 : " + list2);
Scanner plik1=新扫描仪(新文件(“plik1.txt”);
ArrayList=新建ArrayList();
ArrayList list2=新的ArrayList();
//读第一行
扫描仪扫描=新扫描仪(plik1.nextLine());
while(scan.hasNext()){
添加(scan.nextInt());
}
//读第二行
scan=新扫描仪(plik1.nextLine());
while(scan.hasNext()){
list2.add(scan.nextInt());//第二个列表
}
系统输出打印(“strin1:+列表);
系统输出打印(“strin2:+list2”);

以下是java 8的简单答案:

List<List<String>> result = Files.readAllLines(Paths.get("plik.text")).stream().map(s -> Arrays.asList(s.split(" "))).collect(Collectors.asList());
List result=Files.readAllLines(path.get(“plik.text”)).stream().map(s->Arrays.asList(s.split(“”)).collector(Collectors.asList());

不使用扫描仪或缓冲读取器的轻微变化。Java7可以做到这一点

String plik1= new String(Files.readAllBytes(Paths.get("C:\\plik1")));
String[] lines = plik1.split("\r\n");
List<List<Integer>> intLists = new ArrayList<>();
for(int i=0; i<lines.length; i++) {
   List<String> strList = Arrays.asList(lines[i].split(" "));
   List<Integer> intList = new ArrayList<>();
   for(String s : strList) {
      intList.add(Integer.parseInt(s));
   }
   intLists.add(intList);
}
String plik1=新字符串(Files.readAllBytes(path.get(“C:\\plik1”));
字符串[]行=plik1.split(“\r\n”);
List intLists=new ArrayList();
对于(int i=0;i
String plik1= new String(Files.readAllBytes(Paths.get("C:\\plik1")));
String[] lines = plik1.split("\r\n");
List<List<Integer>> intLists = new ArrayList<>();
for(int i=0; i<lines.length; i++) {
   List<String> strList = Arrays.asList(lines[i].split(" "));
   List<Integer> intList = new ArrayList<>();
   for(String s : strList) {
      intList.add(Integer.parseInt(s));
   }
   intLists.add(intList);
}