将 ascii 字符的 txt 文件读入 java 中的二维数组
read txt file of ascii chars into 2d array in java
我有一个包含 ascii 图像(示例如下所示)的 txt 文件,我试图将其传递到大小未知的二维数组中,这意味着我不知道 [row]
和 [=14= 的大小].
我发现了很多东西,但 none 对我有用。
Here
我已尝试调整我阅读的内容以使其适用于我的代码,但没有任何进展。请考虑提供帮助。请注意,我在 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();
}
}
我有一个包含 ascii 图像(示例如下所示)的 txt 文件,我试图将其传递到大小未知的二维数组中,这意味着我不知道 [row]
和 [=14= 的大小].
我发现了很多东西,但 none 对我有用。
Here
我已尝试调整我阅读的内容以使其适用于我的代码,但没有任何进展。请考虑提供帮助。请注意,我在 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();
}
}