问题描述
我希望在TXT文件中以人类可读的形式编写对象,该文件被保存为一个普通的带有不需要字符的序列化对象。
请教我如何重写相同的程序保存到人类可读的TXT文件中。
类书实现Serializable
{
字符串名;
字符串作者;
int nop;
诠释价格;
int折扣;
getDiscount()
{
int finalprice = price - ((price / discount));
System.out.println(折扣后的最终价格=+最终价格);
$ b $ public String toString()
{
return name + author + nop + price + discount;
$ b class fileio
{
public static void main(String args [])
{
MainClass mainObject = new MainClass();
mainObject.writeToFile();
book javabook = new book();
javabook.name =Java unleashed;
javabook.author =someone;
javabook.nop = 1032;
javabook.price = 450;
javabook.discount = 10;
javabook.getDiscount();
public void writeToFile()
{
try
{
File file = new File(JavaBook1.txt);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(book.toString());
bw.close();
catch(Exception e)
{
e.printStackTrace();
$ b
看看下面是否解决了你的目的
重写Object类的toString()方法来返回你的对象的状态,然后把它的输出写入用文件编写器创建文本文件
如果您希望拥有xml类型的表示形式,可以使用JAXB方法
更新:
在下面的程序中,请忽略语法/编译错误,因为我没有测试它,但它会给你一个简单的想法
class book implements Serializable
{
String name;
字符串作者;
int nop;
诠释价格;
int折扣;
getDiscount()
{
int finalprice = price - ((price / discount));
System.out.println(折扣后的最终价格=+最终价格);
public String toString(){
return name + author + nop + price + discount;
//上面可以是任何你想要的格式
$ b}
}
现在在你的主类中
$ b $ pre $ code import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
$ b class fileio
{
public static void main(String args [])
{
fileio mainObject = new fileio();
book javabook = new book();
javabook.name =Java unleashed;
javabook.author =someone;
javabook.nop = 1032;
javabook.price = 450;
javabook.discount = 10;
javabook.getDiscount();
mainObject.writeToFile(javabook);
public void writeToFile(book bookbook)
{
try
{
File file = new File(JavaBook1.txt);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(javabook.toString());
bw.close();
catch(Exception e)
{
e.printStackTrace();
}
}
}
I want to write objects in human readable form in a TXT file , the file gets saved as a normal serialized object with unwanted characters.
Please suggest me how to rewrite the same program for saving into human readable TXT file
import java.io.*;
class book implements Serializable
{
String name;
String author;
int nop;
int price;
int discount;
void getDiscount()
{
int finalprice=price-((price/discount));
System.out.println("Final price after discount="+finalprice);
}
public String toString()
{
return name+author+nop+price+discount;
}
}
class fileio
{
public static void main(String args[])
{
MainClass mainObject=new MainClass();
mainObject.writeToFile();
book javabook=new book();
javabook.name="Java unleashed";
javabook.author="someone";
javabook.nop=1032;
javabook.price=450;
javabook.discount=10;
javabook.getDiscount();
}
public void writeToFile()
{
try
{
File file=new File("JavaBook1.txt");
FileWriter fw=new FileWriter(file.getAbsoluteFile());
BufferedWriter bw=new BufferedWriter(fw);
bw.write(book.toString());
bw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
See if below solves your purpose
override toString() method of Object class to return your object's state and then write the output of it to text file with file writer
If you want to have xml kind of representatio, go for JAXB approach
Update:-
please ignore syntax/compile errors in below program as i have not tested it but it will give you brief idea
class book implements Serializable
{
String name;
String author;
int nop;
int price;
int discount;
void getDiscount()
{
int finalprice=price-((price/discount));
System.out.println("Final price after discount="+finalprice);
}
public String toString() {
return name + author +nop + price +discount;
// above can be any format whatever way you want
}
}
Now in your main class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
class fileio
{
public static void main(String args[])
{
fileio mainObject=new fileio();
book javabook=new book();
javabook.name="Java unleashed";
javabook.author="someone";
javabook.nop=1032;
javabook.price=450;
javabook.discount=10;
javabook.getDiscount();
mainObject.writeToFile(javabook);
}
public void writeToFile(book javabook)
{
try
{
File file=new File("JavaBook1.txt");
FileWriter fw=new FileWriter(file.getAbsoluteFile());
BufferedWriter bw=new BufferedWriter(fw);
bw.write(javabook.toString());
bw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这篇关于如何将Serialized Object写成人类可读的Txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!