保存,使用java.io.ObjectOutputStream + java.io.FileOutputStream,,,,,,記得要先實(shí)現(xiàn)java.io.Serializable
我們一直強(qiáng)調(diào)成都做網(wǎng)站、網(wǎng)站建設(shè)對(duì)于企業(yè)的重要性,如果您也覺得重要,那么就需要我們慎重對(duì)待,選擇一個(gè)安全靠譜的網(wǎng)站建設(shè)公司,企業(yè)網(wǎng)站我們建議是要么不做,要么就做好,讓網(wǎng)站能真正成為企業(yè)發(fā)展過程中的有力推手。專業(yè)網(wǎng)站建設(shè)公司不一定是大公司,創(chuàng)新互聯(lián)作為專業(yè)的網(wǎng)絡(luò)公司選擇我們就是放心。
讀則是使用java.io.ObjectInputstream和java.io.FileInputStream
JFileChooser fd=new JFileChooser();
fd.showSaveDialog(null);
File f=fd.getSelectedFile();
System.out.println(f.getName());
疑惑解答:
1. JFileChooser.getSelectedFile()返回一個(gè)文件對(duì)象,調(diào)這個(gè)文件對(duì)象的getName()很容易得到用戶輸入的文件名。返回文件對(duì)象既包含了文件路徑也包含了文件名,這也體現(xiàn)了Java面向?qū)ο蟮乃枷搿?/p>
2. Java的文件對(duì)象在文件系統(tǒng)中是可以存在,也可不存在的,所以Java的文件對(duì)象有exists()、
createNewFile()、mkdir()等方法。所以文件保存對(duì)話框返回的文件對(duì)象不一定在文件系統(tǒng)實(shí)際存在,而僅僅是一串路徑的表示。
對(duì)于一些小文件,我們可以一次性讀取它的所有字節(jié),然后一次提交到數(shù)據(jù)庫(kù)
///
/// 這個(gè)方法演示了如何一次提交所有的字節(jié)。這樣導(dǎo)致的結(jié)果是:應(yīng)用程序立即需要申請(qǐng)等同于文件大小的內(nèi)存
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();
}
}
}
但是,上面的方法有幾個(gè)問題,主要體現(xiàn)在如果文件比較大的話
它需要一次性很大的內(nèi)存,具體數(shù)據(jù)等同于文件大小。因?yàn)镕ile.ReadAllBytes方法是將所有字節(jié)全部讀入到內(nèi)存。
它會(huì)導(dǎo)致提交失敗,就是因?yàn)閿?shù)據(jù)太大了。數(shù)據(jù)庫(kù)也會(huì)拒絕。
那么,我就對(duì)這個(gè)方法做了一下改進(jìn),將文件拆分為5MB一段,也就是說,此時(shí)每次申請(qǐng)的內(nèi)存只有5MB。這就大大地提高了可用性。
/// 這個(gè)方法是將文件切分為5MB的塊,每次只是提交5MB,所以可能多次提交,但內(nèi)存占用就比較小
static void SubmitFileStepByStep() {
string file = @"F:\功夫熊貓.rmvb";//以這個(gè)文件為例,大小為519MB,一共需要的時(shí)間大約94秒。還是有點(diǎn)慢的,所以還可能需要進(jìn)行壓縮
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();
}
}
這樣的話,有一個(gè)后果就是一個(gè)文件,可能在數(shù)據(jù)庫(kù)中會(huì)有多條記錄。所以在讀取的時(shí)候,我們需要對(duì)其進(jìn)行合并
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文件可以使用file類先創(chuàng)建一個(gè)對(duì)象,然后使用I/O操作,進(jìn)行讀取或者寫入操作,示例如下:
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("學(xué)號(hào):"+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();
}
}
網(wǎng)站題目:文件對(duì)象存儲(chǔ)java代碼 文件對(duì)象存儲(chǔ)java代碼
文章來源:http://www.rwnh.cn/article46/ddehpeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站排名、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、搜索引擎優(yōu)化、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)