当前位置:首页 » 编程语言 » java存

java存

发布时间: 2023-06-11 08:53:59

‘壹’ java怎么保存文件

可以使用java.io.FileOutputStream流保存任意文件或者用java.io.ObjectOutputStream流保存类文件

‘贰’ java如何保存文件

这是我原来做的例子,里面有文件储存的内容,代码不多,给你参考参考.
/**
* 五个按钮的故事,西西哈。
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileMessage extends Frame implements ActionListener
{
private static final long serialVersionUID = 10L;
Dialog dia;
private Panel p;
private File fi;
Process po=null;
private String s;
private TextArea ta;
private FileDialog fd;
private Button b1,b2,b3,b4,b5;
private Button b6;
public FileMessage()
{
super("文本文件处理");
setBackground( Color.LIGHT_GRAY );
setLocation(200,300);
setResizable( false);
setVisible( true);
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit( 0);
}
});
}
public void init()
{
ta=new TextArea("\n\n\n\n\n\t\t\t\t文本显示区");
ta.setSize(30,5);
ta.setEditable(false);
add( ta,"North");
p=new Panel();
add( p,"Center");
b1=new Button("浏览");
b2=new Button("保存");
b3=new Button("清空");
b4=new Button("关闭");
b5=new Button("独立打开");
b6=new Button("确定");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
fd=new FileDialog(this,"请选择文件",FileDialog.LOAD);
fd.setDirectory("f:\\note");
pack();
dia=new Dialog(this,"注意",true);
dia.setLayout(new BorderLayout());
Panel p1=new Panel();
p1.add( b6);
dia.add(new Label(" 请先选择文件"),BorderLayout.CENTER);
dia.add( p1,BorderLayout.SOUTH);
dia.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dia.setVisible( false);
}
});
dia.setLocation(310,370);
dia.setSize(200,130);
}
public static void main(String[] args)
{
new FileMessage().init();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
fd.setVisible(true);
s=fd.getDirectory()+fd.getFile();
fi=new File(s);
byte[] b=new byte[(int)fi.length()];
try
{
new FileInputStream(fi).read(b);
ta.setText(new String(b,0,(int)fi.length()));
}
catch(Exception e1){}
ta.setEditable(true);
}
else if(e.getSource()==b2)
{
try
{
if(ta.getText().equals("保存成功")||ta.getText() .equals( ""))
{}
else
{
new FileOutputStream(fi).write(ta.getText().getBytes());
ta.setText("保存成功");
ta.setEditable(false);
}
}
catch(FileNotFoundException e1)
{
ta.setText(e1.getMessage());
}
catch(IOException e1)
{
ta.setText("出现IOException异常");
}
}
else if(e.getSource()==b4)
System.exit(0);
else if(e.getSource()==b3)
{
ta.setText("");
ta.setEditable( false);
}
else if(e.getSource()==b5)
{
if(s==null)
{
dia.setVisible(true);
}
else
{
try
{
po=Runtime.getRuntime().exec("notepad.exe "+s);
}
catch(Exception ei)
{}
}
}
else if(e.getSource() ==b6)
{
dia.setVisible(false);
}
}
}

‘叁’ java如何实现文本保存

try{ FileOutputStream fos=new FileOutputStream("test.txt",true);//true表明会追加内容 PrintWriter pw=new PrintWriter(fos); pw.write(你想写入的内容); pw.flush(); }catch(FileNotFoundException e){ e.printStackTrace(); }finally{ try{ pw.close(); }catch(Exception e){ e.printStackTrace(); } }

‘肆’ java 怎么把数据存到内存中

这里你采纳与否没关系,给你说说编程与内存的关系。
你定义的任何变量,常量,类,方法等等,其实都在内存中,没有所谓的把数据存内存中,这概念,你可以想一下电脑重启或关机后,内存中的所有数据,都会丢失,除非你保存到磁盘中去。
在内存中的数据有两块,第一、缓冲,一般写数据到磁盘的时候开辟出来的内存空间;第二、缓存,一般是从磁盘读数据到内存中开辟出来的内存空间。会这么使用,原因很简单,磁盘读写数据速度与内存不一致(磁盘的存取效率远远小于内存的存取效率),为了提高数据的存取效率,才会这么干的。
一般而言,java中的所谓数据,大部分都是类,从自动引用计数的概念来分析,你想把对象长久的放在内存中,不会被垃圾回收机制释放,注意制药有一个对象在使用/引用你的数据,这条数据就会存在内存中。所以,想servlet中的全局配置参数,随时可以取到还是唯一一份,你可以参考一下。
另外内存使用分堆与栈,堆在面向对象编程中存储对象的,栈是方法或函数执行的时候临时开辟的存储空间,方法或函数执行完毕就会释放。
希望我的回复能帮助到你,采纳与否没关系。有更好的答案,我就隐藏我的回复。

热点内容
高级语言都要编译解析型语言 发布:2025-02-13 15:06:32 浏览:304
openwrt源码下载 发布:2025-02-13 15:01:59 浏览:644
linux删除一个目录 发布:2025-02-13 15:00:29 浏览:539
蚂蚁存储 发布:2025-02-13 15:00:25 浏览:918
脚本师传奇 发布:2025-02-13 14:45:48 浏览:481
我的世界lce服务器剪辑 发布:2025-02-13 14:40:50 浏览:625
phpsftp上传 发布:2025-02-13 14:35:43 浏览:274
c学生管理系统数据库 发布:2025-02-13 14:21:41 浏览:123
传奇添加会员脚本 发布:2025-02-13 14:20:50 浏览:206
微信开发平台源码 发布:2025-02-13 14:14:20 浏览:614