Java JFileChooser importFile(JFileChooser jFileChooser)

Here you can find the source of importFile(JFileChooser jFileChooser)

Description

import File

License

Open Source License

Declaration

public static String importFile(JFileChooser jFileChooser) 

Method Source Code

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

import javax.swing.*;

import java.io.*;
import java.util.ArrayList;

public class Main {
    public static String importFile(JFileChooser jFileChooser) {

        StringBuilder stringBuilder;

        int result = jFileChooser.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) {
            final ArrayList<String> parfileTextLines = myReadDataFile(jFileChooser
                    .getSelectedFile().toString());

            stringBuilder = new StringBuilder();

            for (String currLine : parfileTextLines) {
                stringBuilder.append(currLine);
                stringBuilder.append("\n");
            }/*from  w  w w  .ja  v a  2  s. c  o  m*/

            return stringBuilder.toString();
        }

        return null;
    }

    private static ArrayList<String> myReadDataFile(String fileName) {
        String lineData;
        ArrayList<String> fileContents = new ArrayList<String>();
        BufferedReader moFile = null;
        try {
            moFile = new BufferedReader(new FileReader(new File(fileName)));
            while ((lineData = moFile.readLine()) != null) {

                fileContents.add(lineData);
            }
        } catch (IOException e) {
            ;
        } finally {
            try {
                moFile.close();
            } catch (Exception e) {
                //Ignore
            }
        }
        return fileContents;
    }
}

Related

  1. getSelectedFileWithExtension(JFileChooser c)
  2. getSelectedFileWithExtension(JFileChooser c)
  3. getSystemFile(Component owner, int mode, FileFilter[] filters)
  4. getTextFileChooser()
  5. getXmlFileChooser()
  6. initFileChooser(JFileChooser fileChooser, FileFilter filter)
  7. isDriveTraversable(File drive)
  8. loadFile()
  9. loadFile(Component parent, String title)