Reputation: 577
In my engine I write the properties of an object to a file (position, scale, obj model and texture) at the moment I only get the position, how can I tell the program to also look for, scale, the obj file and the texture?
This is my code at the moment:
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());
}
}
The program already gets the position but I seem not to be able to tell him to get the other informations.
in the text file it will look something like this (example):
model:(-3.979997 0.0 0.38)(0.5 0.5 0.5) cube.obj cubeTestTexture.png diffuse_NRM.jpg diffuse_DISP.jpg
Can someone explain to me how this can be achieved because I seem no to be able to wrap my head around it.. thanks for your help!
Here's the ProcessCoords Class:
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;
}
}
Upvotes: 1
Views: 73
Reputation: 988
Will try that out, thanks! Sorry I've totally forgot about ProcessCoords I have added the code in my question.. but as I remember it uses the java standart classes - please tell me if there might be something wrong or missing in the ProcessCoords class I wouldn't be sure myself..?
The method OpenFile()
can return a String[]
even without a call to readLines()
(which unnecessarily opens the same file for a second time):
public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
String line;
List<String> textData = new List<>();
while((line = textReader.readLine()) != null) {
textData.add(line);
}
textReader.close();
fr.close();
return textData.toArray(new String[0]);
}
Apart from that, everything seems to be fine.
Upvotes: 0
Reputation: 988
If you need to do it without using any other libraries (as Augusto suggested in his answer), you might want to consider using regular expressions. I do not know how your class ProcessCoords
reads the properties file, so I'll be using standard Java classes in this example.
try (FileReader fr = new FileReader("file.txt"); BufferedReader in = new BufferedReader(fr);) {
// read all lines from the given file
String line;
List<String> lines = new ArrayList<>();
while ((line = in.readLine()) != null) {
lines.add(line);
}
// process the lines
for (int i = 0; i < lines.size(); i++) {
String current = lines.get(i);
// print the current line to the console output
System.out.println(current);
if (current.startsWith("model:")) {
String args = current.substring("model:".length());
Pattern pattern = Pattern
.compile("\\((-?\\d+\\.\\d+) (-?\\d+\\.\\d+) (-?\\d+\\.\\d+)\\)\\((-?\\d+\\.\\d+) (-?\\d+\\.\\d+) (-?\\d+\\.\\d+)\\) (.+?\\.obj) (.+?\\.png) (.+?\\.jpg) (.+?\\.jpg)");
Matcher matcher = pattern.matcher(args);
if (!matcher.matches()) {
System.out.println("illegal argument format");
continue;
}
double xCoord = Double.parseDouble(matcher.group(1));
double yCoord = Double.parseDouble(matcher.group(2));
double zCoord = Double.parseDouble(matcher.group(3));
double xScale = Double.parseDouble(matcher.group(4));
double yScale = Double.parseDouble(matcher.group(5));
double zScale = Double.parseDouble(matcher.group(6));
String modelFile = matcher.group(7);
String textureFile = matcher.group(8);
String diffuseNrmFile = matcher.group(9);
String diffuseDispFile = matcher.group(10);
World_Object_1(xCoord, yCoord, zCoord,
xScale, yScale, zScale,
modelFile, textureFile, diffuseNrmFile, diffuseDispFile);
}
}
} catch (IOException e) {
e.printStackTrace();
}
If you want to get detailed information on the regular expression that was used, you can check it out here.
Upvotes: 2
Reputation: 1336
Why don't you use a library for config files? There's an excelent one made by typesafe called config. Also, you could use a properties file that can be easily read by the Properties class.
Upvotes: 1