📄 jishiben.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileEdit extends Frame implements ActionListener
{
String directory;
TextArea textarea;
public FileEdit ()
{
this(null,null);
}
public FileEdit (String filename)
{
this(null,filename);
}
public FileEdit (String directory,String filename)
{
super();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
textarea = new TextArea("",24,80);
textarea.setFont(new Font("MonoSpced",Font.PLAIN,16));
textarea.setEditable(true);
this.add("Center",textarea);
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
this.add(p,"South");
Font font = new Font("SansSerif",Font.BOLD,14);
Button openfile = new Button("Open File");
Button close = new Button("Close");
Button save = new Button("Save");
Button saveas = new Button("Save as");
Button newfile = new Button("New File");
openfile.addActionListener(this);
openfile.setActionCommand("open");
openfile.setFont(font);
close.addActionListener(this);
close.setActionCommand("close");
//close.setMnemonic(KeyEvent.VK_C);
/*close.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_C)
{
actionPerformed(close.ActionCommand));
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
});*/
close.setFont(font);
save.addActionListener(this);
save.setActionCommand("save");
save.setFont(new Font("SansSerif",Font.BOLD,14));
saveas.addActionListener(this);
saveas.setActionCommand("saveas");
saveas.setFont(new Font("SansSerif",Font.BOLD,14));
newfile.addActionListener(this);
newfile.setActionCommand("newfile");
newfile.setFont(new Font("SansSerif",Font.BOLD,14));
p.add(newfile);
p.add(openfile);
p.add(save);
p.add(saveas);
p.add(close);
this.pack();
if (directory == null)
{
File f;
if ((filename != null) && (f = new File(filename)).isAbsolute())
{
directory = f.getParent();
filename = f.getName();
//System.out.println(filename);
}
else
{
directory = System.getProperty("user.dir");
}
}
this.directory = directory;
setFile(directory,filename);
}
public void setFile(String directory,String filename)
{
if ((filename == null) || (filename.length() == 0))
{
return;
}
File f;
BufferedReader in = null;
//FileReader in = null;//FileReader,FileWrite.操作字符流,FileInputStream,FileOnputStream操作字节流
/*
*java.io.Reader 和 java.io.InputStream 组成了 Java 输入类。Reader 用于读入16位字符,
*也就是 Unicode 编码的字符;而 InputStream 用于读入 ASCII 字符和二进制数据。
*/
try
{
f = new File(directory,filename);
in = new BufferedReader(new FileReader(f));
char[] buffer = new char[4069];
int len;
textarea.setText("");
while ((len = in.read(buffer)) != -1)
{
String s = new String(buffer,0,len);
textarea.append(s);
}
this.setTitle(directory + filename);
//textarea.append(this.getTitle());
textarea.setCaretPosition(0);
}
catch (IOException e)
{
textarea.setText(e.getClass().getName() + ": " + e.getMessage());
this.setTitle("FileEdit : " + directory + "\\" + filename + ": I/O Exception");
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException e)
{
}
}
}
public void saveFile(String directory,String filename,String text)
{
if ((filename == null) || (filename.length() == 0))
{
return;
}
File f;
BufferedWriter out = null;
try
{
f = new File(directory,filename);
out = new BufferedWriter(new FileWriter(f));
out.write(text);
out.flush();
}
catch (IOException e)
{
textarea.setText(e.getClass().getName() + ": " + e.getMessage() + "保存文件失败");
//this.setTitle("FileEeit : " + directory + "\\" + filename + ": I/O Exception");
}
finally
{
try
{
if (out != null)
{
out.close();
}
}
catch (IOException e)
{
}
}
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if (cmd.equals("open"))
{
FileDialog f = new FileDialog(this,"Open File",FileDialog.LOAD);
f.setDirectory(directory);
f.show();
directory = f.getDirectory();
setFile(directory,f.getFile());
f.dispose();
}
else if (cmd.equals("close"))
{
this.dispose();
}
else if (cmd.equals("save"))
{
/*FileDialog f = new FileDialog(this,"Save File",FileDialog.LOAD);
f.setDirectory(directory);
f.show();
directory = f.getDirectory();*/
String filepath = this.getTitle();
String directory = "",filename = "";
/*if ((filepath == null) || (filepath.length() == 0))
{
return;
}*/
int len = filepath.lastIndexOf("\\");
if (len >= 1)
{
directory = filepath.substring(0,len);
filename = filepath.substring(len + 1);
saveFile(directory,filename,textarea.getText());
}
else
{
FileDialog f = new FileDialog(this,"Save File",FileDialog.SAVE);
f.setDirectory(directory);
f.show();
directory = f.getDirectory();
saveFile(directory,f.getFile(),textarea.getText());
this.setTitle(directory + f.getFile());
f.dispose();
}
//f.dispose();
}
else if (cmd.equals("saveas"))
{
FileDialog f = new FileDialog(this,"Save File",FileDialog.SAVE);
f.setDirectory(directory);
f.show();
directory = f.getDirectory();
saveFile(directory,f.getFile(),textarea.getText());
f.dispose();
}
else if (cmd.equals("newfile"))
{
textarea.setText("");
this.setTitle("");
}
}
public void keyEvent()
{
}
public static void main(String[] args) throws IOException
{
//Frame f = new FileEdit ((args.length == 1)?args[0]:null);
Frame f = new FileEdit ();
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
f.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -