「java存file」Java怎么另存为
今天给各位分享java存file的知识,其中也会对Java怎么另存为进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、Java File存入字节最大可以,存入多少
- 2、java如何保存文件
- 3、java怎么保存文件
- 4、JAVA保存文件 File f = new File(directory, filename); 我要如何将这个文件在电脑里生成
Java File存入字节最大可以,存入多少
java在使用字节输入流读取文本文件的时候,可以定义字节缓冲区,来实现高效的读取。如:FileinputStreamfis=newFileinputStream(path);byte[]buffer=newbyte[1024];//一次读取1024个字节然后通过fis.read(buffer);来将读取到的字节数保存在字节缓冲区中。
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怎么保存文件
可以使用java.io.FileOutputStream流保存任意文件或者用java.io.ObjectOutputStream流保存类文件
JAVA保存文件 File f = new File(directory, filename); 我要如何将这个文件在电脑里生成
FileOutputStream fos=null;
try {
if (!f.exists()) {//文件不存在则创建
f.createNewFile();
}
fos=new FileOutputStream(f);
String str="文件内容";
fos.write(str.getBytes());//写入文件内容
fos.flush();
} catch (IOException e) {
System.err.println("文件创建失败");
}finally{
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
System.err.println("文件流关闭失败");
}
}
}
java存file的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java怎么另存为、java存file的信息别忘了在本站进行查找喔。