2014-05-24 27 views
-2

我試圖讓文件讀入Arraylist,然後從文件中讀取數字並計算平均值。我無法讀取該文件,並打開JFileChooser。我花了三天的時間試圖讓這個工作。我在這裏看到過一些類似的問題,但沒有使用JFileChooser。我的代碼是JFileChooser。我知道如何計算平均值,但我只需要讀取文件中的數字。當我通過文件選擇器打開文件時,如何獲取文件以讀取Arraylist

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 


public class Week07 { 
    static JFileChooser fileChooser = new JFileChooser(); 
    public static void main(String[] args) throws IOException { 
     String theFile; 
     theFile = getTheFileName(); 
     double theAverage; 
     theAverage = getTheAverage(theFile); 
     displayTheResult(theAverage,"The average is: "); 
    } 
    public static String getTheFileName() { 
     String status; 
     fileChooser.showOpenDialog(null); 
     status = fileChooser.getSelectedFile().getAbsolutePath(); { 

      return status; 
     } 

     } 

    private static double getTheAverage(String theFile) throws NumberFormatException, IOException{ 
     String fileName = getTheFileName(); 
     FileReader fr = new FileReader(theFile); 
     BufferedReader br = new BufferedReader(fr); 

所以這裏是我有,但現在我不能讓它顯示平均值。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

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


public class Week07 { 
    static JFileChooser chooser = new JFileChooser(); 
    public static void main(String[] args) throws IOException { 
     String theFile; 
     theFile = getTheFileName(); 
     double theAverage; 
     //theAverage = getTheAverage(theFile); 
     //displayTheResult(theAverage,"The average is: "); 
    } 
    public static String getTheFileName() { 
     String status; 
     chooser.showOpenDialog(null); 
     status = chooser.getSelectedFile().getAbsolutePath(); { 

      return status; 
     } 

     } 

    public List<String> readFile() throws IOException 
    { 
     JFileChooser chooser = new JFileChooser(); 
     int result = chooser.showOpenDialog(null); 
     //check result 
     File theFile = chooser.getSelectedFile(); 

      //needs exception handling etc. 
     BufferedReader br = new BufferedReader(new FileReader(theFile)); 
     List<String> data = new ArrayList<String>(); 
     String line; 
     while ((line = br.readLine()) != null) 
      data.add(line); 

     br.close(); 
     return data; 
    }} 


public static double average(List<Integer> readFile) { 

    if (readFile == null || readFile.isEmpty()) 
    return 0.0; 
    // Calculate the summation of the elements in the list 
    long sum = 0; 
    int n = readFile.size(); 
    // Iterating manually is faster than using an enhanced for loop. 
    for (int i = 0; i < n; i++) 
     sum += readFile.get(i); 
    // We don't want to perform an integer division, so the cast is mandatory. 
    return ((double) sum)/n; 

} 
} 
+0

哪裏文件讀取代碼? – HectorLector

+2

兩個正交的問題。是否通過FileChooser選擇文件並不重要。 – merlin2011

+0

這是帶有文件閱讀代碼的代碼。 – user3670665

回答

0

在Java中有用於處理井文件的File類。因此,無論您是從FileChooser還是通過其他任何方式獲取文件都無關緊要。

有幾件事我會做不同的方式在你的getTheFileName()梅索德:

  • 返回的File,而不是直接的路徑
  • 創建梅索德
  • JFileChooser檢查返回值的showOpenDialog(如果用戶按取消怎麼辦)

這是一些示例代碼,它讀取文件的內容我N個字符串列表:

public List<String> readFile() 
{ 
    JFileChooser chooser = new JFileChooser(); 
    int result = chooser.showOpenDialog(null); 
    //check result 
    File file = chooser.getSelectedFile(); 

    //needs exception handling etc. 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    List<String> data = new ArrayList<String>(); 
    String line; 
    while ((line = br.readLine()) != null) 
     data.add(line); 

    br.close(); 
    return data; 
} 

如果您使用> = Java 7中,你可以簡單地使用this

List<String> data = Files.readAllLines(Path path, Charset cs); 

另一種方法是使用Scanner類。這可能會簡化一些事情,因爲您可以直接檢查並讀取整數/雙精度值並對其進行總結。

+0

這是一個很好的答案,除非你沒有適當地處理你的資源。如果拋出某種異常,你將不會關閉'BufferedReader'。儘管如此,我確實喜歡對'Files.readAllLines'的引用 – MadProgrammer

0

讓我們開始與正確使用JFileChooser的...

JFileChooser#showOpenDiaog返回int

返回:

被彈文件選擇時的返回狀態:
JFileChooser.CANCEL_OPTION
JFileChooser.APPROVE_OPTION
JFileChooser.ERROR_OPTION如果發生錯誤 或對話框不可用錯過

這一點很重要,因爲JFileChooser#getSelectedFile也可能返回null,這將導致你當前的代碼......

public static String getTheFileName() { 
    String status; 
    fileChooser.showOpenDialog(null); 
    status = fileChooser.getSelectedFile().getAbsolutePath(); { 
     return status; 
    } 

} 

拋出NullPointerException ...

相反,你應該考慮使用類似...

public static File getTheFileName() { 
    File file = null; 
    if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
     file = fileChooser.getSelectedFile(); 
    } 
    return file; 
} 

之所以使用實際File對象是它承載更多的內容和功能僅只是一個String

下一步是讀取文件。這取決於Java版本使用的是您可以使用...

File file = ...; 

List<String> values = new ArrayList<String>(25); 
try (BufferedReader br = new BufferedReader(new FileReader(file))) { 

    String text = null; 
    while ((text = br.readLine()) != null) { 
     values.add(text); 
    } 

} catch (IOException exp) { 
    exp.printStackTrace(); 
} 

對於Java 7如果您使用的是Java 6或更早版本(使用try-with-resources)或

File file = ...; 

List<String> values = new ArrayList<String>(25); 
BufferedReader br = null; 
try { 
    br = new BufferedReader(new FileReader(file));    
    String text = null; 
    while ((text = br.readLine()) != null) { 
     values.add(text); 
    } 
} catch (IOException exp) { 
    exp.printStackTrace(); 
} finally { 
    try { 
     br.close(); 
    } catch (Exception e) { 
    } 
} 

。這裏重要的是要確保你正確地關閉你的資源,如果你打開了,你必須盡一切努力來關閉它。

退房Basic I/OHow to use File Choosers更多細節

相關問題