導航:首頁 > 編程語言 > java文件流轉文件對象

java文件流轉文件對象

發布時間:2021-10-24 16:09:39

java怎麼把數據流轉換成對象

首先,要想把數據流轉換成對象,要先將數據流對應的對象序列化,然後反序列化,出來就是對象了;
協議你不能定,但是你肯定知道報文的格式吧(前10位代表 報文頭A,11位到20位代表報文頭B);
你把buffer里邊的內容按欄位解析出來,然後構造一個對象就行了。

② 請問各位如何將一個文件流轉換成文件對象(不創建本地文件)

Java中文件(File)的概念,其實是"路徑" ,連Thinking in Java上都這么說.

但是流作為資源,不是抽象的概念,必須有其載體的,即一個實體的"文件".

當然你可以把流寫到一個臨時文件,即
File temp = File.createTempFile()里.

FileOutputStream fos = new FileOutputStream(temp);

...

③ java中如何將流轉換成文件類型

我查了一下,File的api 這個File的構造方法的介紹。。。

File(File parent, String child)
根據 parent 抽象路徑名和 child 路徑名字元串創建一個新 File 實例。
File(String pathname)
通過將給定路徑名字元串轉換成抽象路徑名來創建一個新 File 實例。
File(String parent, String child)
根據 parent 路徑名字元串和 child 路徑名字元串創建一個新 File 實例。
File(URI uri)
通過將給定的 file: URI 轉換成一個抽象路徑名來創建一個新的 File 實例。

也就意味著,如果,file這種類型,就必須有一個路徑。
那,能不能在內存中虛擬一個File file呢?
File f = new File("/1.txt");
假如這樣,那麼,一旦,你開始往這個file裡面開流寫內容。只有兩種情況可能發生,一種是找不到文件,拋異常。另外一種可能是,直接create了一個文件出來,並且寫進去這個文件~~

所以,如果是這樣的情況,就很郁悶。

那麼,在user對象中,如果非要放File文件類型格式,那麼,就寫到一個臨時文件里吧。等用完之後刪除。
如果該成byte[] 或者別的內容,如果你要用數據,其實會更加方便,不用開流從文件裡面讀取,而是直接從這個數組裡面讀就是了。

所以,建議把這個User裡面的文件變成byte[] 。

一點淺見~~

另祝節日愉快~~

④ java如何將對象寫入文件

假設創建了5個對象per1---per5
Vector<Person> account = new Vector<Person>();
account.add(per1);
......
account.add(per5);
直接添加即可
Vector 現在用了比較少了
已經被ArrayList替代了
ArrayList效率比較高些

static void test1() throws Exception {
Person stu = new Person(
"張三專",12,"M",60);
// 對象的序列化流
ObjectOutputStream out =
new ObjectOutputStream(
new FileOutputStream("d:/d.dat"));
out.writeObject(stu);
out.flush();
out.close();
}
輸出到文件的話屬用這個就可以了
ObjectOutputStream 這個要導包

⑤ java如何從文件中讀取對象並用set保存

java中從文件中讀取對象,主要是使用ObjectInputStream的readObject方法來讀取對象,如下代碼

()
{
Objecttemp=null;
Filefile=newFile("test.dat");
FileInputStreamin;
try{
in=newFileInputStream(file);
ObjectInputStreamobjIn=newObjectInputStream(in);
temp=objIn.readObject();//從文件當中讀取對象
Set<Object>ixiang=newHashSet<Object>();
ixiang.add(temp);//添加對象到set集合裡面
objIn.close();
System.out.println("readobjectsuccess!");
}catch(IOExceptione){
System.out.println("readobjectfailed");
e.printStackTrace();
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
returntemp;
}

⑥ Java 如何對文件進行多個Object對象流的讀寫操作

思路:把已經序列化的對象存入容器(如LinkedList<?>)中,然後用ObjectInputStream和ObjectOutputStream對這個實例化的LinkedList<?>對象進行讀寫。測試主程序:/** * @Title: FileRW.java * @Package com.file * @Description: 文件、文件夾的創建、寫入練習。讀寫是使用對象流實現。 * @author 慢跑學Android * @date 2011-11-19 下午03:53:01 * @version V1.0 */ package com.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.LinkedList; public class FileRW { private String dirPath; private String filename; public static void main(String[] args) { String path = "C:\\曉聲"; String fileName = "test.txt"; FileRW fileRW = new FileRW(path, fileName); LinkedList<TestMessage> msgOut = new LinkedList<TestMessage>(); LinkedList<TestMessage> msgIn = null; msgOut.add(new TestMessage("柯南", "偶像")); msgOut.add(new TestMessage("卡卡西", "好樣的")); msgOut.add(new TestMessage("Android", "Android")); msgOut.add(new TestMessage("哈哈", "測試下喔")); fileRW.writeObject(path, fileName, msgOut); msgIn = fileRW.readObject(path,fileName); for(TestMessage temp:msgIn) { System.out.println(temp.getName() + temp.getData()); } } public FileRW(String dirPath, String filename) { this.dirPath = dirPath; this.filename = filename; if (creatDir()) { creatFile(); } } private boolean creatDir() { if (null != dirPath) { File path = new File(dirPath); if (path.exists()) { return true; } if (true == path.mkdirs() ) { return true; } } return false; } private void creatFile() { if (null != filename) { File file = new File(dirPath, filename); if (false == file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } } /** * @Title: writeObject * @Description: Write a object to a file. * @param path the directory of the target file * @param filename the name of the target file * @param msg the type of the object * @return void * @throws */ private void writeObject(String path, String filename, LinkedList<TestMessage> msg) { File file = new File(path, filename); if (false == file.isFile()) { return ; } try { // The value "false" for FileOutputStream means that overwrite this file, // if it is "true",append the new data to this file. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,false)); oos.writeObject(msg); oos.flush(); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @Title: readObject * @Description: Read a object from a file. * @param path the directory of the target file * @param filename the name of the target file * @return LinkedList<TestMessage> * @throws */ @SuppressWarnings("unchecked") private LinkedList<TestMessage> readObject(String path, String filename) { File file = new File(path, filename); ObjectInputStream ois = null; LinkedList<TestMessage> msgAll = null; try { ois = new ObjectInputStream(new FileInputStream(file)); try { msgAll = (LinkedList<TestMessage>)ois.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } return msgAll; } } 測試程序中的消息包定義:/** * @Title: TestMessage.java * @Package com.file * @Description: FileRW的消息流 * @author 慢跑學Android * @date 2011-11-19 下午04:35:11 * @version V1.0 */ package com.file; public class TestMessage implements java.io.Serializable { private String name; private String data; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getData() { return data; } public void setData(String data) { this.data = data; } public TestMessage(String name, String msg) { this.name = name; data = msg; } } 程序運行結果:參考資料:ObjectInputStream類和ObjectOutputStream類的使用

⑦ java輸出一個對象到文件

先創建一個文件對象:
File file = new File("");
if (!file.exists())
file.createNewFile();
然後創建一個文件輸出流
FileOutputStream fos = new FileOutputStream(file);
然後可以把一個對象用.toString()方法轉換成字元串。
然後再用.getBytes()轉換成字元數組。
byte[] bytes = "".getBytes();
寫入文件:
fos.write(bytes);

⑧ java從文件讀取對象

可以使用 ObjectOutputStream 將對象寫入文件中,使用 ObjectInputStream 類從文件中讀取出對象。

代碼如下:

importjava.io.EOFException;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;
importjava.util.ArrayList;
importjava.util.List;

{

=-3527230407404080537L;

privateStringname;

privateintage;

publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}

@Override
publicStringtoString(){
return"Person[name="+name+",age="+age+"]";
}
}

publicclassApp34{

publicstaticvoidmain(String[]args)throwsIOException,ClassNotFoundException{

List<Person>persons=newArrayList<>();

persons.add(newPerson("張三",20));
persons.add(newPerson("李四",25));

Filefile=newFile("persons.data");

//創建文件輸出流
FileOutputStreamoutput=newFileOutputStream(file);

//創建對象輸出流
ObjectOutputStreamoOutput=newObjectOutputStream(output);

//將整個數組列表輸出至文件
oOutput.writeObject(persons);

//或者逐個將對象輸出至文件,讀取時需要逐個讀取
/*for(Personp:persons){
oOutput.writeObject(p);
}*/

oOutput.close();
output.close();



//創建文件輸入流
FileInputStreaminput=newFileInputStream(file);

//創建對象輸入流
ObjectInputStreamoInput=newObjectInputStream(input);

//讀出整個對象列表
List<Person>persons2=(List<Person>)oInput.readObject();

for(Personp:persons2){
System.out.println(p);
}


//或者逐個讀取對象
/*Personp1=null;
try{
while((p1=(Person)oInput.readObject())!=null){
System.out.println(p1);
}
}catch(EOFExceptione){
System.out.println("讀取對象結束");
}*/

oInput.close();
input.close();
}
}

⑨ JAVA能否把一個對象寫入文件(請高手指點)

可以,使用對象序列化;可以以流的方式寫入文件,讀取是自動還原為對象;

⑩ 怎麼用JAVA文件流讀取文件然後保存到對象中

應用public ObjectInputStream(InputStream in)構造一個ObjectionInputStream對象ois,再應用ois的版public final Object readObject()讀取一個Object對象強制權轉換為你對象。
ObjectionInputStream ois=new ObjectionInputStream(in);
Object o=ois.readeObject();

閱讀全文

與java文件流轉文件對象相關的資料

熱點內容
胡八一盜墓電影全部系列 瀏覽:635
百度雲文件庫更新 瀏覽:313
es文件管理卸載系統軟體 瀏覽:637
國語韓國電影 瀏覽:323
台灣古裝四級電影 瀏覽:403
頸子上長睾丸的電影 瀏覽:453
尺度大les影片 瀏覽:430
主角血親全收的小說 瀏覽:957
槍火粵語電影百度雲 瀏覽:42
周星馳的全部電影粵語 瀏覽:423
歐姆龍plc編程線驅動程序 瀏覽:46
重生紅軍反圍剿的小說 瀏覽:142
主角獲得外星戰艦認主 瀏覽:401
免費能搜索的在線看片 瀏覽:584
韓劇電影在線觀看國語 瀏覽:772
win10系統去廣告嗎 瀏覽:900
無法打開物理文件 瀏覽:487
jar啟用指定配置文件 瀏覽:994
蘋果手機用什麼app拍美顏照片 瀏覽:595
蘇州網路公關公司有哪些比較好的 瀏覽:26

友情鏈接