java - 将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
"-. /
~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
解决方案
我用您的 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();
}
}
推荐阅读
- web3 - 如何使用以太坊 API 获取用户已导入其 Metamask 的令牌?
- scikit-learn - 在 TfidfVectorizer 上进行特征选择后删除冗余的 ngram
- javascript - 如何根据 GeoJSON 数据过滤散景视觉效果?
- javascript - 如何使用javascript单击从数组中删除特定对象
- python - 如何在 python 标记器库中设置词汇量?
- bash - 在linux中删除具有特定列数的行
- java - How can I configure Checkstyle to validate this simple import style?
- spring - 如何使用 Spring Cloud Kubernetes 以编程方式写入 ConfigMap 值?
- flutter - Dart // Flutter - 如何连接图像和音频来渲染静态视频
- reactjs - 当所有孩子都有钥匙时,反应抱怨没有钥匙的孩子