将具有重复模式的文本文件读入列表
Read text file with repeated pattern into list
我正在尝试为 Java 创建一个 Tile Map Editor,但我在打开文件时卡住了。打开文件本身并不是什么大事,但是一旦我在文本文件中放入空格,它就会产生运行时错误。每个图块都包含一个图像(黑色或白色方形 atm)以及它是实心 (1) 还是非实心 (0)。 tilemaps 当前将以如下格式保存:
1:1 1:1 1:1 1:1 1:1 1:1
1:1 0:0 0:0 0:0 0:0 1:1
1:1 0:0 0:0 0:0 0:0 1:1
1:1 1:1 1:1 1:1 1:1 1:1
例如,这可能是一个简单的房间,里面有坚固的黑色墙壁,会挡住玩家。这将是一个 6x4 的瓦片地图。如何定义格式 x:x(此处为空格)?
List<Integer> list = new ArrayList<Integer>();
File file = new File("file.txt");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
list.add(Integer.parseInt(text));
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
虽然猜测这就是您要的,但类似这样的事情...
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadTextFile {
public static void main(String[] args) {
List<Tile> list = new ArrayList<Tile>();
String path = "C:/whatever/your/path/is/";
File file = new File(path + "file.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String text = null;
while ((text = reader.readLine()) != null) {
String[] pairs = text.split(" ");
for(String pair : pairs) {
String[] chars = pair.split(":");
int id = Integer.parseInt(chars[0]);
int type = Integer.parseInt(chars[1]);
list.add(new Tile(id, type));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Tile {
int id;
int type;
Tile(int id, int type) {
this.id = id;
this.type = type;
}
@Override
public String toString() {
return "Tile [id=" + id + ", type=" + type + "]";
}
}
如果您只是被空格打扰,请将代码更改为
while ((text = reader.readLine()) != null)
{
list.add(Integer.parseInt(text.trim()));
}
但我认为这不会起作用,因为您不能将整行转换为整数,所以我建议:
while ((text = reader.readLine()) != null)
{
String[] values = text.split(":")
for(String val : values)
{
list.add(Integer.parseInt(val.trim()));
}
}
我正在尝试为 Java 创建一个 Tile Map Editor,但我在打开文件时卡住了。打开文件本身并不是什么大事,但是一旦我在文本文件中放入空格,它就会产生运行时错误。每个图块都包含一个图像(黑色或白色方形 atm)以及它是实心 (1) 还是非实心 (0)。 tilemaps 当前将以如下格式保存:
1:1 1:1 1:1 1:1 1:1 1:1
1:1 0:0 0:0 0:0 0:0 1:1
1:1 0:0 0:0 0:0 0:0 1:1
1:1 1:1 1:1 1:1 1:1 1:1
例如,这可能是一个简单的房间,里面有坚固的黑色墙壁,会挡住玩家。这将是一个 6x4 的瓦片地图。如何定义格式 x:x(此处为空格)?
List<Integer> list = new ArrayList<Integer>();
File file = new File("file.txt");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
list.add(Integer.parseInt(text));
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
虽然猜测这就是您要的,但类似这样的事情...
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadTextFile {
public static void main(String[] args) {
List<Tile> list = new ArrayList<Tile>();
String path = "C:/whatever/your/path/is/";
File file = new File(path + "file.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String text = null;
while ((text = reader.readLine()) != null) {
String[] pairs = text.split(" ");
for(String pair : pairs) {
String[] chars = pair.split(":");
int id = Integer.parseInt(chars[0]);
int type = Integer.parseInt(chars[1]);
list.add(new Tile(id, type));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Tile {
int id;
int type;
Tile(int id, int type) {
this.id = id;
this.type = type;
}
@Override
public String toString() {
return "Tile [id=" + id + ", type=" + type + "]";
}
}
如果您只是被空格打扰,请将代码更改为
while ((text = reader.readLine()) != null)
{
list.add(Integer.parseInt(text.trim()));
}
但我认为这不会起作用,因为您不能将整行转换为整数,所以我建议:
while ((text = reader.readLine()) != null)
{
String[] values = text.split(":")
for(String val : values)
{
list.add(Integer.parseInt(val.trim()));
}
}