Java JFileChooser readFromFile()

Here you can find the source of readFromFile()

Description

read matrix written as atext file with ; split token

License

Open Source License

Declaration

public static double[][] readFromFile() 

Method Source Code

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

import java.io.*;

import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Main {
    /**//from  www. j  a  va  2s.co  m
     * read matrix written as atext file with ; split token
     *
     * @return
     */
    public static double[][] readFromFile() {
        return readFromFile(getFileFromChooserLoad(getDefaultDirectory())
                .getAbsolutePath(), ";");
    }

    public static double[][] readFromFile(String token) {
        return readFromFile(getFileFromChooserLoad(getDefaultDirectory())
                .getAbsolutePath(), token);
    }

    public static double[][] readFromFile(String file_name, String token) {
        double[][] d = new double[1][1];
        ArrayList<double[]> lst = new ArrayList<>();
        File file = new File(file_name);
        if (!file.exists()) {
            showMessage(file_name + " isminde bir dosya yok");
            return d;
        }
        try (BufferedReader br = new BufferedReader(new FileReader(
                file_name))) {
            String s;
            while ((s = br.readLine()) != null) {
                if (s.indexOf("@") != -1) {
                    continue;
                }
                double[] row = null;
                if (token.isEmpty()) {
                    row = new double[1];
                    row[0] = Double.parseDouble(s);
                } else {
                    String[] sd = s.split(token);
                    row = new double[sd.length];
                    for (int i = 0; i < sd.length; i++) {
                        row[i] = Double.parseDouble(sd[i]);
                    }
                }
                lst.add(row);

            }
        } catch (IOException e) {
            e.printStackTrace();
            return d;
        }
        return lst.toArray(d);
    }

    public static File getFileFromChooserLoad() {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("select file");
        chooser.setSize(new java.awt.Dimension(45, 37)); // Generated
        //        chooser.setFileFilter(new FileNameExtensionFilter("png", "png"));
        File file = null;
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            return file;
        }
        return file;
    }

    public static File getFileFromChooserLoad(String folderPath) {
        JFileChooser chooser = new JFileChooser(folderPath);
        chooser.setDialogTitle("select file");
        chooser.setSize(new java.awt.Dimension(45, 37)); // Generated
        //        chooser.setFileFilter(new FileNameExtensionFilter("png", "png"));
        File file = null;
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            return file;
        }
        return file;
    }

    public static String getDefaultDirectory() {
        String workingDir = System.getProperty("user.dir");
        return workingDir;
    }

    public static void showMessage(String str) {
        JOptionPane.showMessageDialog(null, str);
    }

    public static double[][] add(double[][] a, double[][] b) {
        double[][] d = new double[a.length][a[0].length];
        if (isIdenticalMatrix(a, b)) {
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    d[i][j] = a[i][j] + b[i][j];
                }
            }
        } else {

        }
        return d;
    }

    private static boolean isIdenticalMatrix(double[][] a, double[][] b) {
        if (a.length == b.length && a[0].length == b[0].length) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. promptForFilename(String title)
  2. PromptForFileOpen(Component c)
  3. promptSaveJFileChooser(String suggestedFileName, FileNameExtensionFilter... fileNameExtensionFilters)
  4. readAsString(File file)
  5. readAsString(File file)
  6. removeChoosableFileFilters(JFileChooser fc)
  7. retrieveFilesFromDir(File basedir, FileFilter filter)
  8. run_file_chooser(String file_desc, String extension)
  9. save(JFileChooser fileChooser, Runnable action)