简体   繁体   English

Java将文件值存储到数组中

[英]Java storing file values into an array

Currently I am trying to read all the files from .prg file into an array.目前我正在尝试将 .prg 文件中的所有文件读入一个数组。 Currently the code I have only reads the first line of the file.目前我的代码只读取文件的第一行。 I would like to read each word within the file and store it within an array.我想读取文件中的每个单词并将其存储在一个数组中。 I believe the issue is with the way I am splitting the file but I cannot seem to find a solution我相信问题出在我拆分文件的方式上,但我似乎找不到解决方案

Code below:代码如下:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
 public class Reader {
    private Scanner reader

    public Reader(String fileName){

              try {
                   reader = new Scanner(new File(fileName));
                } catch (FileNotFoundException e){
                         e.printStackTrace();
                 }


        }
    public String[] getInput() {

    String [] input = reader.nextLine().trim().split(" ");
    return input;

}

 }
 public class Player {
     private String[] PlayerInformation;
     private String pName;


public Player(String gName) {
    this.pName = gName;

    this.reader = new 
    Reader("C:\\SpaceInvader\\Files\\PlayerInfo.prg");
    this.PlayerInformation= reader.getInput();
}
 public void ArrayPrintTest() {

    System.out.println(Arrays.toString(this.PlayerDetails));

}
public class Game {

  public Game() {

  }
  public static void main(String[] args){
             Player player = new Player("Player1");
             player.ArrayPrintTest();
             }

}

File format:文件格式:

Instruction 1
Jane
ForwardBackUpDown

Current Output电流输出

[Instruction, 1]

Expected Output预期产出

[Instruction, 1, Jane, ForwardBackUpDown]

This may fix your problem.这可能会解决您的问题。 You are only splitting by spaces but you have some items on newlines.您只是按空格拆分,但换行符上有一些项目。 Each operating system has its own new line code.每个操作系统都有自己的新行代码。 The code below contains all the different newline codes.下面的代码包含所有不同的换行代码。

String [] input = reader.nextLine().trim().split("\\r?\\n|\\r");

You are actually reading only the first line of your file, because you are running reader.nextLine() only one time.您实际上只读取文件的第一行,因为您只运行reader.nextLine()一次。

To read the complete file you can use the Files.lines() method and use Java Streams:要读取完整的文件,您可以使用Files.lines()方法并使用 Java Streams:

List<String> words = Files.lines(Paths.get(fileName))
        .map(String::trim)
        .map(line -> line.split(" "))
        .flatMap(Arrays::stream)
        .collect(Collectors.toList());

If you want you result to be an array you can use the Stream.toArray() method instead of Stream.collect() :如果你希望你的结果是一个数组,你可以使用Stream.toArray()方法而不是Stream.collect()

String[] words = Files.lines(Paths.get(fileName))
        .map(String::trim)
        .map(line -> line.split(" "))
        .flatMap(Arrays::stream)
        .toArray(String[]::new);

If you want to read the file without the Files.lines() method you can use the following:如果你想在没有Files.lines()方法的情况下读取文件,你可以使用以下方法:

List<String> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {
    String line;
    while ((line = br.readLine()) != null) {
        lines.add(line);
    }
}

Now you can use lines.stream() and continue with one of the first approaches.现在您可以使用lines.stream()并继续使用第一种方法。

At the end one last hint: If you want to split by any whitespace, not just " " you can use line.split("\\\\s+") .最后一个提示:如果你想用任何空格分割,而不仅仅是" "你可以使用line.split("\\\\s+")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM