简体   繁体   中英

Java storing file values into an array

Currently I am trying to read all the files from .prg file into an array. 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.

To read the complete file you can use the Files.lines() method and use 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() :

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:

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.

At the end one last hint: If you want to split by any whitespace, not just " " you can use line.split("\\\\s+") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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