java怎么保存
㈠ 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 生成的实例如何保存
这游戏我也写过,呵呵!你可以把它设计为单例模式,每次得到的就是同一个对象的实例了。单例的步骤:先定义一个本类的私有属性,提供set和get方法(private Game game;)。再把写一个静态语句块,把此对象的实例生成放在里面(static (game=new Game();))。最后构造私有(private void Game(){})。再提供一个公开的方法(public static Game getgame(){return game;} )。这个类就完成了,以后不论到那里想得到game都只用调这公开方法得到唯一的game对象的实例。
㈢ java怎样将数据保存到缓存中,之后再保存
Java中可以使用队列来保存数据,当使用的时候,加上锁,防止其他进程访问,当不用的时候保存到数据库里面,示例如下:
packagecom.henry;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Random;
importjava.util.concurrent.locks.ReadWriteLock;
importjava.util.concurrent.locks.ReentrantReadWriteLock;
publicclassCacheDataTest{
staticMap<Integer,Object>dataMap=newHashMap<Integer,Object>();
staticReadWriteLocklock=newReentrantReadWriteLock();//创建读写锁的实例
staticObjectgetData(Integerkey){
lock.readLock().lock();//读取前先上锁
Objectval=null;
try{
val=dataMap.get(key);
if(val==null){
//
lock.readLock().unlock();
lock.writeLock().lock();
try{
//可能已经由其他线程写入数据
if(val==null){
//dataMap.put(key,"");//queryfromdb
val=queryDataFromDB(key);
}
}finally{
//
lock.readLock().lock();
//Unlockwrite,stillholdread
lock.writeLock().unlock();
}
}
}finally{
lock.readLock().unlock();//最后一定不要忘记释放锁
}
System.out.println("getdatakey="+key+">val="+val);
returnval;
}
staticObjectqueryDataFromDB(Integerkey){
Objectval=newRandom().nextInt(1000);
dataMap.put(key,val);
System.out.println("writeintodatakey="+key+">val="+val);
returnval;
}
publicstaticvoidmain(String[]args){
for(inti=0;i<10;i++){
newThread(newRunnable(){publicvoidrun(){
getData(newRandom().nextInt(5));
}}).start();
}
}
}
㈣ java怎样保存打开就可执行的文件
会生成.
class文件,只能用反编译软件看,在你的eclipse工作空间,一般是workspace下的工程中,路径时你自己指定的.
编译输出路径的默认位置,普通工程:bin,web工程:WEB-INF/classes,maven工程:target/classes.
㈤ 怎么用java储存文件(save file)
这是我原来做的例子,里面有文件储存的内容,代码不多,给你参考参考.
/**
* 五个按钮的故事,西西哈。
*/
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的保存
记事本写完后 选择另存为
然后记得 下拉框选 所有文件
然后文件名再写 XX.JAVA
㈦ 如何保存.java格式的文件
你打开我的电脑后 , 选择 工具 查看
在下面的列表中 你找到
隐藏已知文件类型的扩展名
把这项前面的 对号 去掉
你再去记事本里保存一次看看
㈧ 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里数据怎么保存到硬盘或TXT文件里去
Java是通过使用I/O文件操作类,创建输入输出流,将数据保存在指定的路径下的文件里面。
示例代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileTest {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("D:/test.txt");//初始化file
fop = new FileOutputStream(file);//初始化输出流
// 若文件不存在,则创建它
if (!file.exists()) {
file.createNewFile();
}
// 获取字节的内容数组
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);//写出到指定路径文件中字节的内容数组
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) { //捕捉异常
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) { //捕捉异常
e.printStackTrace();
}
}
}
}