当前位置:首页 » 服务存储 » java文件临时存储
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

java文件临时存储

发布时间: 2022-05-03 10:36:56

‘壹’ java怎么保存文件

可以使用java.io.FileOutputStream流保存任意文件或者用java.io.ObjectOutputStream流保存类文件

‘贰’ java 中有关对象临时存储的问题

编写一个类,用来存储临时的对象,并编写必要的方法来处理你的要求,这样能很好控制和操作

‘叁’ 如何将JAVA程序中的数据暂时性保存在内存中,使得在下次启动程序时可以直接调用

根据我对Java编程的理解,这个是做不到的,如果你说的内存是指运行内存:因为Java具有垃圾回收机制,程序退出之后,使用的所有内存全部都将被释放。
事实上可以通过把链表中的数据保存在文件中,来实现你的需求。只是变换了实现方式。你可以查阅Java 文件读写方面的资料。

‘肆’ Java临时文件放在哪里

搂住不用想了
页面中的java代码是动态程序,你下下来的页面已经是经过编译处理后的静态页面了。

‘伍’ java临时存取数据

敢呀临时存取数据,如果涉及大数据修改完成后还会再次修改已做过更新的数据的话,那么这就必须是使用数据库技术,基于此点考虑。在设计构架时候,需要有两个数据库,和计划任务

使用spring定时器,定时在10点执行数据库同步。
具体同步方式,可在目标数据库编写存储过程,这样spring定时器 触发存储过程,完成数据库同步。在此过程中,可以建立一张表来存放,具体修改数据的id

‘陆’ java程序中怎样用文件存储数据

对于一些小文件,我们可以一次性读取它的所有字节,然后一次提交到数据库
///
/// 这个方法演示了如何一次提交所有的字节。这样导致的结果是:应用程序立即需要申请等同于文件大小的内存
static void SubmitFileByOnce() {
string file = @"F:\功夫熊猫.rmvb";//文件大小为519MB
byte[] buffer = File.ReadAllBytes(file);
using (SqlConnection conn = new SqlConnection("server=(local);database=demo;integrated security=true")) {
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "INSERT INTO Files(FileName,FileContents) VALUES(@fileName,@fileContents)";
cmd.Parameters.AddRange(
new[]
{
new SqlParameter("@fileName",file),
new SqlParameter("@fileContents",buffer)
});
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}

但是,上面的方法有几个问题,主要体现在如果文件比较大的话
它需要一次性很大的内存,具体数据等同于文件大小。因为File.ReadAllBytes方法是将所有字节全部读入到内存。
它会导致提交失败,就是因为数据太大了。数据库也会拒绝。
那么,我就对这个方法做了一下改进,将文件拆分为5MB一段,也就是说,此时每次申请的内存只有5MB。这就大大地提高了可用性。
/// 这个方法是将文件切分为5MB的块,每次只是提交5MB,所以可能多次提交,但内存占用就比较小
static void SubmitFileStepByStep() {
string file = @"F:\功夫熊猫.rmvb";//以这个文件为例,大小为519MB,一共需要的时间大约94秒。还是有点慢的,所以还可能需要进行压缩
FileStream fs = new FileStream(file, FileMode.Open);

byte[] buffer = new byte[5 * 1024 * 1024];
int readCount;
using (SqlConnection conn = new SqlConnection("server=(local);database=demo;integrated security=true"))
{
conn.Open();

while ((readCount = fs.Read(buffer, 0, buffer.Length)) > 0)
{

using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "INSERT INTO Files(FileName,FileContents) VALUES(@fileName,@fileContents)";
cmd.Parameters.AddRange(
new[]
{
new SqlParameter("@fileName",file),
new SqlParameter("@fileContents",buffer)
});

cmd.ExecuteNonQuery();
}

}
conn.Close();

}
}

这样的话,有一个后果就是一个文件,可能在数据库中会有多条记录。所以在读取的时候,我们需要对其进行合并
static void DownloadFile() {
string file = @"F:\功夫熊猫.rmvb";
string destfile = @"E:\Temp\Temp.wmv";
using (SqlConnection conn = new SqlConnection("server=(local);database=demo;integrated security=true"))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT FileContents FROM Files WHERE FileName=@fileName";
cmd.Parameters.AddRange(
new[]
{
new SqlParameter("@fileName",file),
});
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
FileStream fs = new FileStream(destfile, FileMode.Append, FileAccess.Write);

while (reader.Read())
{
byte[] buffer = (byte[])reader[0];
fs.Write(buffer, 0, buffer.Length);
}
fs.Close();
reader.Close();
conn.Close();
}
}
}

‘柒’ java缓存是什么意思 文件放在哪

Java的缓存机制是通过JVM(Java虚拟机)提供的运行时缓存来实现的,由于JVM是不跨平台的(Java的跨平台正是通过JVM的不跨平台来实现的),所以JVM的缓存机制没有实现本地临时存储,因此你找不到所谓Java的缓存文件夹。这些问题你不用这么纠结,实在不清楚的话打电话问一下官方人员就清楚了。

‘捌’ 怎么用java储存文件(save file)

java.io底下有很多类可以写文件,FileOutStream,RandomAccessFile之类的都行。

‘玖’ 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如何在文件中间写入而不破坏原有内容

你的需求是不现实的,只使用写操作必然新写入的数据会覆盖原来位置的数据。一般的做法都是 边读取,边组合,最后写入到源文件,如果文件很大的话便使用一个临时文件来进行临时存储。