有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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;   
    }
}

共 (3) 个答案

  1. # 1 楼答案

    为什么不使用配置文件库呢?有一个优秀的由typesafe制作的名为config。此外,还可以使用一个属性文件,该文件可以被Properties class轻松读取

  2. # 2 楼答案

    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..?

    方法OpenFile()可以返回一个String[],即使不调用readLines()(不必要地再次打开同一个文件):

    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]);
    }
    

    除此之外,一切似乎都很好

  3. # 3 楼答案

    如果您不需要使用任何其他的库(如奥古斯托在他的回答中建议的那样),那么您可能需要考虑使用正则表达式。我不知道您的类ProcessCoords如何读取属性文件,因此在本例中我将使用标准Java类

    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();
    }
    

    如果您想获得有关所使用的正则表达式的详细信息,可以将其签出here