[英]read txt file of ascii chars into 2d array in java
I have txt file consists of ascii image (sample shown below) that I am trying to pass into a 2d array of unknown size which means I do not know the size of [row]
and [col]
.我有一个包含 ascii 图像(示例如下所示)的 txt 文件,我试图将其传递到一个未知大小的二维数组中,这意味着我不知道
[row]
和[col]
的大小。
I have found so many things but none have worked for me.我发现了很多东西,但没有一个对我有用。
I have tried to adapt the things I read to make it work for my code but no progress.我试图调整我阅读的内容以使其适用于我的代码但没有进展。 Please consider helping.
请考虑提供帮助。 Note I am doing this inside a class AsciiImage.
请注意,我在 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
}
Here is the txt file I am working on.这是我正在处理的 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
"-. /
~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I created an input file with your ASCII boat and was able to reproduce the image.我用你的 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
"-. /
~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I used a List<list<Character>>
to hold the ASCII matrix image.我使用
List<list<Character>>
来保存 ASCII 矩阵图像。
I created two methods.我创建了两种方法。 The
createImageMatrix
method creates the List<List<Character>>
image matrix. createImageMatrix
方法创建List<List<Character>>
图像矩阵。 Each line of the image text file is read and put in a List<Character>
line list.读取图像文本文件的每一行并将其放入
List<Character>
行列表中。 Each line list is put in the image matrix.每行列表都放在图像矩阵中。
The printImageMatrix
method uses a StringBuilder
to construct the ASCII image for display and verification. printImageMatrix
方法使用StringBuilder
构造用于显示和验证的 ASCII 图像。
Here's the complete runnable code.这是完整的可运行代码。 You can ignore the first two lines of the
main
method.您可以忽略
main
方法的前两行。 I keep all my testing resources in a resource folder.我将所有测试资源保存在资源文件夹中。 The first two lines give me the complete path to the text file in the resource folder.
前两行为我提供了资源文件夹中文本文件的完整路径。
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.