java文件保存,java文件保存路徑

本文目錄一覽:

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的mgc文件保存不了

1、java的mgc文件保存不了是因為系統的C盤或者儲存文件的磁盤空間滿了導致無法保存。

2、還有可能是java的mgc文件出現異常保存不了mgc格式。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/275994.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-17 19:38
下一篇 2024-12-17 19:38

相關推薦

發表回復

登錄後才能評論