java怎么将多维字符串数组输入到文本文档并读取文档

能够写字符串,但是将多个字符串组成多维数组的时候无法写入多维字符串数组
读取字符串数组的时候不清楚是不是应该进行循环,应该怎么实现循环

用文件流将数组的元素逐行写入文本文档,将字符串数组 strArray 写入到名为 test.txt 的文本文档中,并读取文件中的数据。

在写入文件时,用了双重循环,将数组的元素逐行写入文件,并使用 bw.newLine() 方法将每一行的元素分隔开。

在读取文件时,用了 BufferedReader 类的 readLine() 方法逐行读取文本文件,然后用 split() 方法将每一行的字符串分割成字符串数组。这样就可以将字符串数组还原成原来的多维字符串数组。读取到的每一行的字符串数组可以使用 for 循环逐个打印出来。
例子:

import java.io.*;

public class WriteReadArrayToFile {
    public static void main(String[] args) {
        String[][] strArray = {{"a", "b", "c"}, {"d", "e", "f"}};
        File file = new File("test.txt");
        try {
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            for (String[] str : strArray) {
                for (String s : str) {
                    bw.write(s + " ");
                }
                bw.newLine();
            }
            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 读取文件
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                String[] tempArray = line.split(" ");
                for (String s : tempArray) {
                    System.out.print(s + " ");
                }
                System.out.println();
            }
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


该回答引用GPTᴼᴾᴱᴺᴬᴵ
要将多维字符串数组输入到文本文档,可以使用Java中的BufferedWriter和FileWriter类。首先,需要将多维字符串数组转换为字符串,可以使用Arrays.deepToString()方法,将其转换为一个字符串,然后写入到文本文档中。读取多维字符串数组可以使用Java中的BufferedReader和FileReader类,读取每一行并使用字符串分割方法将其分割成多个字符串,最后将其存储到一个多维字符串数组中。

以下是示例代码:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class Example {
    public static void main(String[] args) {
        String[][] array = {{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}};

        // 将多维字符串数组写入文本文档
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File("output.txt")))) {
            for (String[] row : array) {
                writer.write(Arrays.deepToString(row));
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 从文本文档读取多维字符串数组
        try (BufferedReader reader = new BufferedReader(new FileReader(new File("output.txt")))) {
            String line;
            String[][] newArray = new String[array.length][];
            int i = 0;
            while ((line = reader.readLine()) != null) {
                String[] row = line.substring(1, line.length() - 1).split(", ");
                newArray[i++] = row;
            }
            System.out.println(Arrays.deepToString(newArray));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


上述代码中,将多维字符串数组转换为字符串时,使用了Arrays.deepToString()方法。读取文本文档时,使用BufferedReader读取每一行,然后使用字符串分割方法将其分割成多个字符串,最后将其存储到一个多维字符串数组中。需要注意的是,读取每一行后,字符串数组中的每个字符串前后会有方括号和空格,需要使用substring()方法将其去除。