0

我想創建一個程序,讀取一個File,保存在一個ArrayList內容(字),排序ArrayList,然後寫入的內容進行排序ArrayListFile文件分揀機 - FileNotFoundException異常

我不知道爲什麼,它不斷給我一個FileNotFoundExceptionNullPointerException(兩者都存在,這是一個有點怪異)...

這裏是我的代碼,如果有人能幫助這將是巨大的。

謝謝。

順便說一句,該代碼包含四類:

DriverClass,視圖(GUI),ReadFile的,和WriteFile。

你可以忽略評論,我只是爲自己寫的 - 他們很明顯。對於「field.getText();」假設用戶輸入C:\Users\Corecase\Desktop\test.txt我試過C:\\Users\\Corecase\\Desktop\\test.txt),但這也不起作用。

再次感謝!

public class DriverClass 
{ 
    public static void main(String[] args) 
    { 
     View open= new View(); 
    } 
} 

//查看

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class View implements ActionListener 
{ 
private JFrame frame = new JFrame("File Sorter"); 
private JPanel mainPane = new JPanel(); 
private JPanel textPane = new JPanel(); 
private JPanel buttonPane = new JPanel(); 
private JButton sortButton = new JButton("Sort"); 
private JLabel label = new JLabel("Enter file path: "); 
public JTextField field = new JTextField(25); 

private Font f = new Font("Trebuchet MS", Font.PLAIN, 20); 


public View() 
{ 
    frame.setSize(500,500); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(mainPane); 

    mainPane.setLayout(new GridLayout(2,1)); 
    mainPane.setBackground(Color.gray); 

    mainPane.add(textPane); 
    mainPane.add(buttonPane); 

    textPane.setLayout(new GridLayout(2,1)); 
    textPane.add(label); 
    textPane.add(field); 
    buttonPane.add(sortButton); 
    field.setFont(f); 
    sortButton.setFont(f); 
    label.setFont(f); 

    sortButton.addActionListener(this); 
} 
public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource() == sortButton) 
    { 
     ReadFile r = new ReadFile(field.getText()); 
     WriteFile w = new WriteFile(field.getText()); 

     r.openFile(); 
     r.readAndSortFile(); 
     r.closeFile(); 

     w.openFile(); 
     w.writeFile(r.getList()); 
     w.closeFile(); 
    } 
} 
} 

// ReadFile的

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

public class ReadFile extends View 
{ 
private ArrayList<String> words = new ArrayList<String>(); 
private String fileName = new String(); 
private Scanner x; 

public ReadFile(String address) 
{ 
    fileName = address; 
} 
public void openFile() 
{ 
    try 
    { 
     x = new Scanner(new File(fileName)); 
    } 
    catch(Exception e) 
    { 
     field.setText("Could not read file."); 
    } 
} 

public void readAndSortFile() 
{ 
    while(x.hasNext()) 
     words.add(x.next()); 

    sort(); 
} 

public void closeFile() 
{ 
    x.close(); 
} 

public ArrayList<String> sort() 
{ 
    String temp = ""; 

    for(int index = 0; index < words.size(); index++) 
    { 
     for(int inner = 0; inner < words.size(); inner++) 
     { 
      if((words.get(inner)).compareTo(words.get(inner+1)) > 0) 
      { 
       temp = words.get(inner); 
       words.set(inner, words.get(inner + 1)); 
       words.set(inner + 1, temp); 
      } 
     } 
    } 
    return words; 
} 

public ArrayList<String> getList() 
{ 
    return words; 
} 
} 

// WriteFile的

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

public class WriteFile extends View 
{ 
private Formatter x; 
private String fileName = new String(); 

public WriteFile(String address) 
{ 
    fileName = address; 
} 

public void openFile() 
{ 
    try 
    { 
     x = new Formatter(fileName); 
    } 
    catch(Exception e) 
    { 
     field.setText("Could not write to file."); 
    } 
} 

public void writeFile(ArrayList<String> myWords) 
{ 
    for(int index = 0; index < myWords.size(); index++) 
     x.format("%s", myWords.get(index), "\n");//%s means string - in this case ONE string 
} 

public void closeFile() 
{ 
    x.close(); 
} 
} 
+1

我沒有看到你提到的2個輸入之間的差異。另外,嘗試使用斜槓('/')而不是反斜槓。 – user845279

+0

反斜槓在Windows上正常。只有字符串文字才需要轉義反斜槓,控制檯中的用戶(或大多數輸入類型)不必轉義它們。 –

+0

您可以將問題簡化爲代碼中存在錯誤而不是提供整個程序的地方? Stacktraces通常指向異常的一行。 –

回答

1

您的代碼中有幾個問題。您正在擴展視圖以獲取textField'字段'的引用!那不是要走的路。你應該使用異常處理來完成這個簡單的任務。你也無法同時讀寫文件。所以你需要分開這裏的職責。當你完成閱讀文件時,關閉它並用你想使用的任何編寫器再次打開它。最後說明:您可以使用FileChooser來獲取路徑,併爲您節省檢查有效輸入的需求!如果你想做的很難,並強迫用戶手動輸入路徑,你必須添加轉義字符'/',在你的情況下,一個有效的路徑將是C:\\Users\\Corecase\\Desktop\\test.txt

Change the following code in 'View.java'

if (e.getSource() == sortButton) 
    { 
     ReadFile r; 
     try 
     { 
      r = new ReadFile(field.getText()); 
      r.readAndSortFile(); 

      WriteFile w = new WriteFile(field.getText()); 
      w.writeFile(r.getList()); 

     } 
     catch (FileNotFoundException e1) 
     { 
      field.setText(e1.getMessage()); 
      e1.printStackTrace(); 
     }  
    } 

and change 'WriteFile.java' to

public class WriteFile 
{ 
    private Formatter x; 
    private String fileName; 

    public WriteFile(String address) throws FileNotFoundException 
    { 
     fileName = address; 
     try 
     { 
      x = new Formatter(fileName); 
     } catch (Exception e) 
     { 
      throw new FileNotFoundException("Could not write to file."); 
     } 
    } 

    public void writeFile(ArrayList<String> myWords) 
    { 
     for (int index = 0; index < myWords.size(); index++) 
      x.format("%s%s", myWords.get(index), System.lineSeparator()); 

     // now you are done writing so close the file. 

     x.close(); 
    } 
} 

Change 'ReadFile.java' to

public class ReadFile 
{ 
    private ArrayList<String> words; 
    private String fileName; 
    private Scanner x; 

    public ReadFile(String path) throws FileNotFoundException 
    { 
     fileName = path; 
     words = new ArrayList<String>(); 
     try 
     { 
      x = new Scanner(new File(fileName)); 
     } catch (Exception e) 
     { 
      throw new FileNotFoundException("File Doesn't exist in the place you specified."); 
     } 
    } 


    public void readAndSortFile() 
    { 
     while (x.hasNext()) 
      words.add(x.next()); 
     Collections.sort(words); 

     // Now you are done reading and sorting, so close the file. 
     x.close(); 
    } 

    . 
    . 
    . 
} 
+0

非常感謝。 – corecase

+0

不客氣:) – Sultan

1

有在你的代碼夫婦問題:

  • ReadFileWriteFile正在擴展View,按照構造函數,你將有打開的JFrame多個實例,因爲你可以將這個幀可見在構造函數frame.setVisible(true);ReadFileWriteFile只是需要JTextField的參考應該是更新,只需將其作爲參數傳遞即可。

  • sort肯定會拋出IndexOutOfBoundsException此訂單

    如果((words.get(內))。的compareTo(words.get(內+ 1))> 0){

    此行當到達最後一個索引將無法正常工作,爲什麼不使用簡單Collections.sort(words);

  • 你沒有檢查,如果用戶輸入路徑或沒有,如果輸入什麼,你會在你的ReadFile得到NullPointerException,理想如果沒有找到文件,即您的掃描儀是nul l,不要繼續下去。目前你正在顯示一條錯誤消息,但你的代碼並沒有停在那裏,它仍然試圖讀取和排序錯誤的文件。

1

我試過你的例子並調試它。我使用了c:\\ dir \\文件。文本作爲參數到GUI和文件被正確讀取,所以它不是你的問題。我得到的例外是來自此代碼:

for (int index = 0; index < words.size(); index++) { 
     for (int inner = 0; inner < words.size(); inner++) { 
      if ((words.get(inner)).compareTo(words.get(inner + 1)) > 0) { 
       temp = words.get(inner); 
       words.set(inner, words.get(inner + 1)); 
       words.set(inner + 1, temp); 
      } 
     } 
    } 
0

FileNotFoundException意味着該文件未找到。

NullPointerException表示您試圖取消引用空指針。

如果這不能回答你的問題,我不知道是什麼。 StackOverflow不是一個調試服務。

+0

你是絕對正確的; StackOverflow不是一種調試服務 - 它是人們互相幫助理解/解決問題的服務。 – corecase

相關問題