簡體   English   中英

如何使用Jfilechooser使用文本文件中的數據

[英]How to use data from text file using Jfilechooser

我試圖在程序中使用文本文件中的值,但是首先我想真正地了解如何使用JFileChooser ,但我無法使其工作。

該程序:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class Hw7Problem2 {
    public static void main(String[] args) throws FileNotFoundException {
        JFileChooser student_scores = new JFileChooser();
        int jfcUserOption = student_scores.showOpenDialog(null);
        // To verify it reads
        if (jfcUserOption == JFileChooser.APPROVE_OPTION) {
            File chosenFile = student_scores.getSelectedFile();
            System.out.println("The file you chose was: " + chosenFile.getName());
        }
        Scanner scanner = new Scanner(new File("student_scores.txt"));
        // Print text file on program
        System.out.println(scanner);
    }
}

錯誤:

The file you chose was: student_scores.txt
Exception in thread "main" java.io.FileNotFoundException: student_scores.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at Hw7Problem2.main(Hw7Problem2.java:21)

您必須正確執行此操作,直到創建掃描儀。 問題是您沒有使用JFileChooser的結果。 看起來您將結果放入chosenFile getSelectedFile()將返回所選的文件,因此您只需要使用它創建掃描程序。

如果您需要了解有關JFileChooser如何工作的更多信息,可以在此處在線找到文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM