资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java保存文件代码,java 存储文件

java如何保存文件

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

创新互联公司-专业网站定制、快速模板网站建设、高性价比卓资网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式卓资网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖卓资地区。费用合理售后完善,十载实体公司更值得信赖。

/**

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

*/

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将数据保存到txt文件

首先创建一个新的txt文件,然后new File(“txt文件路径”),

封装一个输入输出流,将要写入的数据写入到txt中,刷新流,关闭流。

代码如下:

public static void main(String[] args) throws IOException{

String str = "这个项目什么时候上线";

File file;//创建文件夹

FileOutputStream stream = null;//new文件流

try {

file = new File("C:/Users/qisf/Desktop/Aa.txt");

stream = new FileOutputStream (file);//将文件夹放在文件流中

if (!file.exists()) {

file.createNewFile();

}

byte[] contentInBytes = str.getBytes();//转化成字节形

stream.write(contentInBytes);//写入

stream.flush(); //写完之后刷新

stream.close();//关闭流

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

用Java读取TXT文件和存入TXT文件的代码

/****************

* 文件读取与保存

*

* @author Administrator

*

****************/

public class FileOption {

/**

* 根据路径读取文件

*

* @param readPath

* 读取文件的路径

* @return

* @throws Exception

*/

public String readFile(String readPath) throws Exception {

return readFile(new File(readPath));

}

/**

* 读取文件

*

* @param file

* @return

* @throws Exception

*/

public String readFile(File file) throws Exception {

BufferedReader br = new BufferedReader(new FileReader(file));

StringBuffer sbf = new StringBuffer("");

String line = null;

while ((line = br.readLine()) != null) {

sbf.append(line).append("\r\n");// 按行读取,追加换行\r\n

}

br.close();

return sbf.toString();

}

/**

* 写入文件

*

* @param str

* 要保存的内容

* @param savePath

* 保存的文件路径

* @throws Exception

* 找不到路径

*/

public void writeFile(String str, String savePath) throws Exception {

BufferedWriter bw = new BufferedWriter(new FileWriter(savePath));

bw.write(str);

bw.close();

}

public static void main(String[] args) {

FileOption fop = new FileOption();

String filePath = "d:/derby.log";

String str = null;

try {

str = fop.readFile(filePath);

System.out.println(str);

} catch (Exception e) {

System.out.println("文件不存在");

}

String savePath = "d:/derby.log.bak";// 将上一个读取的文件另存一份

try {

fop.writeFile(str, savePath);

} catch (Exception e) {

System.out.println("保存文件失败(路径错误)");

}

}

}


当前名称:java保存文件代码,java 存储文件
分享网址:http://cdkjz.cn/article/hoccss.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220