简体   繁体   English

Java:从先前在另一个类中选择的文件中读取文本

[英]Java: Read text from a file that was previously selected in another class

Using JavaFX, I have the user input information into text fields. 使用JavaFX,我可以将用户输入信息输入文本字段。 My MainController class allows the user to save that inputted text to a txt file and save it at a given location. 我的MainController类允许用户将输入的文本保存到txt文件中,并将其保存在给定位置。

Im wondering if its possible to pass what was entered in that text file into another class so I can parse and have my program use its data. 我想知道是否有可能将在文本文件中输入的内容传递给另一个类,以便我可以解析并让程序使用其数据。

I used strings and .getText() then used a filewriter and bufferedWriter. 我使用字符串和.getText(),然后使用文件编写器和bufferedWriter。 Can I get the .getText input into another class? 我可以将.getText输入输入另一个类吗?

如果您的课程比是公开的,则可以在应用程序的任何课程中使用它;

You should research for Java - Access Modifiers 您应该研究Java-访问修饰符

but as an ideia, 但是作为一个观念,

public class MainController {

    private String savedFilePath;

    public String getSavedFilePath() {
        return savedFilePath;
    }

    public void setSavedFilePath(String savedFilePath) {
        this.savedFilePath = savedFilePath;
    }
}

and from the other class, when you save the file you can call: 在另一个类中,保存文件时可以调用:

MainController controller = new MainController();
controller.setSavedFilePath("file path");

If you don't want to create a new object MainController, you can define the savedFilePath as static 如果您不想创建新的对象MainController,则可以将savedFilePath定义为static

public class MainController {

    private static String savedFilePath;

    public static void setSavedFilePath(String paramSavedFilePath) {
        savedFilePath = paramSavedFilePath;
    }
}

and just call: 然后致电:

MainController.setSavedFilePath("file path");

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

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