我正在尝试创建一个程序来读取 a File
,将内容(单词)保存在 an 中ArrayList
,对 进行排序ArrayList
,然后将排序 ArrayList
后的内容写回File
.
我不知道为什么,它一直给我一个FileNotFoundException
或一个NullPointerException
(两者都在发生,这有点奇怪)......
这是我的代码,如果有人可以提供帮助,那就太好了。
谢谢。
顺便说一句,代码包含四个类:
DriverClass、View(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();
}
}
}
//读取文件
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;
}
}
//写文件
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();
}
}