Example usage for java.nio.file Files newBufferedWriter

List of usage examples for java.nio.file Files newBufferedWriter

Introduction

In this page you can find the example usage for java.nio.file Files newBufferedWriter.

Prototype

public static BufferedWriter newBufferedWriter(Path path, Charset cs, OpenOption... options)
        throws IOException 

Source Link

Document

Opens or creates a file for writing, returning a BufferedWriter that may be used to write text to the file in an efficient manner.

Usage

From source file:com.rvantwisk.cnctools.controllers.CNCToolsController.java

@FXML
public void onViewGCode(ActionEvent actionEvent) {
    try {//from w  ww .ja  v  a 2  s .  c o m
        if (v_projectList.getSelectionModel().selectedItemProperty().get() != null) {
            final Project p = v_projectList.getSelectionModel().selectedItemProperty().get();

            if (p.postProcessorProperty().get() == null) {
                MonologFX dialog = new MonologFX(MonologFX.Type.QUESTION);
                dialog.setTitleText("No postprocessor");
                dialog.setMessage("No post processor configured, please select a post processor first!");
                dialog.show();
            } else {

                File file = File.createTempFile("cnctools", "gcode");
                final GCodeCollection gCode = p.getGCode(toolDBManager);
                file.createNewFile();
                file.deleteOnExit();
                if (file != null) {
                    try (BufferedWriter br = Files.newBufferedWriter(file.toPath(), Charset.forName("UTF-8"),
                            new OpenOption[] { StandardOpenOption.WRITE })) {
                        br.write(gCode.concate().toString());
                        br.write("\n");
                        br.flush();
                        br.close();

                        Project project = projectModel.projectsProperty()
                                .get(v_projectList.getSelectionModel().getSelectedIndex());
                        showGCodeFromFile(project, file.getAbsolutePath());

                    } catch (IOException ex) {
                        handleException(ex);
                    }
                }
            }

        }
    } catch (Exception e) {
        handleException(e);
    }
}