Java 如何将文本自动加载到文本区域

Java 如何将文本自动加载到文本区域,java,javafx,textarea,bufferedreader,Java,Javafx,Textarea,Bufferedreader,我有一个具有记事本式功能的程序。当我打开它时,我希望从文本文件保存的文本自动加载到文本区域 我有两节课。Writer类(保存的文本应该出现在其中)和Load类,后者实际从文本文件导入文本 编写器类: public class Writer extends Application { private FlowPane notepadLayout = new FlowPane(Orientation.VERTICAL); private Scene notepadScene = new Scene(

我有一个具有记事本式功能的程序。当我打开它时,我希望从文本文件保存的文本自动加载到文本区域

我有两节课。Writer类(保存的文本应该出现在其中)和Load类,后者实际从文本文件导入文本

编写器类:

public class Writer extends Application {
private FlowPane notepadLayout = new FlowPane(Orientation.VERTICAL);
private Scene notepadScene = new Scene(notepadLayout,600,300);
private TextArea inputArea = new TextArea();

private void notepadSetup(){
Text titleText = new Text("Notepad");
notepadLayout.getChildren().add(titleText);
notepadLayout.getChildren().add(inputArea);
}

public void start(Stage primaryStage) throws Exception {
notepadSetup();

 Load.loadOperation(); 

primaryStage.setTitle("ROBOT V1!");
primaryStage.setScene(notepadScene);
primaryStage.show();
因此,上面的类具有文本区域。我想做的是使用下面的类将信息从文本文件加载到上面的文本区域

public class Load {
private static String line;
static ArrayList<String> x = new ArrayList<>();

 public static void loadOperation(){
    try{
        BufferedReader br = new BufferedReader (new FileReader("Notes.txt"));
        line = br.readLine();

        while(line != null){
             x.add(line);                
            line = br.readLine();
        }
    }catch(Exception e){

    }
    System.out.println(x);
}
公共类加载{
专用静态字符串线;
静态ArrayList x=新ArrayList();
公共静态void loadOperation(){
试一试{
BufferedReader br=新的BufferedReader(新文件阅读器(“Notes.txt”);
line=br.readLine();
while(行!=null){
x、 添加(行);
line=br.readLine();
}
}捕获(例外e){
}
系统输出println(x);
}

line Load.loadOperation打印文本文件中的内容。如何将其加载到文本区域?它还必须保留格式(换行符)

for(int i=0;i

只需更改方法,使其返回字符串即可。(我对其进行了更新,使其使用更现代的Java。)

那你就这么做吧

try {
    inputArea.setText(Load.loadOperation());
} catch (IOException exc) {
    exc.printStackTrace();
}

您的代码甚至无法编译。@James\u D它现在可以编译方法是,而不是
append(…)
@James\u D在API文档中,它列出了这两种方法,并说
appendText(…)
已弃用。您正在链接AWT文本区域的API文档。您不能在JavaFX应用程序中使用AWT文本区域。我是否可以上载我的保存类,然后您检查它是否最新且有效?不,请阅读API文档中我使用的方法,并查看同一类中的其他类似方法。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

public class Load {

    public static String loadOperation() throws IOException {
            return Files.lines(Paths.get("Notes.txt"))
                .collect(Collectors.joining("\n"));

    }
}
try {
    inputArea.setText(Load.loadOperation());
} catch (IOException exc) {
    exc.printStackTrace();
}