java javafx gui 打开文本文件,如何读取文本文件中的内容以及编辑/保存文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37769481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javafx gui that opens a text file, how to read whats in text file and edit/save text file
提问by Progamminnoob
I wrote code in javaFX to open a text file and my code opens it but it doesn't show anything inside the text file. and also just for fun I wanted to know whats the best way to go about editing the text file and then saving the text file with the edits the user just made. any tips would be greatly appreciated.
我在 javaFX 中编写了代码来打开一个文本文件,我的代码打开了它,但它没有在文本文件中显示任何内容。也只是为了好玩,我想知道编辑文本文件的最佳方法是什么,然后用用户刚刚进行的编辑保存文本文件。任何提示将非常感谢。
here is my code:
这是我的代码:
package blah;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.FileChooser;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.io.File;
public class blah
extends Application {
private Text actionStatus;
private Stage savedStage;
public static void main(String [] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
Button open = new Button("open");
open.setOnAction(new SingleFcButtonListener());
HBox open1 = new HBox(10);
open1.getChildren().addAll(open);
Button save = new Button("Save");
HBox save1 = new HBox(10);
save1.getChildren().addAll(save);
actionStatus = new Text();
VBox vbox = new VBox(30);
vbox.getChildren().addAll( open1,save1, actionStatus);
Scene scene = new Scene(vbox, 500, 300);
primaryStage.setScene(scene);
primaryStage.show();
savedStage = primaryStage;
}
private class SingleFcButtonListener implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
showSingleFileChooser();
}
}
private void showSingleFileChooser() {
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(null);
if (selectedFile != null) {
actionStatus.setText("File selected: " + selectedFile.getName());
}
}
}
回答by GOXR3PLUS
You can use BufferedReaderor Scannerfor reading the file:
您可以使用BufferedReader或Scanner来读取文件:
BufferedReader:
缓冲阅读器:
try (BufferedReader reader = new BufferedReader(new FileReader(new File("file.txt")))) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
Scanner:
扫描仪:
try (Scanner scanner = new Scanner(new File("file.txt"))) {
while (scanner.hasNext())
System.out.println(scanner.next());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
You can use PrintWriteror FileWriter(with BufferedWriter) to write to a file:
您可以使用PrintWriter或FileWriter(使用 BufferedWriter)写入文件:
FileWriter:
文件编写器:
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
example here:
这里的例子:
PrintWriter:
打印写入器:
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.flush();
writer.close();
taken from here
取自这里