我正在处理我的单词处理器,我设法让保存文件工作。但是我遇到一个恼人的错误,保存文件将导致文件名(输入)复制自己,例如,键入文件名“test”文件将保存,它将显示为“testtest。文本”。如果可以提供,我也非常感谢代码审查。 我也试图坚持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");
}
}
}
这是视图。
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;
}
}
}
}
我打算实现字体选择等功能。我非常感谢对我的代码的批评,我没有发现任何弱点等等,而且我一直非常感谢任何反馈。
编辑:我也想问一下,另一个烦人的方面是如果你决定取消保存文件我得到一个堆栈跟踪说文件是null任何解决方案,因为我试图在saveFile方法中添加一个if块DataStuff它没有工作,所以它发生在控制器中。
答案 0 :(得分:2)
重复的名称是因为您正在执行f.getAbsolutePath()+f.getName()+".txt"
- 我认为getAbsolutePath
已包含文件名。
对于代码审查,有一个堆栈交换站点就是这样设置的,你会得到更好的响应。