gpt4 book ai didi

Java FileInputStream 问题(额外空格)

转载 作者:行者123 更新时间:2023-12-02 01:29:21 25 4
gpt4 key购买 nike

当我使用 FileInputStream 从 spawn.txt 文件输入基于图 block 的 map 的文本时,它会在每行之间添加额外的空格。

package mapInit;

import java.io.FileInputStream;

public class SpawnMap {

static char[][] spawnWorld = new char[30][30];

public static void main(String[] args) {
try {
FileInputStream spawn = new FileInputStream("Resources/Map/spawn.txt");
int i = 0;
int h = 0;
int k = 0;
while((i=spawn.read())!=-1){
if(h == 30) {
h = 0;
k++;
}
spawnWorld[k][h] = (char)i;
h++;
}
spawn.close();
} catch (Exception e) {
}
for (int i=0; i<30; i++) {
for (int j=0;j<30;j++) {
System.out.println(spawnWorld[i][j]);
}
}
}

}

这是输出循环的结果: Output result这是文本文件的图片:

Text File

GitHub 链接:https://github.com/WeaponGod243/Machlandia

最佳答案

我认为 Scanner 类更适合您的任务

Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them.

The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. It provides many methods to read and parse various primitive values.

The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc.

The Java Scanner class extends Object class and implements Iterator and Closeable interfaces.

The Java Scanner class provides nextXXX() methods to return the type of value such as nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean(), etc. To get a single character from the scanner, you can call next().charAt(0) method which returns a single character. Source

Scanner Java Doc

public static void main(String arg[]) {

try (Scanner sc = new Scanner(new File("Resources/Map/spawn.txt"))) {
// Checking if sc has another token in the file
while(sc.hasNext()) {
// Print line
System.out.println(sc.next());
}

} catch (Exception ex) {
// Use a Logger to log exceptions in real projects
ex.printStackTrace();
}
}

您也可以使用 Apache Commons IO 库

      <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>


import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class ReadTextFile {

public static void main(String[] args) throws IOException {

try {
File f = new File("Resources/Map/spawn.txt");

List<String> lines = FileUtils.readLines(f, "UTF-8");

for (String line : lines) {
System.out.println(line);
}

} catch (IOException e) {
// Use a Logger to log exceptions in real projects
e.printStackTrace();
}

}
}

关于Java FileInputStream 问题(额外空格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57560506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com