简体   繁体   English

BufferedReader 保存句子,String 数组,然后是 2D int 数组

[英]BufferedReader saving sentence, String array, then 2D int array

I have a.txt file that contains text that I would like to save part of it in a String, part of it in a String array, and then the last part in a 2D int array, and am faced with two issues:我有一个包含文本的 .txt 文件,我想将它的一部分保存在 String 中,一部分保存在 String 数组中,最后一部分保存在 2D int 数组中,并且面临两个问题:

  1. How to read and save both of the arrays when their size is not known ahead of time?如果事先不知道 arrays 的大小,如何读取和保存它们?
  2. 2D array is not reading/saving properly二维数组未正确读取/保存

Here is the text file for reference:这是供参考的文本文件:

This is the sentence to be read.
This
is
a
String
array.
90 47 110 95 95
101 87 
54 0 38 12 

Here is part of my method that is supposed to read and save the three data types:这是我的方法的一部分,它应该读取和保存三种数据类型:

BufferedReader br = new BufferedReader(new FileReader(fileName));
        
        sentence = br.readLine();

        stringArr = new String[5]; //how to initialize without set number of elements?
        for(int i = 0; i<stringArr.length; i++){
            stringArr[i] = br.readLine();
        }

        int2DArr = new int[3][5]; //how to initialize with any amount of rows and columns?
        for(int i = 0; i<int2DArr.length; i++){
            for(int j = 0; j<int2DArr[i].length; j++){
                int2DArr[i][j] = br.read();
                //how to read the space after each int ?
            }
        }

How would I "grab" the size of the arrays by reading the text file, so that when I initialize both arrays, I have the proper sizes?如何通过读取文本文件“获取” arrays 的大小,以便在初始化两个 arrays 时,我有合适的大小? Any help would be greatly appreciated!任何帮助将不胜感激!

Path path = Paths.get(fileName);
List<String> lines = Files.readAllLines(path, Charset.defaultCharset());

String title = lines.get(0);
List<String> words = new ArrayList<>();
for (int i = 1; i < lines.size(); ++i) {
    String word = lines.get(i);
    if (!word.isEmpty() && Character.isDigit(word.codePointAt(0)) {
        break;
    }
    words.add(word);
}
String[] wordArray = words.toArray(new String[]);
int i0 = 1 + words.size();
int n = lines.size() - i0;
int[][] numbers = new int[n][];
for (int i = i0; i < lines.size(); ++i) {
    String[] values = lines.get(i).trim().split("\\s+");
    int m = values.length;
    int[] row = new int[m];
    for (int j = 0; j < m; ++m) {
        row[j] = Integer.parse(values[j]);
    }
    numbers[i - i0] = row;
}
  • Path is a generalisation of File , also URLs. PathFile的概括,也是 URL。
  • Files is a treasure trove of file functions. Files是文件功能的宝库。
  • One could do without dynamically sized List ;没有动态大小的List也可以; one would need to test first, but normally one would use a List anyhow.需要先进行测试,但通常无论如何都会使用 List 。
  • String.split splits on one or more whitespace. String.split在一个或多个空格上拆分。
import java.util.*;
import java.io.*;

class ReadFileIntoArr {
    public static void main(String args[]) throws IOException {
        String fileName = "test.txt";
        BufferedReader br = new BufferedReader(new FileReader(fileName));

        String line = br.readLine();
        int strSize = 0;
        int intSize = 0;
        boolean isDigit = false;

        while (line != null && line.trim().length() != 0) {
            if (!isDigit && Character.isDigit(line.charAt(0)))
                isDigit = true;
            if (isDigit)
                intSize++;
            else
                strSize++;
            line = br.readLine();
        }

        br = new BufferedReader(new FileReader(fileName));

        String[] stringArr = new String[strSize];
        for (int i = 0; i < stringArr.length; i++) {
            stringArr[i] = br.readLine();
        }

        int[][] int2DArr = new int[intSize][];
        for (int i = 0; i < int2DArr.length; i++) {
            int2DArr[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        }

        System.out.println(Arrays.toString(stringArr));
        System.out.println(Arrays.deepToString(int2DArr));
    }
}

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

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