Example usage for java.nio.file Files readString

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

Introduction

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

Prototype

public static String readString(Path path) throws IOException 

Source Link

Document

Reads all content from a file into a string, decoding from bytes to characters using the StandardCharsets#UTF_8 UTF-8 Charset charset .

Usage

From source file:de.bwravencl.controllerbuddy.gui.Main.java

private void loadProfile(final File file) {
    stopAll();/*from w w w.ja v a2  s. c om*/

    var profileLoaded = false;

    try {
        final var jsonString = Files.readString(file.toPath());
        final var actionAdapter = new ActionTypeAdapter();
        final var gson = new GsonBuilder().registerTypeAdapterFactory(new ModeAwareTypeAdapterFactory())
                .registerTypeAdapter(IAction.class, actionAdapter).create();

        try {
            final var profile = gson.fromJson(jsonString, Profile.class);

            final var unknownActionClasses = actionAdapter.getUnknownActionClasses();
            if (!unknownActionClasses.isEmpty())
                JOptionPane.showMessageDialog(frame,
                        rb.getString("UNKNOWN_ACTION_TYPES_DIALOG_TEXT")
                                + String.join("\n", unknownActionClasses),
                        rb.getString("WARNING_DIALOG_TITLE"), JOptionPane.WARNING_MESSAGE);

            profileLoaded = input.setProfile(profile, input.getJid());
            if (profileLoaded) {
                saveLastProfile(file);
                updateModesPanel();
                updateOverlayPanel();
                loadedProfile = file.getName();
                setUnsavedChanges(false);
                setStatusBarText(rb.getString("STATUS_PROFILE_LOADED") + file.getAbsolutePath());
                scheduleStatusBarText(rb.getString("STATUS_READY"));
                fileChooser.setSelectedFile(file);

                restartLast();
            }
        } catch (final JsonParseException e) {
            log.log(Logger.Level.ERROR, e.getMessage(), e);
        }
    } catch (final IOException e) {
        log.log(Logger.Level.ERROR, e.getMessage(), e);
    }

    if (!profileLoaded)
        JOptionPane.showMessageDialog(frame, rb.getString("COULD_NOT_LOAD_PROFILE_DIALOG_TEXT"),
                rb.getString("ERROR_DIALOG_TITLE"), JOptionPane.ERROR_MESSAGE);
}