首页 > 解决方案 > 将ASCII字符的txt文件读入java中的二维数组

问题描述

我的 txt 文件包含我试图传递给未知大小的二维数组的 ascii 图像(示例如下),这意味着我不知道 和 的[row]大小[col]

我发现了很多东西,但没有一个对我有用。

这里

这里

这里

我试图调整我阅读的内容以使其适用于我的代码,但没有进展。请考虑提供帮助。请注意,我在 AsciiImage 类中执行此操作。

ArrayList<String>[][] arrayOfCharacters = new ArrayList[20][20];

    

    public AsciiImage(String passFile) throws FileNotFoundException, IOException{
         try{
                Scanner input = new Scanner(new File(passFile));

                int row = 0;
                int column = 0;

                while(input.hasNext()){
                    String[] c = input.nextL();
                    arrayOfCharacters[row][column] = c.charAt(0);

                    column++;

                    // handle when to go to next row
                }   

                input.close();
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
                // handle it
            }
        

这是我正在处理的 txt 文件。

                .'|     .8
               .  |    .8:
              .   |   .8;:        .8
             .    |  .8;;:    |  .8;
            .     n .8;;;:    | .8;;;
           .      M.8;;;;;:   |,8;;;;;
          .    .,"n8;;;;;;:   |8;;;;;;
         .   .',  n;;;;;;;:   M;;;;;;;;
        .  ,' ,   n;;;;;;;;:  n;;;;;;;;;
       . ,'  ,    N;;;;;;;;:  n;;;;;;;;;
      . '   ,     N;;;;;;;;;: N;;;;;;;;;;
     .,'   .      N;;;;;;;;;: N;;;;;;;;;;
    ..    ,       N6666666666 N6666666666
    I    ,        M           M
   ---nnnnn_______M___________M______mmnnn
         "-.                          /
 ~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

标签: javaarraysarraylist

解决方案


我用您的 ASCII 船创建了一个输入文件,并且能够重现该图像。

                .'|     .8
               .  |    .8:
              .   |   .8;:        .8
             .    |  .8;;:    |  .8;
            .     n .8;;;:    | .8;;;
           .      M.8;;;;;:   |,8;;;;;
          .    .,"n8;;;;;;:   |8;;;;;;
         .   .',  n;;;;;;;:   M;;;;;;;;
        .  ,' ,   n;;;;;;;;:  n;;;;;;;;;
       . ,'  ,    N;;;;;;;;:  n;;;;;;;;;
      . '   ,     N;;;;;;;;;: N;;;;;;;;;;
     .,'   .      N;;;;;;;;;: N;;;;;;;;;;
    ..    ,       N6666666666 N6666666666
    I    ,        M           M
   ---nnnnn_______M___________M______mmnnn
         "-.                          /
 ~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我用 aList<list<Character>>来保存 ASCII 矩阵图像。

我创建了两种方法。该createImageMatrix方法创建List<List<Character>>图像矩阵。读取图像文本文件的每一行并将其放入List<Character>行列表中。每个行列表都放在图像矩阵中。

printImageMatrix方法使用aStringBuilder来构造ASCII图像以进行显示和验证。

这是完整的可运行代码。您可以忽略该main方法的前两行。我将所有测试资源保存在资源文件夹中。前两行给了我资源文件夹中文本文件的完整路径。

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AsciiImage {

    public static void main(String[] args) {
        URL url = AsciiImage.class.getResource("/boat.txt");
        String filename = url.getFile();

        AsciiImage ai = new AsciiImage(filename);
        List<List<Character>> imageMatrix = ai.createImageMatrix();
        System.out.println(ai.printImageMatrix(imageMatrix));
    }

    private String filename;

    public AsciiImage(String filename) {
        this.filename = filename;
    }

    public List<List<Character>> createImageMatrix() {
        List<List<Character>> imageMatrix = new ArrayList<>();

        try {
            Scanner input = new Scanner(new File(filename));
            while (input.hasNext()) {
                String s = input.nextLine();
                List<Character> lineList = new ArrayList<>(s.length());
                for (int index = 0; index < s.length(); index++) {
                    lineList.add(Character.valueOf(s.charAt(index)));
                }
                imageMatrix.add(lineList);
            }
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return imageMatrix;
    }

    public String printImageMatrix(List<List<Character>> imageMatrix) {
        StringBuilder builder = new StringBuilder();
        for (List<Character> lineList : imageMatrix) {
            for (char c : lineList) {
                builder.append(c);
            }
            builder.append(System.lineSeparator());
        }

        return builder.toString();
    }

}

推荐阅读