java编写的记事本的保存和另存为功能
你要的关键程序代码:。(源文件超过10000字,无法粘全在这,要的话,加我)if(e.getActionCommand()==”保存”||e.getActionCommand()==”保存为”)
{
JFileChooser chooser=new JFileChooser(); //创建文件选择对话框
int result=chooser.showSaveDialog(frame);
if(result==JFileChooser.APPROVE_OPTION)
{
try
{
FileWriter fout = new FileWriter(chooser.getSelectedFile()); //向磁盘中写文件
fout.write(jtextpane.getText()+”\r\n”);
fout.close();
}
catch (IOException ioex)
{
}
}
}执行效果:
创建gui,实现记事本【file】菜单下的【新建】、【打开】、【保存】、【另存为],退出等功能
新建打开等操作都可以用ofstream 类进行操作,但是有一点儿你要注意。每一个的程序的这些菜单的代码不是都一样的。只不过实现的方法大致相同,因为要看你新建文件的类型。如果只是文档的话随便百度都有。 ofstream file; file.open(“data.txt”,ios::app); file.close(); 这是打开文件的操作的类。
用JAVA编写一个GUI记事本程序,实现文本的输入,保存,修改,打开操作
代码如下:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class jtxtfm{
public static void main(String args[]){
jtxtfrm fm=new jtxtfrm();
}
}
class jtxtfrm extends Frame implements ActionListener{
FileDialog op,sv;
Button btn1,btn2,btn3;
TextArea tarea;
jtxtfrm(){
super(“读写文件”);
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=new Button(“打开”);
btn2=new Button(“保存”);
btn3=new Button(“关闭”);
tarea=new TextArea(“”);
add(btn1);add(btn2);add(btn3);add(tarea);
tarea.setBounds(30,50,460,220);
btn1.setBounds(520,60,50,30);
btn2.setBounds(520,120,50,30);
btn3.setBounds(520,180,50,30);
op=new FileDialog(this,”打开”,FileDialog.LOAD);
sv=new FileDialog(this,”保存”,FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
String str;
op.setVisible(true);
try{
File f1=new File(op.getDirectory(),op.getFile());
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
tarea.setText(“”);
while((str=br.readLine())!=null)tarea.append(str+’\n’);
fr.close();
}
catch(Exception e1)
{}
}
if(e.getSource()==btn2){
sv.setVisible(true);
try{
File f1=new File(sv.getDirectory(),sv.getFile());
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
String gt=tarea.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch ( Exception e2)
{}
}
if(e.getSource()==btn3){
System.exit(0);
}
}
}
效果图:
java&GUI&文件保存
用ide软件编程比较快,比较容易用的有netbeans
,直接可以选择gui的工具就可以完成界面了。。。
然后在按键上加入监听就完成了。。。
如何用Java实现另存为
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class BakTo extends JFrame implements ActionListener {
JLabel l1 = new JLabel(“原始文件”);
JTextField t1 = new JTextField(40);
JButton b1 = new JButton(“选择”);
JLabel l2 = new JLabel(“保存目录”);
JTextField t2 = new JTextField(40);
JButton b2 = new JButton(“保存”);
JFileChooser j1 = new JFileChooser();
JFileChooser j2 = new JFileChooser();
static File fileFlag = new File(“”);
public BakTo() {
setBounds(200, 200, 600, 140);
setLayout(new FlowLayout());
add(l1);
add(t1);
add(b1);
add(l2);
add(t2);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setResizable(false);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b1) {
int n = j1.showOpenDialog(null);
String filename = j1.getSelectedFile().toString();
if (n == JFileChooser.APPROVE_OPTION) {
t1.setText(filename);
fileFlag = new File(filename);
}
}
else if (e.getSource() == b2) {
j2.setCurrentDirectory(fileFlag);// 设置打开对话框的默认路径
j2.setSelectedFile(fileFlag);// 设置选中原来的文件
int n = j2.showSaveDialog(null);
String filename2 = j2.getSelectedFile().toString();
if(filename2.indexOf(“.”)!=-1){
filename2=filename2.substring(0,filename2.indexOf(“.”));
}
// 以下两句是获得原文件的扩展名
int flag = t1.getText().lastIndexOf(“.”);
String kuozhan = t1.getText().substring(flag);
String date = getDate();// 取得当前日期
if (n == JFileChooser.APPROVE_OPTION) {
t2.setText(filename2 +date+ kuozhan);// 把日期和扩展名添加到原来文件的后面
}
int b;
char[] t = new char[25];
// 这里我改用了文件流
FileInputStream input = new FileInputStream(t1.getText());
FileOutputStream output = new FileOutputStream(filename2+date
+ kuozhan);// 把扩展名添加到原来文件的后面
int in = input.read();
while (in != -1) {
output.write(in);
in = input.read();
}
input.close();
output.close();
}
} catch (Exception x) {
System.out.println(x);
}
}
public String getDate() {
Calendar rightNow = Calendar.getInstance();
System.out.println(rightNow.toString());
int year = rightNow.YEAR;
int date = rightNow.DATE;
int month = rightNow.MONTH + 1;
String d = year + “年” + month + “月” + date + “日”;
return d;
}
public static void main(String args[]) {
BakTo c1 = new BakTo();
}
}