网站建设资讯

NEWS

网站建设资讯

文件对象存储java代码 文件对象存储java代码

用java编写用文件保存学生对象,并且可以添加,删除,查找学生信息

保存,使用java.io.ObjectOutputStream + java.io.FileOutputStream,,,,,,记得要先实现java.io.Serializable

我们一直强调成都做网站、网站建设对于企业的重要性,如果您也觉得重要,那么就需要我们慎重对待,选择一个安全靠谱的网站建设公司,企业网站我们建议是要么不做,要么就做好,让网站能真正成为企业发展过程中的有力推手。专业网站建设公司不一定是大公司,创新互联作为专业的网络公司选择我们就是放心。

读则是使用java.io.ObjectInputstream和java.io.FileInputStream

Java (文件选择器) 保存文件

JFileChooser fd=new JFileChooser();

fd.showSaveDialog(null);

File f=fd.getSelectedFile();

System.out.println(f.getName());

疑惑解答:

1. JFileChooser.getSelectedFile()返回一个文件对象,调这个文件对象的getName()很容易得到用户输入的文件名。返回文件对象既包含了文件路径也包含了文件名,这也体现了Java面向对象的思想。

2. Java的文件对象在文件系统中是可以存在,也可不存在的,所以Java的文件对象有exists()、

createNewFile()、mkdir()等方法。所以文件保存对话框返回的文件对象不一定在文件系统实际存在,而仅仅是一串路径的表示。

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中如何通过txt文件存储和取出数据?

Java中读取txt文件可以使用file类先创建一个对象,然后使用I/O操作,进行读取或者写入操作,示例如下:

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

public class demo2 {

private static String path = "f:/demo1.txt";

private static File file;

static{

file = new File(path);

if(!file.exists()){

try {

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

}

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

Student stu = new Student(1,"张三",90);

writeDataToFile(file,stu);

readDataFromFile(file);

}

private static void readDataFromFile(File file) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

String str = "";

while((str = reader.readLine())!=null){

String[] stuInfo = str.split(",");

System.out.println("学号:"+stuInfo[0]+" 姓名:"+stuInfo[1]+" score:"+stuInfo[2]);

}

}

private static void writeDataToFile(File file,Student stu) throws FileNotFoundException {

PrintWriter out = new PrintWriter(new FileOutputStream(file, true));

out.println(stu.toString());

out.close();

}

}


网站题目:文件对象存储java代码 文件对象存储java代码
文章位置:http://cdweb.net/article/ddehpeg.html