我正在尝试创建一个程序,该程序读取File
,将内容(单词)保存在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();
}
}
}
// 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();
}
}
答案 0 :(得分:1)
您的代码中存在几个问题:
ReadFile
和WriteFile
正在扩展View
,根据构造函数,您将打开多个JFrame
实例,因为您可以在您的构造函数frame.setVisible(true);
,ReadFile
和WriteFile
只需要更新JTextField
的引用,只需将其作为参数传递。
您的sort
肯定会为此行投放IndexOutOfBoundsException
if((words.get(inner))。compareTo(words.get(inner + 1))&gt; 0){
这一行在到达最后一个索引时不起作用,为什么不使用简单的Collections.sort(words);
您没有检查用户是否输入了路径,如果未输入任何内容,您将NullPointerException
中的ReadFile
,理想情况下,如果找不到文件,即您的scanner为null,请勿继续。目前您正在显示错误消息,但您的代码并未停止,它仍然会尝试读取和排序错误的文件。
答案 1 :(得分:1)
我试过你的例子并调试它。我使用c:\\ dir \\ file.text作为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);
}
}
}
答案 2 :(得分:1)
您的代码中有几个问题。您正在扩展View以获取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
答案 3 :(得分:0)
FileNotFoundException
表示找不到该文件。
NullPointerException
表示您尝试取消引用空指针。
如果这不能回答你的问题,我不知道是什么。 StackOverflow不是调试服务。