Java JFileChooser readAsString(File file)

Here you can find the source of readAsString(File file)

Description

read As String

License

Open Source License

Declaration

public static String readAsString(File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.file.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    public static String readAsString(String path) throws IOException {
        return readAsString(new File(path));
    }//  w  w w .j a va2 s.co m

    public static String readAsString(File file) throws IOException {
        return readAsString(file.toPath());
    }

    public static String readAsString(Path path) throws IOException {
        return new String(Files.readAllBytes(path));
    }

    public static String readAsString(JFrame frame, String ext, String desc) throws IOException {
        File file = findFileOpen(frame, ext, desc);

        if (file == null) {
            return null;
        }

        return readAsString(file);
    }

    public static File findFileOpen(JFrame frame, String ext, String desc) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new FileNameExtensionFilter(desc, ext));

        if (chooser.showOpenDialog(frame) != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        return chooser.getSelectedFile();
    }
}

Related

  1. openFile(String fileExtension)
  2. promptFile(File file, FileFilter filter, String title)
  3. promptForFilename(String title)
  4. PromptForFileOpen(Component c)
  5. promptSaveJFileChooser(String suggestedFileName, FileNameExtensionFilter... fileNameExtensionFilters)
  6. readAsString(File file)
  7. readFromFile()
  8. removeChoosableFileFilters(JFileChooser fc)
  9. retrieveFilesFromDir(File basedir, FileFilter filter)