【问题标题】:Using File Dialogue Box Rather than FilePath Java使用文件对话框而不是 FilePath Java
【发布时间】:2018-04-11 02:23:05
【问题描述】:

Java 新手 - 我正在尝试导入包含整数的 .txt 文件。 最终我想将整数导入一个数组列表,然后创建一个频率分布并计算平均值。

目前我正在努力使用文件对话框,这是我的最终目标。我可以使用所示代码中的文件路径导入 .txt。如果有人可以帮助我解决如何使用对话框,将不胜感激!

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

public class Distribution {

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);

        System.out.println("Enter a complete file path to file you would like to open:");
        String fileName = input.nextLine();

        File inFile = new File(fileName);

        FileReader ins = null;

        try {
            ins = new FileReader(inFile);
            int ch;
            while ((ch = ins.read()) != -1) {
                System.out.print((char) ch);
            }
        } 
        catch (Exception e) {
            System.out.println(e);
        } 
        finally {
            try {
                ins.close();
            } 
            catch (Exception e) {
            }
        }

    } // main end
}

【问题讨论】:

标签: java openfiledialog


【解决方案1】:

嗯,简单易行。

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

class DistributionUI {

    public static void main(String[] args) {
        System.out.println("Enter a complete file path to file you would like to open:");

        final JFileChooser fchooser = new JFileChooser() {

            private static final long serialVersionUID = 1L;

            public void approveSelection() {
                File inFile = getSelectedFile();

                if (inFile.exists() ) {
                    FileReader ins = null;

                    try {
                        ins = new FileReader(inFile);
                        int ch;
                        while ((ch = ins.read()) != -1) {
                            System.out.print((char) ch);
                        }
                    } catch (Exception e) {
                        System.out.println(e);
                    } finally {
                        try {
                            ins.close();
                        } catch (Exception e) {
                        }
                    }
                }
                super.approveSelection();
            }
        };
        fchooser.setCurrentDirectory(new File("."));
        fchooser.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("my file", "txt");
        fchooser.addChoosableFileFilter(filter);
        fchooser.showOpenDialog(null);


    } // main end
}

【讨论】:

  • 我很抱歉没有早点回来!今天不得不去上班... 2个问题。 1) 为什么需要“private static final long serialVersionUID = 1L;”? 2) 什么是"FileNameExtensionFilter filter = new FileNameExtensionFilter("my file", "txt");"在做什么?
  • 1) 类的接口之一是Serializable 接口。检查另一个答案。 stackoverflow.com/questions/285793/… 2) 表示过滤只接受txt后缀。
猜你喜欢
  • 2015-08-30
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多