Java获取文本文件的属性
在我的引擎中,我将一个对象的属性写入一个文件(位置、缩放、obj模型和纹理),在我只获得位置的那一刻,我如何告诉程序也要查找、缩放、obj文件和纹理
这是我目前的代码:
private void load_ExampleWorld()
{
try
{
ProcessCoords file = new ProcessCoords(file_name);
String[] aryLines = file.OpenFile();
int i;
for (i = 0; i < aryLines.length; i++)
{
System.out.println(aryLines[i]);
if(aryLines[i].startsWith("model")) {
String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
String[] ArgArray = Arguments.substring(1, Arguments.length() - 2).split(" ");
World_Object_1(Double.parseDouble(ArgArray[0]),
Double.parseDouble(ArgArray[1]),
Double.parseDouble(ArgArray[2]),
0.5f,0.5f,0.5f,
"cube.obj","cubeTestTexture.png","diffuse_NRM.jpg","diffuse_DISP.jpg");
}
}
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
程序已经得到了这个职位,但我似乎无法告诉他获得其他信息
在文本文件中,它将如下所示(示例):
model:(-3.979997 0.0 0.38)(0.5 0.5 0.5) cube.obj cubeTestTexture.png diffuse_NRM.jpg diffuse_DISP.jpg
有人能告诉我这是怎么做到的吗?因为我似乎无法控制自己的想法。。谢谢你的帮助
以下是ProcessCoords类:
public class ProcessCoords
{
private String path;
private boolean append_to_file = false;
public ProcessCoords (String file_path)
{
path = file_path;
}
public ProcessCoords(String file_path, boolean append_value)
{
path = file_path;
append_to_file = append_value;
}
public String[] OpenFile() throws IOException
{
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i < numberOfLines; i++)
{
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
public void writeToFile(String textLine) throws IOException
{
FileWriter write = new FileWriter(path, append_to_file);
PrintWriter print_line = new PrintWriter(write);
print_line.printf("%s" + "%n", textLine);
print_line.close();
}
int readLines() throws IOException
{
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null)
{
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
# 1 楼答案
为什么不使用配置文件库呢?有一个优秀的由typesafe制作的名为config。此外,还可以使用一个属性文件,该文件可以被Properties class轻松读取
# 2 楼答案
方法
OpenFile()
可以返回一个String[]
,即使不调用readLines()
(不必要地再次打开同一个文件):除此之外,一切似乎都很好
# 3 楼答案
如果您不需要使用任何其他的库(如奥古斯托在他的回答中建议的那样),那么您可能需要考虑使用正则表达式。我不知道您的类
ProcessCoords
如何读取属性文件,因此在本例中我将使用标准Java类如果您想获得有关所使用的正则表达式的详细信息,可以将其签出here