我想創建一個程序,讀取一個File
,保存在一個ArrayList
內容(字),排序ArrayList
,然後寫入的內容進行排序ArrayList
回File
。文件分揀機 - FileNotFoundException異常
我不知道爲什麼,它不斷給我一個FileNotFoundException
或NullPointerException
(兩者都存在,這是一個有點怪異)...
這裏是我的代碼,如果有人能幫助這將是巨大的。
謝謝。
順便說一句,該代碼包含四類:
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();
}
}
我沒有看到你提到的2個輸入之間的差異。另外,嘗試使用斜槓('/')而不是反斜槓。 – user845279
反斜槓在Windows上正常。只有字符串文字才需要轉義反斜槓,控制檯中的用戶(或大多數輸入類型)不必轉義它們。 –
您可以將問題簡化爲代碼中存在錯誤而不是提供整個程序的地方? Stacktraces通常指向異常的一行。 –