[英]read txt file of ascii chars into 2d array in java
我有一个包含 ascii 图像(示例如下所示)的 txt 文件,我试图将其传递到一个未知大小的二维数组中,这意味着我不知道[row]
和[col]
的大小。
我发现了很多东西,但没有一个对我有用。
我试图调整我阅读的内容以使其适用于我的代码但没有进展。 请考虑提供帮助。 请注意,我在 class 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
"-. /
~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我使用List<list<Character>>
来保存 ASCII 矩阵图像。
我创建了两种方法。 createImageMatrix
方法创建List<List<Character>>
图像矩阵。 读取图像文本文件的每一行并将其放入List<Character>
行列表中。 每行列表都放在图像矩阵中。
printImageMatrix
方法使用StringBuilder
构造用于显示和验证的 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.