Open And Read File

  JFileChooser fileChooser = new JFileChooser();

fileChooser.setDialogTitle("Open File");


int userSelection = fileChooser.showOpenDialog(null);


if (userSelection == JFileChooser.APPROVE_OPTION) {

    File selectedFile = fileChooser.getSelectedFile();

    StringBuilder contentBuilder = new StringBuilder();


    try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {

        String line;

        while ((line = reader.readLine()) != null) {

            contentBuilder.append(line).append("\n");

        }

        textPane1.setText(contentBuilder.toString()); // Show content in textPane1

        System.out.println("File loaded: " + selectedFile.getAbsolutePath());

    } catch (IOException ex) {

        Logger.getLogger(DashBoard.class.getName()).log(Level.SEVERE, null, ex);

    }

} else {

    System.out.println("Open command cancelled by user.");

}

Comments

Popular posts from this blog

Remove (Clear) and remove Selected element

JSpinner

🛢️ MySQL Connection & CRUD Operations in Java