📄 notepad_savefile.java
字号:
/**
*
*/
package com.edu.sccp.snail.notepad.view;
import java.io.*;
import javax.swing.*;
import com.edu.sccp.snail.notepad.interfaces.Notepad_File;
/**
* @author 小豆包
* @return 右键菜单事件
* @time 2008-1-7
*/
public class Notepad_SaveFile implements Notepad_File {
private static final long serialVersionUID = -9113141423140078072L;
Notepad_newFile nnf = null;
Notepad_Main parent = null;
File file = null;
//FileWriter fw = null;
FileOutputStream fos = null;
public Notepad_SaveFile() {
}
public Notepad_SaveFile(Notepad_Main parent_frame) {
this.parent = parent_frame;
Notepad_JFileChooser notepad_filechooser = new Notepad_JFileChooser(
file);
int returnVal = notepad_filechooser.showSaveDialog(parent.notepad);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = notepad_filechooser.getSelectedFile();
this.Notepad_FileSave(parent, file);
} else {
return;
}
}
/**
* 获取文件后缀名
* @param FileSrc
* @return a String postfix name
*/
public String getExt(File FileSrc){
String Src = FileSrc.getAbsolutePath();
String srcExt = Src.substring(Src.length() - 4,Src.length()).toLowerCase(); //转化成小写为什么?
return srcExt;
//返回文件后缀名
}
/**
* @param file
* @return a boolean
* then judge is it have a postfix
* @author 小豆包
* @time 08/1/8
*/
public boolean haveExt(File file){
String src = getExt(file);
if(src.equals("java") || src.equals("txt")){
return true;
}
return false;
}
/**
* 保存代码块
*/
public void Notepad_FileSave(Notepad_Main notepad_main, File file) {
if(!haveExt(file)){
file = new File(file.getAbsoluteFile()+".txt");
}
String str = null;
byte buf[] = new byte[1024];
if(notepad_main.nf instanceof Notepad_newFile){
if(notepad_main.nf != null){
this.nnf = notepad_main.nf;
///////////////////////////////////////////////////////////////////////////////////////
//构造流然后进行一般的读写操作
if(file !=null){
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}if(file.canWrite()){
try {
fos = new FileOutputStream(file);
str = nnf.jta.getText();
buf= str.getBytes();
fos.write(buf,0,str.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
notepad_main.jb3.setEnabled(false); //设置保存按钮变灰显
///////////////////////////////////////////////////////////////////////////////////////
}else{
//还没有数据要保存
JOptionPane.showMessageDialog(notepad_main.notepad,"还没有数据要保存");
return;
}
}
}
public void Notepad_File_OrtherSave(Notepad_Main notepad_main, File file) {
}
public void Notepad_FileOpen(Notepad_newFile nnf, File file) {
}
public void Notepad_File_TreeOpenFile(Notepad_Main notepad_main, File file) throws IOException {
}
public void Notepad_File_TreeOpenFile(Notepad_Main notepad_main, Notepad_newFile nnf, File file) throws IOException {
// TODO Auto-generated method stub
}
public void Notepad_File_TreeNewFile(Notepad_Main notepad_main, Notepad_newFile nnf, File file) throws IOException {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -