導航:首頁 > 編程語言 > java對象寫入文件

java對象寫入文件

發布時間:2022-04-26 09:00:32

java將對象寫入大文件產生內存問題

  1. 為Data類設置一個set方法,上述for循環外面,Data data=new Data(),for循環裡面data.setData(i,i)即可,只用一份Data大小的空間就好了,內存不會緊張

  2. 網上查一下看看怎樣修改javaw的內存大小設置值,將其改大一些,不過這也要看你的電腦內存多大啊

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

你問的其實就是Java序列化的問題,這是RMI、分布式應用的基礎。
寫了個例子給你,挺簡單的,如果看不懂的話再補充問題吧。

Test.java
import java.io.*;
public class Test {
public static void main(String args[]) throws Exception{
Person me = new Person();
me.setId(1);
me.setName("haha");
FileOutputStream outstream = new FileOutputStream("library.dat");
ObjectOutputStream out = new ObjectOutputStream(outstream);
out.writeObject(me); //將這個對象寫入流
out.close();

ObjectInputStream in = new ObjectInputStream(new FileInputStream("library.dat"));
Person who = (Person)in.readObject();
System.out.println(who.getName());
in.close();
}
}

class Person implements Serializable{
private String name;
private int id;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

⑶ java中將list對象寫入文件

可以通過「FileOutputStream」創建文件實例,之後過「OutputStreamWriter」流的形式進行存儲list對象,舉例:
OutputStreamWriter pw = null;//定義一個流
pw = new OutputStreamWriter(new FileOutputStream(「D:/test.txt」),"GBK");//確認流的輸出文件和編碼格式,此過程創建了「test.txt」實例
pw.write(list);//將要寫入文件的內容,可以多次write
pw.close();//關閉流
備註:文件流用完之後必須及時通過close方法關閉,否則會一直處於打開狀態,直至程序停止,增加系統負擔。

⑷ java 我想將一個裝了對象的arraylist 寫入文件,讀出文件,請問怎麼寫代碼,改對象的類定義如下

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.io.Serializable;
import java.util.ArrayList;
public class User implements Serializable{
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User(String name, String code) {
super();
this.name = name;
this.code = code;
}
public static void writeUser(ArrayList<User> userList,File file){//file 為寫入的文件
ObjectOutputStream oos;
try {
if(!file.exists())
file.createNewFile();
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
有疑問可追問或者hi

⑸ Java:內存中已經有了一個對象,如何使用ByteArrayOutputStream將該對象寫入文件呢

ByteArrayOutputStream是將內存中的byte數據作為輸出目標,

寫入到文件則需要打開文件流,

內存中的對象要持久化有兩種方式,一種是直接使用對象的序列化介面,該對象必須實現Serializable介面;另外一種自己寫對象序列化介面。


1、ByteArrayOutputStream要寫到文件可以通過writeTo方法,但是這樣顯然太啰嗦了,需要控制flush時機。

2、如果是對象已經實現序列化介面直接使用如下方式

ObjectOutputStreamo=newObjectOutputStream(newFileOutputStream("filename"));
o.writeObject(obj);

⑹ java io 對象寫入了txt文件,怎樣讀取對象

File file = new File("你的文件路徑");
InputStream in = new FileInputStream(file);
...
讀入流以後可以寫到String

⑺ java中如何將一個對象寫入文本中,如現在student創建了一對象,

importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;

{

=-6601903208557464574L;

privateStringid;
privateStringname;

publicStudent(){
super();
}

publicStudent(Stringid,Stringname){
super();
this.id=id;
this.name=name;
}

publicStringgetId(){
returnid;
}

publicvoidsetId(Stringid){
this.id=id;
}

publicStringgetName(){
returnname;
}

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

}

{

publicstaticvoidmain(String[]args)throwsIOException,
ClassNotFoundException{
Studento=newStudent("1001","張三");
FileOutputStreamos=newFileOutputStream("D:\student.data");
ObjectOutputStreamoos=newObjectOutputStream(os);
oos.writeObject(o);
oos.flush();
oos.close();

FileInputStreamis=newFileInputStream("D:\student.data");
ObjectInputStreamois=newObjectInputStream(is);
Studentdeserialized=(Student)ois.readObject();
ois.close();
System.out.println("學號:"+deserialized.getId());
System.out.println("姓名:"+deserialized.getName());
}

}

⑻ JAVA 在監聽器中怎樣寫對象到文件中

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
* Title:NewFrame
* Description:
* Copyright: Copyright (c) 2012-12-11
* Company: ETHERGNS-COM
* @author Guzi
* @version 1.0.0
*/
public class NewFrame extends Frame
{
private static final long serialVersionUID = -7322165277682705641L;
public static String path = "test.txt";

public static void main ( String [] args )
{
NewFrame frame = new NewFrame();
frame.setBounds(500, 500, 100, 100);
Button button = new Button("click");
frame.add(button);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosed ( WindowEvent e )
{
System.exit(-1);
}

});
button.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked ( MouseEvent e )
{
ObjectOutputStream output;
try
{
User user = new User("xx", 22);
output = new ObjectOutputStream(new FileOutputStream(path));
output.writeObject(user);
output.close();
}
catch ( FileNotFoundException e1 )
{
e1.printStackTrace();
}
catch ( IOException e1 )
{
e1.printStackTrace();
}
}
});
}

}
感覺你寫的沒錯啊....我的就可以保存對象......難道是對象不一樣的問題?

閱讀全文

與java對象寫入文件相關的資料

熱點內容
銀河麒麟字體庫存在哪個文件夾 瀏覽:956
魔獸加丁伺服器的航空叫什麼 瀏覽:152
花冠改裝案例哪個app多 瀏覽:515
成績單app哪個好用 瀏覽:140
北美程序員vs國內程序員 瀏覽:181
php解析xml文檔 瀏覽:121
石墨文檔APP怎麼橫屏 瀏覽:185
牆主鋼筋加密和非加密怎麼看 瀏覽:144
金山區文件夾封套定製 瀏覽:708
soho程序員 瀏覽:672
java位元組截取 瀏覽:525
php提交作業 瀏覽:815
房產還沒解壓可以辦理贈予嗎 瀏覽:224
java毫秒轉分鍾 瀏覽:753
模式識別中文pdf 瀏覽:774
c語言平均數字編譯錯誤 瀏覽:171
單片機算交流 瀏覽:45
php自適應網站 瀏覽:467
2b2t伺服器怎麼獲得許可權 瀏覽:816
c語言javaphp 瀏覽:804