java|I/o输入输出怎么存储和读取对象
2个回答
2013-07-28
展开全部
这是我作的记事本,你看看。I/O流的我在里边有标注,这样你看比较明显。
/**
* 思路:做一个记事本,添加菜单,多行文本框,提供相关功能
* 菜单栏的目录有文件,编辑,查看,搜索,工具,文档,帮助
* 文件包括:新建,打开,保存,页面设置,打印,退出
* 编辑:撤销,剪切,复制,粘贴,删除,查找,替换,全选
* ......
* 帮助:关于
*/
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.*;
public class MyText extends JFrame implements ActionListener, WindowListener {
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private JScrollBar sb;
private JTextArea txtArea;
private JFileChooser chooser;
private JScrollPane sp;
private JOptionPane op;
private String[] strMenu;
private String[][] strMenuItem;
private final static int I = 20;
private static int x = 100;
private static int y = 100;
private int width = 600;
private int height = 500;
private String name;
public MyText() {
super("我的记事本");
menuBar = new JMenuBar();
strMenu = new String[] { "文件", "编辑", "查看", "搜索", "工具", "文档", "帮助" };
strMenuItem = new String[][] {
{ "新建", "打开", "保存", "另存为", "页面设置", "打印", "退出" },
{ "撤销", "剪切", "复制", "粘贴", "删除", "替换", "全选", },
{ "查找", "自动换行" }, {}, {}, {}, { "关于" } };
for (int i = 0; i < strMenu.length; i++) {
menu = new JMenu(strMenu[i]);
for (int j = 0; j < strMenuItem[i].length; j++) {
menuItem = new JMenuItem(strMenuItem[i][j]);
menu.add(menuItem);
menuItem.addActionListener(this);
}
menuBar.add(menu);
}
txtArea = new JTextArea();
// sb=new JScrollBar();
sp = new JScrollPane(txtArea);
// sp.setHorizontalScrollBarPolicy(
// JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// sp.setVerticalScrollBarPolicy(
// JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sp.getViewport().add(txtArea);
//
// sp.setVisible(true);
// add(txtArea);
getContentPane().add(sp,BorderLayout.CENTER);
// add(pf,BorderLayout.EAST);
// txtArea.setLineWrap(true);
// add(sb, BorderLayout.EAST);
//分别设置水平和垂直滚动条自动出现
// JPanel jpanel=new JPanel();
// jpanel.add(sp);
// add(jpanel,BorderLayout.EAST);
// add(menuBar,BorderLayout.NORTH);
setJMenuBar(menuBar);
// addWindowListener(this);//若加此句,则会将多个窗体一并关闭。
// setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// setSize(500,400);
setBounds(x, y, width, height);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if ("新建".equals(comm)) {
newTextArea();
} else if ("打开".equals(comm)) {
openTextArea();
} else if ("保存".equals(comm)) {
saveTextArea();
} else if ("另存为".equals(comm)) {
otherSaveTextArea();
} else if ("退出".equals(comm)) {
exitTextArea();
} else if ("复制".equals(comm)) {
txtArea.copy();
} else if ("剪切".equals(comm)) {
txtArea.cut();
} else if ("粘贴".equals(comm)) {
txtArea.paste();
} else if ("删除".equals(comm)) {
txtArea.replaceSelection("");
} else if ("替换".equals(comm)) {
op.showInputDialog(null, "f", "fd", JOptionPane.QUESTION_MESSAGE);
txtArea.replaceSelection("good");
}// 方法未真正实现
else if ("查找".equals(comm)) {
op.showInputDialog(null, "请输入你要查找的内容", "查找",
JOptionPane.INFORMATION_MESSAGE);
} else if ("全选".equals(comm)) {
txtArea.selectAll();
} else if ("自动换行".equals(comm)) {
txtArea.setLineWrap(true);
}
}
public void newTextArea() {
x += I;
y += I;
new MyText();
}
public void openTextArea() {//读取数据
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int result = chooser.showOpenDialog(null);
// if file selected,set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION) {
name = chooser.getSelectedFile().getPath();
// txtArea.setText(name);
try {
FileInputStream fis = new FileInputStream(name);
byte[] b = new byte[1024];
String s = "";// 以字符串的格式读出来
while (true) {
int i = fis.read(b);
if (i == -1) {
break;
}
s = s + new String(b, 0, i);
}
fis.close();// 关闭流
txtArea.setText(s);
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
public void exitTextArea() {
// op = new JOptionPane();
// int i = op.showConfirmDialog(null, "是否保存并退出", "关闭提示",
// JOptionPane.YES_NO_CANCEL_OPTION);
// if (i == 0) {
// System.out.println("保存并退出");
System.exit(0);
// } else if (i == 1) {
// System.out.println("不保存并退出");
//
// } else if (i == 2) {
// System.out.println("取消退出");
// }
}
public void saveTextArea() {
try {
FileOutputStream fos = new FileOutputStream(name);// 默认为false,如果为("wtest.txt",true)为追加,如果为("wtest.txt",false)为替换,即先清空文本
fos.write(txtArea.getText().getBytes());
fos.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void otherSaveTextArea() {//存取数据
try {
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int result = chooser.showSaveDialog(null);
name = chooser.getSelectedFile().getPath();
System.out.println(name);
FileOutputStream fos = new FileOutputStream(name);// 默认为false,如果为("wtest.txt",true)为追加,如果为("wtest.txt",false)为替换,即先清空文本
fos.write(txtArea.getText().getBytes());
fos.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
exitTextArea();
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public static void main(String[] args) {
MyText myText= new MyText();
myText.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/**
* 思路:做一个记事本,添加菜单,多行文本框,提供相关功能
* 菜单栏的目录有文件,编辑,查看,搜索,工具,文档,帮助
* 文件包括:新建,打开,保存,页面设置,打印,退出
* 编辑:撤销,剪切,复制,粘贴,删除,查找,替换,全选
* ......
* 帮助:关于
*/
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.*;
public class MyText extends JFrame implements ActionListener, WindowListener {
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private JScrollBar sb;
private JTextArea txtArea;
private JFileChooser chooser;
private JScrollPane sp;
private JOptionPane op;
private String[] strMenu;
private String[][] strMenuItem;
private final static int I = 20;
private static int x = 100;
private static int y = 100;
private int width = 600;
private int height = 500;
private String name;
public MyText() {
super("我的记事本");
menuBar = new JMenuBar();
strMenu = new String[] { "文件", "编辑", "查看", "搜索", "工具", "文档", "帮助" };
strMenuItem = new String[][] {
{ "新建", "打开", "保存", "另存为", "页面设置", "打印", "退出" },
{ "撤销", "剪切", "复制", "粘贴", "删除", "替换", "全选", },
{ "查找", "自动换行" }, {}, {}, {}, { "关于" } };
for (int i = 0; i < strMenu.length; i++) {
menu = new JMenu(strMenu[i]);
for (int j = 0; j < strMenuItem[i].length; j++) {
menuItem = new JMenuItem(strMenuItem[i][j]);
menu.add(menuItem);
menuItem.addActionListener(this);
}
menuBar.add(menu);
}
txtArea = new JTextArea();
// sb=new JScrollBar();
sp = new JScrollPane(txtArea);
// sp.setHorizontalScrollBarPolicy(
// JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// sp.setVerticalScrollBarPolicy(
// JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sp.getViewport().add(txtArea);
//
// sp.setVisible(true);
// add(txtArea);
getContentPane().add(sp,BorderLayout.CENTER);
// add(pf,BorderLayout.EAST);
// txtArea.setLineWrap(true);
// add(sb, BorderLayout.EAST);
//分别设置水平和垂直滚动条自动出现
// JPanel jpanel=new JPanel();
// jpanel.add(sp);
// add(jpanel,BorderLayout.EAST);
// add(menuBar,BorderLayout.NORTH);
setJMenuBar(menuBar);
// addWindowListener(this);//若加此句,则会将多个窗体一并关闭。
// setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// setSize(500,400);
setBounds(x, y, width, height);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if ("新建".equals(comm)) {
newTextArea();
} else if ("打开".equals(comm)) {
openTextArea();
} else if ("保存".equals(comm)) {
saveTextArea();
} else if ("另存为".equals(comm)) {
otherSaveTextArea();
} else if ("退出".equals(comm)) {
exitTextArea();
} else if ("复制".equals(comm)) {
txtArea.copy();
} else if ("剪切".equals(comm)) {
txtArea.cut();
} else if ("粘贴".equals(comm)) {
txtArea.paste();
} else if ("删除".equals(comm)) {
txtArea.replaceSelection("");
} else if ("替换".equals(comm)) {
op.showInputDialog(null, "f", "fd", JOptionPane.QUESTION_MESSAGE);
txtArea.replaceSelection("good");
}// 方法未真正实现
else if ("查找".equals(comm)) {
op.showInputDialog(null, "请输入你要查找的内容", "查找",
JOptionPane.INFORMATION_MESSAGE);
} else if ("全选".equals(comm)) {
txtArea.selectAll();
} else if ("自动换行".equals(comm)) {
txtArea.setLineWrap(true);
}
}
public void newTextArea() {
x += I;
y += I;
new MyText();
}
public void openTextArea() {//读取数据
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int result = chooser.showOpenDialog(null);
// if file selected,set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION) {
name = chooser.getSelectedFile().getPath();
// txtArea.setText(name);
try {
FileInputStream fis = new FileInputStream(name);
byte[] b = new byte[1024];
String s = "";// 以字符串的格式读出来
while (true) {
int i = fis.read(b);
if (i == -1) {
break;
}
s = s + new String(b, 0, i);
}
fis.close();// 关闭流
txtArea.setText(s);
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
public void exitTextArea() {
// op = new JOptionPane();
// int i = op.showConfirmDialog(null, "是否保存并退出", "关闭提示",
// JOptionPane.YES_NO_CANCEL_OPTION);
// if (i == 0) {
// System.out.println("保存并退出");
System.exit(0);
// } else if (i == 1) {
// System.out.println("不保存并退出");
//
// } else if (i == 2) {
// System.out.println("取消退出");
// }
}
public void saveTextArea() {
try {
FileOutputStream fos = new FileOutputStream(name);// 默认为false,如果为("wtest.txt",true)为追加,如果为("wtest.txt",false)为替换,即先清空文本
fos.write(txtArea.getText().getBytes());
fos.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void otherSaveTextArea() {//存取数据
try {
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int result = chooser.showSaveDialog(null);
name = chooser.getSelectedFile().getPath();
System.out.println(name);
FileOutputStream fos = new FileOutputStream(name);// 默认为false,如果为("wtest.txt",true)为追加,如果为("wtest.txt",false)为替换,即先清空文本
fos.write(txtArea.getText().getBytes());
fos.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
exitTextArea();
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public static void main(String[] args) {
MyText myText= new MyText();
myText.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-28
展开全部
把人物属性 的值 保存到 文件中 下一次 开始游戏的 读取任务属性的值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询