Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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 逐行将文件从txt读取到整数数组_Java - Fatal编程技术网

Java 逐行将文件从txt读取到整数数组

Java 逐行将文件从txt读取到整数数组,java,Java,我有一个如下所示的文本文件: 1 2 3 4 5 8 12 22 5 33 11 56 5 2 3 45 89 45 我包含了一个代码,我尝试了,但没有工作,因为每次当我试图打印到控制台时,行看起来都是空的 我尝试使用ArrayList读取文件,但不起作用 BufferedReader bufReader = new BufferedReader(new FileReader("file1.txt")); ArrayList<String> listOfLines =

我有一个如下所示的文本文件:

1 2 3 4 5 8 
12 22 5 33 11 56
5 2 3 45 89 45
我包含了一个代码,我尝试了,但没有工作,因为每次当我试图打印到控制台时,行看起来都是空的

我尝试使用ArrayList读取文件,但不起作用

BufferedReader bufReader = new BufferedReader(new 
FileReader("file1.txt"));
    ArrayList<String> listOfLines = new ArrayList<>();
    String line = bufReader.readLine();
    while (line != null) {
      listOfLines.add(line);
      line = bufReader.readLine();
      System.out.println("full: " + line);
      System.out.print("0: " + line.charAt(0));    
    }
BufferedReader bufReader=新的BufferedReader(新的
FileReader(“file1.txt”);
ArrayList listOfLines=新的ArrayList();
String line=bufReader.readLine();
while(行!=null){
添加(行);
line=bufReader.readLine();
System.out.println(“完整:“+行);
系统输出打印(“0:+line.charAt(0));
}
我想逐行从文件读取到数组,例如:
[12] [22][5][33][11][56]

如果要将文件读到每行一个数组中,可以使用以下代码:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class ReadIntArrayFromFile {

    public static void main(String[] args) throws IOException {
        //enter your path to the file here
        File file = new File("src/main/java/integer_array_from_file/file.txt");
        //read the lines from the file
        List<String> lines = Files.readAllLines(file.toPath());

        //create a list of arrays (one array per line)
        List<int[]> arrayForEachLine = new ArrayList<int[]>(lines.size());
        for (String line : lines) {
            //create the array from the line (one array for each line)
            int[] array = Stream.of(line.split(" ")).mapToInt(Integer::parseInt).toArray();
            //add the array to the list
            arrayForEachLine.add(array);
        }

        //print the arrays
        arrayForEachLine.stream().map(array -> Arrays.toString(array)).forEach(System.out::println);
    }
}
试试这个代码

final String dataFilePath = "C:\\Users\\Desktop\\txt.txt";
            ArrayList<String> listOfLines = new ArrayList<>();
           List listOfLinesint = new ArrayList<>();
                    BufferedReader reader;
                    try {
                        reader = new BufferedReader(new FileReader(dataFilePath));
                        String line = reader.readLine();
                        while (line != null) {
                            //System.out.println(line);
                            listOfLinesint.add(line);
                            line = reader.readLine();
                        }
                        reader.close();
                        System.out.println(listOfLinesint);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
final String dataFilePath=“C:\\Users\\Desktop\\txt.txt”;
ArrayList listOfLines=新的ArrayList();
List listOfLinesint=new ArrayList();
缓冲读取器;
试一试{
reader=newbufferedreader(newfilereader(dataFilePath));
字符串行=reader.readLine();
while(行!=null){
//系统输出打印项次(行);
添加(行)中的行列表;
line=reader.readLine();
}
reader.close();
System.out.println(listOfLinesint);
}捕获(IOE异常){
e、 printStackTrace();
}
您可以使用流:

Files.lines(Paths.get("file.txt"))
    .map(line -> Arrays.stream(line.split(" "))
        .map(Integer::valueOf)
        .collect(Collectors.toList()))
    .collect(Collectors.toList())

这将返回一个
列表
,其中所有元素都是整数。如果您只想打印每行的所有元素,那么可以将最后一行替换为
.forEach(System.out::println)

1。使用
文件.line
。2.对于每行:按空格分割。3.使用
Integer.parseInt
将每个元素转换为int。您必须在every while循环中读取文件,因此:
String line=null
while((line=bufReader.readLine())!=null){……}
你可以帮我在代码中解决这个问题吗?@mcepender
文件。行
一次在内存中加载所有行,对大文件来说效率不高。@alibenzarouk好吧,我通常不希望这样的格式的文件非常大,所以在大多数情况下
就足够了。但是,您可以轻松地将
文件.lines
替换为
文件.newbuffereder
Files.lines(Paths.get("file.txt"))
    .map(line -> Arrays.stream(line.split(" "))
        .map(Integer::valueOf)
        .collect(Collectors.toList()))
    .collect(Collectors.toList())