如何在附加到文件时关闭JavaFX FileChooser的覆盖警告?
我需要创建一个数据输入JavaFx类,它以追加模式输出到一个文件。在下面的简化可运行示例中,我可以创建一个文件,输入初始数据,然后毫无问题地附加到它。但是,如果我关闭程序并稍后再次运行,文件选择器将帮助查找文件,但会发出警告: 确认另存为__ TryFile.txt已存在__ 你想替换它吗 如果我选择确认,新数据将按预期追加,文件不会被替换。 我该如何关闭该警告?我在Windows7上使用Java8_20 注意:我已经按照下面的建议更正了代码。参见附录文件如何在附加到文件时关闭JavaFX FileChooser的覆盖警告?,java,io,append,javafx-8,filechooser,Java,Io,Append,Javafx 8,Filechooser,我需要创建一个数据输入JavaFx类,它以追加模式输出到一个文件。在下面的简化可运行示例中,我可以创建一个文件,输入初始数据,然后毫无问题地附加到它。但是,如果我关闭程序并稍后再次运行,文件选择器将帮助查找文件,但会发出警告: 确认另存为__ TryFile.txt已存在__ 你想替换它吗 如果我选择确认,新数据将按预期追加,文件不会被替换。 我该如何关闭该警告?我在Windows7上使用Java8_20 注意:我已经按照下面的建议更正了代码。参见附录文件 public class TryApp
public class TryAppend extends Application
{
static File file;
static void initiatefile( String setupInfo )
{
FileChooser choose = new FileChooser();
choose.getExtensionFilters().add(
new FileChooser.ExtensionFilter( "Text doc(*.txt)", "*.txt" ) );
choose.setInitialFileName( "*.txt" );
choose.setInitialDirectory( new File(System.getProperty("user.home") ) );
file = choose.showSaveDialog( null );
if ( file != null )
{
if ( file.getName().endsWith( ".txt" ) )
{
try( PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( file ))))
{
out.println( setupInfo );
}catch (IOException e) {
System.err.println(e);
}
}
}
}
static void appendToFile( String appendString )
{
if (file == null)
{
FileChooser choose = new FileChooser();
choose.getExtensionFilters().add(
new FileChooser.ExtensionFilter( "Text doc(*.txt)", "*.txt" ) );
choose.setInitialDirectory( new File(System.getProperty("user.home") ) );
file = choose.showOpenDialog( null ); //Now corrected. Previously
//was: choose.showSaveDialog( null );
}
if ( file != null )
{
try( PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( file, true ))))
{
out.println( appendString );
}catch (IOException e) {
System.err.println(e);
}
}
}
@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle( "Try Append to File" );
Group root = new Group();
Scene scene = new Scene( root, 300, 200 );
Button initBtn = new Button();
initBtn.setLayoutX( 100 );
initBtn.setLayoutY( 40 );
initBtn.setText( "Initiate File" );
initBtn.setOnAction( new EventHandler<ActionEvent>()
{
public void handle(ActionEvent event)
{
initiatefile( "Initial data" );
}
} );
Button appendBtn = new Button();
appendBtn.setLayoutX( 90 );
appendBtn.setLayoutY( 100 );
appendBtn.setText( "Append to File" );
appendBtn.setOnAction( new EventHandler<ActionEvent>()
{
public void handle(ActionEvent event)
{
appendToFile( "Appended data" );
}
} );
root.getChildren().add( initBtn );
root.getChildren().add( appendBtn );
primaryStage.setScene( scene );
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
是否可以使用“打开”对话框而不是“保存”对话框?似乎可以将“选择要附加到的文件”视为一个打开的函数,即使应用程序将写入该文件。是的!我按照appendToFile中的建议,通过替换file=choose.showsavediog null修复了它;如果file=choose.showOpenDialog为空;效果很好。谢谢我在原始帖子中更正了代码,以防有人正在学习JavaFx FileChooser并想尝试我使用的功能。是的,不幸的是,看到QuantemToolkit.showFileChooser的操作,它最终会委托给本机方法应用程序。\u showFileChooser。阅读本机调用之前,我没有看到任何代码执行任何可能导致“您确定要覆盖吗?”的循环,因此要真正做到这一点,需要一个功能请求。