简体   繁体   English

Java使用文本文件中的一段代码

[英]Java use a piece of code from a text file

I am currently trying to figure something out. 我目前正在尝试解决问题。 For my world editor I want my program to read a text file and use its content as code material. 对于我的世界编辑器,我希望程序读取文本文件并将其内容用作代码材料。 I've already made a decent file reader but now I've got a problem. 我已经做了不错的文件阅读器,但是现在遇到了问题。 In the console I am getting the right output, the file has only one line that says: 在控制台中,我得到正确的输出,该文件只有一行显示:

this.makeGrass(new Vector3f(0, 1, 2));

this is actually part of a code that tells my program to render a specific object to the scene, in this case it's a grass model. 这实际上是告诉我的程序将特定对象渲染到场景的代码的一部分,在这种情况下,这是草模型。 However instead of just printing this information to the console with 但是,不只是使用以下命令将这些信息打印到控制台

System.out.println(aryLines[i]);

I want to be able to use the information stored on the .txt file so I can actually add it to my rendering code. 我希望能够使用存储在.txt文件中的信息,以便可以将其实际添加到渲染代码中。 The entire method that prints the lines on the text file to the console is: 将文本文件上的行打印到控制台的整个方法是:

     public void TextOutput()
     {
        String file_name = "C:/Text.txt";

        try
        {
            StoreCoords file = new StoreCoords(file_name);
            String[] aryLines = file.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++)
            {
                System.out.println(aryLines[i]);
            // !! How to use the information as part of my code ??
            }
        } catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
     }

I hope you understand what I want: The content of my text file is a piece of code that I want to use further instead of having it just print to the console, I'm sure this is possible but I wouldn' know how. 我希望您理解我想要的:文本文件的内容是我想进一步使用的一段代码,而不是仅将其打印到控制台上,我敢肯定这是可能的,但我不知道如何做。

As Java is a compiled language, you'd have to recompile at runtime and I am not sure that is even possible. 由于Java是一种编译语言,因此您必须在运行时重新编译,但我不确定这样做是否可行。 If I were you, I'd hardcode in my own commands. 如果我是你,我会用自己的命令进行硬编码。 You want to call a function called makeGrass, hardcode it in. Maybe in your text file you can have this: 您想调用一个名为makeGrass的函数,对其进行硬编码。也许在您的文本文件中可以有以下内容:

    makeGrass:0,1,2

Then have this right after the println: 然后在println之后拥有此权限:

    if(aryLines[i].startsWith("makeGrass:")) {
            String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
            ArgArray = Arguments.split(",");
            this.makeGrass(new Vector3f(Double.parseDouble(ArgArray[0]), Double.parseDouble(ArgArray[1]), Double.parseDouble(ArgArray[2])));
    }

I'm going to leave my answer like this, assuming you are an experienced programmer. 假设您是一位经验丰富的程序员,我将这样回答。 If I am wrong feel free to ask and I will explain it to you. 如果我错了,请随时提出,我会向您解释。 I can also explain how to modify it to add different commands if you want. 如果需要,我还可以解释如何修改它以添加其他命令。

Also, this is rather unsafe because if the input is in the wrong format it will crash the app. 另外,这是相当不安全的,因为如果输入格式错误,则会使应用程序崩溃。 If you plan on letting users edit the file, then I can show you how to add on safeties. 如果您打算让用户编辑文件,那么我可以向您展示如何增加安全性。

Hope this helped, Joseph Meadows 希望这对您有所帮助,约瑟夫·梅多斯(Joseph Meadows)

Okay, thanks to Joseph Meadows for the hint, I'm doing the following thing, right after the println statement I've added the code provided by him. 好的,感谢Joseph Meadows的提示,在println语句添加了他提供的代码之后,我正在做以下事情。 To make ArgArray work I had to put String[] before it and also I had to create a new constructor in my Vector3f class to match the Double.parseDouble thingy.. 为了使ArgArray工作,我必须将String []放在前面,并且还必须在Vector3f类中创建一个新的构造函数以匹配Double.parseDouble。

public void TextOutput()
     {   
        String file_name = "C:/Users/Server/Desktop/textText.txt";

        try
        {           
            StoreCoords file = new StoreCoords(file_name);
            String[] aryLines = file.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++)
            {
                System.out.println(aryLines[i]);    

                 if(aryLines[i].startsWith("makeGrass:")) {
                        String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
                        String[] ArgArray = Arguments.split(",");

                        this.makeGrass(new Vector3f(Double.parseDouble(ArgArray[0]), 
                                                    Double.parseDouble(ArgArray[1]), 
                                                    Double.parseDouble(ArgArray[2])));
                }
            }
        } catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
     }

my original Vector3f constructor is: 我最初的Vector3f构造函数是:

public Vector3f(float x, float y, float z)
    {
        this.m_x = x;
        this.m_y = y;
        this.m_z = z;
    }

and to make the code in the TextOutput method work I've added another constructor right below the original one.. 为了使TextOutput方法中的代码正常工作,我在原始构造函数的下面添加了另一个构造函数。

public Vector3f(double parseDouble, double parseDouble2, double parseDouble3) {
        this.m_x = (float) parseDouble;
        this.m_y = (float) parseDouble2;
        this.m_z = (float) parseDouble3;
    }

Now everything works great, the console gives me the apropriate statement 现在一切正常,控制台给了我适当的声明

makeGrass:0,1,2

and the rendering system creates the grass model at the apropriate coordinates, the only thing I want to change now is that I don't have to add an additional constructor to the Vector3f class, I'm sure I'll figure that out too. 并且渲染系统在适当的坐标处创建了草模型,现在我唯一要更改的就是我不必在Vector3f类中添加其他构造函数,我敢肯定我也会弄清楚这一点。

In the picture provided in this link you can see exactly what's going on: 在此链接提供的图片中,您可以确切了解正在发生的事情:

http://www.pic-upload.de/view-27720774/makeGrassEx.png.html http://www.pic-upload.de/view-27720774/makeGrassEx.png.html

As you can see, the content of the text file is printed out in the console (the numbers below is the fps counter) and the coordinates provided by the text file are interpreted correctly, two grass models being displayed at the respective coordinates which is exactly what I wanted! 如您所见,文本文件的内容已在控制台中打印出来(下面的数字是fps计数器),并且文本文件提供的坐标被正确解释,两个草绘模型分别显示在各自的坐标上我想要什么!

Thanks again for your help Joseph Meadows, this is exactly what I was looking for! 再次感谢您的帮助,约瑟夫·梅多斯(Joseph Meadows),这正是我想要的!

I am not sure if you solved this yet, but you did not need the second constructor. 我不确定您是否已解决此问题,但是您不需要第二个构造函数。 I was unsure of the data type you were using for the coordinates, and I assumed you use doubles because that is what I have grown accustomed to using. 我不确定您用于坐标的数据类型,并且我假设您使用双精度,因为这是我已经习惯使用的数据。

In actuality, all types can be parsed from a string. 实际上,所有类型都可以从字符串中解析。 Look here: 看这里:

    this.makeGrass(new Vector3f(Double.parseDouble(ArgArray[0]), 
                                Double.parseDouble(ArgArray[1]), 
                                Double.parseDouble(ArgArray[2])));

This right now is turning the string into a double. 现在,这把字符串变成了双。 That is what 那是什么

    Double.parseDouble();

does. 确实。

It looks like you are using floats though, so you can always just use the float parsing method: 看起来您好像正在使用浮点数,因此您始终可以只使用浮点数解析方法:

    Float.parseFloat("String");

That would result with this: 这将导致以下结果:

    this.makeGrass(new Vector3f(Float.parseFloat(ArgArray[0]), 
                                Float.parseFloat(ArgArray[1]), 
                                Float.parseFloat(ArgArray[2])));

Sorry for the late response, and you are surely welcome for the help. 抱歉,您的回复很晚,欢迎您的帮助。 I just love being useful! 我只是喜欢有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM