如何自动将文本加载到文本区域
How to automatically load text into Text area
我有一个具有记事本式功能的程序。当我打开它时,我希望从文本文件中保存的文本自动加载到文本区域。
我有两个 class。 Writer class(应显示保存的文本的位置)和实际从文本文件导入文本的 Load class。
作者Class:
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();
所以上面的class有文本区。我想要做的是使用下面的 class 将文本文件中的信息加载到上面的文本区域中。
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);
}
行Load.loadOperation打印出文本文件中的内容。我如何让它加载到文本区域?它还必须保留格式(换行符)。
从文件中读取文本后,需要将其添加到 TextArea
对象中。因此,假设您手边有对 inputArea
的引用,您可以将其附加到(空)控件中:
for (int i = 0; i < x.length; i++) {
inputArea.appendText(x.get(i));
}
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextArea.html
只需更改方法,使其 returns 成为一个字符串。 (我对其进行了更新,以便它也使用更现代的 Java。)
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();
}
我有一个具有记事本式功能的程序。当我打开它时,我希望从文本文件中保存的文本自动加载到文本区域。
我有两个 class。 Writer class(应显示保存的文本的位置)和实际从文本文件导入文本的 Load class。
作者Class:
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();
所以上面的class有文本区。我想要做的是使用下面的 class 将文本文件中的信息加载到上面的文本区域中。
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);
}
行Load.loadOperation打印出文本文件中的内容。我如何让它加载到文本区域?它还必须保留格式(换行符)。
从文件中读取文本后,需要将其添加到 TextArea
对象中。因此,假设您手边有对 inputArea
的引用,您可以将其附加到(空)控件中:
for (int i = 0; i < x.length; i++) {
inputArea.appendText(x.get(i));
}
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextArea.html
只需更改方法,使其 returns 成为一个字符串。 (我对其进行了更新,以便它也使用更现代的 Java。)
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();
}