java如何保存文件

java如何保存文件,第1张

这是我原来做的例子,里面有文件储存的内容,代码不多,给你参考参考.

/**

* 五个按钮的故事,西西哈。

*/

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)

}

}

}

1、首先需要在记事本中编写一个“hello,下午好”程序。

2、编写完成后,保存该文件,并将文件名改为与类名相同。

3、把文件的格式从.txt改成.java文件。

4、更改完毕后,打开cmd指令(wins+R)。

5、输入javac,如果下方出现许多东西,则说明环境变量已经配置成功,否则就要去配置环境变量。

6、找到.java文件所在的位置,也在cmd中找到它(D盘)。

7、在cmd中进入这个.java文件所在的文件夹,输入javac   类名.java ,此时在.java文件所存在的地方出现同名.class文件。

8、再输入java  类名,下方便会运行出.java文件,输出“hello,下午好”。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/7936395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存