我正在使用我的文字处理器,我已经设法使保存文件正常工作。然而,我遇到了一个令人讨厌的错误,保存文件会导致文件名(键入的)重复。例如,如果你键入文件名"test",文件将保存,它将显示为"testtest.txt"。如果可以提供代码审查,我也非常感谢。我也在努力遵守MVC(模型-视图-控制器范式),我想知道我做得是否正确,我能进一步抽象它吗?无论如何,这是我的代码:型号
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStuff {
private File file;
String text;
String name;
private File saveFile;
int counter = 0;
FileInputStream fis = null;
FileOutputStream fout = null;
StringBuilder sb = new StringBuilder(4096);
int count = 0;
public void loadFile(File fileName){
this.file = fileName;
try{
fis = new FileInputStream(file);
while ((counter = fis.read()) != -1) {
System.out.print((char) counter);
sb.append((char) counter);
}
}
catch(IOException ex){
System.out.println("file couldn't be opened, or was incorrect or you clicked cancel");
}
finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public StringBuilder showText(){
return sb;
}
public void saveFile(String name, String text) {
this.name = name;
try{
fout = new FileOutputStream(name);
fout.write(text.getBytes());
System.out.println("file saving worked");
}
catch(IOException e){
System.out.println("File failed to save or something went horribly wrong");
}
}
}
这就是View。
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class WordFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JMenuBar menubar;
private JMenu fileMenu, editMenu, viewMenu;
JMenuItem saveMenuItem, openMenuItem, newMenuItem, exitMenuItem, FontMenuItem;
JTextArea textArea = new JTextArea(1000,900);
private int width = 1280, height = 980;
private JScrollPane scrollbar = new JScrollPane(textArea);
JFileChooser fileChooser = new JFileChooser();
private int textHeight = 12;
private Font yeah = new Font(Font.SANS_SERIF, 2, textHeight);
public WordFrame(){
setUI();
addMenuBar();
textArea.setFont(yeah);
}
public void setUI(){
this.setTitle("Word Processor");
this.setIconImage(new ImageIcon(this.getClass().getResource("Bridge.jpg")).getImage());
this.setSize(width, height);
this.setLocation(0,0);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(scrollbar);
}
public void addMenuBar(){
menubar = new JMenuBar();
fileMenu = new JMenu(" File ");
editMenu = new JMenu("Edit ");
viewMenu = new JMenu("View ");
newMenuItem = new JMenuItem("New");
fileMenu.add(newMenuItem);
fileMenu.addSeparator();
fileMenu.setMnemonic('f');
openMenuItem = new JMenuItem("Open");
fileMenu.add(openMenuItem);
saveMenuItem = new JMenuItem("Save");
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
exitMenuItem = new JMenuItem("Exit");
fileMenu.add(exitMenuItem);
FontMenuItem = new JMenuItem("Font");
editMenu.add(FontMenuItem);
menubar.add(fileMenu);
menubar.add(editMenu);
menubar.add(viewMenu);
this.setJMenuBar(menubar);
}
public void setFontSize(int i){
this.textHeight = i;
}
public void addListener(ActionListener listener){
openMenuItem.addActionListener(listener);
exitMenuItem.addActionListener(listener);
saveMenuItem.addActionListener(listener);
}
}
最后是控制器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class ProcessEvents {
private WordFrame frame = new WordFrame();
private DataStuff data = new DataStuff();
private DialogBoxes dialogs = new DialogBoxes();
private boolean fileSaved;
String fileName = "";
int fontSize = 0;
public ProcessEvents(WordFrame frame, DataStuff data){
this.frame = frame;
this.data = data;
this.frame.addListener(new wordProcessListener());
}
class wordProcessListener implements ActionListener{
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(frame.openMenuItem)){
frame.fileChooser.showOpenDialog(frame);
File f = frame.fileChooser.getSelectedFile();
System.out.println("Command Executed: open");
data.loadFile(f.getAbsoluteFile());
if(data.showText() != null){
System.out.println(data.showText());
frame.textArea.append(data.showText().toString());
}
}
if(e.getSource().equals(frame.FontMenuItem)){
System.out.println("font");
}
if(e.getSource().equals(frame.exitMenuItem)){
dialogs.getConfirmDialog("exitWithoutSave");
}
if(e.getSource().equals(frame.saveMenuItem)){
frame.fileChooser.showSaveDialog(null);
File f = frame.fileChooser.getSelectedFile();
String text = frame.textArea.getText();
data.saveFile(f.getAbsolutePath()+f.getName()+".txt", text);
System.out.println(f.getName());
fileSaved = true;
}
}
}
}
我打算实现字体选择等功能。我非常感谢对我的代码的批评,以及我未能发现的任何弱点等,一如既往,我非常感谢任何反馈。
edit:我还想问,另一个令人讨厌的方面是,如果你决定取消保存文件,我会得到一个堆栈跟踪,说文件为空——任何解决方案,因为我试图在DataStuff中的saveFile方法中添加一个if块,但它不起作用,所以它发生在控制器中。
重复的名称是因为您正在执行f.getAbsolutePath()+f.getName()+".txt"
-我认为getAbsolutePath
已经包含了文件名。
对于代码审查,有一个堆栈交换网站就是为此而设置的,你会在那里得到更好的响应。