引用页_初学Java:仿写记事本_SaveFile.java

<< 返回
  

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

public class SaveFile {
	private Notepad notepad = null;
	private boolean saveDone = false;
	private File file = null;
	private StringBuffer filePath = null;
	private File currentFile = null;
	private JFileChooser fileChooser = null;
	private FileNameExtensionFilter filterTXT = null;	// TXT过滤器
	private int result = 0;
	private String strOld = null;
	private String strNew = null;
	
	public SaveFile(Notepad notepad) {
		this.notepad = notepad;
		this.fileChooser = new JFileChooser();
		this.currentFile = this.notepad.getCurrentFile();
		if(this.currentFile == null) {	//如果当前是一个新文件
			this.saveFile();			//显示选择框
		} else {
			this.file = this.currentFile;
			this.printFile();
		}
	}
	
	public SaveFile(Notepad notepad, String another) {
		this.notepad = notepad;
		this.fileChooser = new JFileChooser();
		
		this.currentFile = null;	//把当前当作新文件(实际上已经存在)
		this.saveFile();
	}
	
	private void saveFile() {
		this.filterTXT = new FileNameExtensionFilter("文本文档(*.txt)", "txt");
		this.fileChooser.setFileFilter(this.filterTXT);	//设置默认的TXT文件过滤器
		result = fileChooser.showSaveDialog(this.notepad.getFrame());
		if(result == JFileChooser.APPROVE_OPTION) {	//如果点击了“保存”
			this.file = fileChooser.getSelectedFile();	//得到已选择的文件
			if(this.fileChooser.getFileFilter() == this.filterTXT) { //如果当前的过滤器是TXT的
				System.out.println("选择器是TXT");
				this.filePath = new StringBuffer(file.getAbsolutePath());	//得到选择的文件的绝对路径
				if(".txt".equals(this.filePath.subSequence(this.filePath.length() - 4, this.filePath.length()))
						|| ".TXT".equals(this.filePath.subSequence(this.filePath.length() - 4, this.filePath.length()))
						) {	//如果文件后缀名是.txt或者.TXT
					System.out.println("后缀名是TXT");
				} else {	//如果后缀名不是.txt,也不是.TXT
					System.out.println("后缀名不是TXT");
					this.file = new File(this.filePath.toString() + ".txt");	//自动补上.txt后缀名
				}
			}	//如果当前不是TXT过滤器,则略过补全后缀名的操作
			if (this.currentFile == null) {	//如果这是一个新文件 
				if(this.file.exists()) {	//而且选择的文件已经存在了
					result = JOptionPane.showConfirmDialog(fileChooser, "该文件已存在,是否覆盖?", "确认", 1);
					if(result == JOptionPane.YES_OPTION) {	//如果选择了“是”
						this.printFile();
					}
				} else {	//当前选择的文件不存在
					this.printFile();
				}
			} else if (this.file.getAbsolutePath().equals(this.currentFile.getAbsolutePath())) {
				//如果选择的文件就是当前文件
				this.printFile();
			} else {	//如果选择的文件不是当前文件
				if(this.file.exists()) {	//而且选择的文件已经存在了
					result = JOptionPane.showConfirmDialog(fileChooser, "该文件已存在,是否覆盖?", "确认", 1);
					if(result == JOptionPane.YES_OPTION) {	//如果选择了“是”
						this.printFile();
					}
				} else {	//当前选择的文件不存在
					this.printFile();
				}
			}
		} else {
			file = null;
		}
	}
	
	private void printFile() {	// 文件内容输出
		if (this.file.canWrite() || !this.file.exists()) {
			try {
				PrintStream out =  new PrintStream(new FileOutputStream(file));
				strOld = new String(this.notepad.getTextArea().getText());
				strNew = strOld.replaceAll("\\n", "\r\n");	//正则替换,Windows使用\r\n表示换行
				out.print(strNew);
				out.close();
				System.out.println("保存的内容为:\n" + strNew);
				notepad.setCurrentFile(this.file);
				notepad.changTitle(this.file.getName());
				notepad.setSavedString(this.notepad.getTextArea().getText());
				this.saveDone = true;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(fileChooser, "该文件为只读的,无法写入!");
		}
	}
	
	public boolean saveDone() {
		return this.saveDone;
	}
}

  
<< 返回

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值