如何将对象编写为人类可读的文本文件 [英] How to write Object as human readable text file

查看:79
本文介绍了如何将对象编写为人类可读的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以人类可读的形式在文本文件中写入对象,该文件将另存为带有不需要字符的普通序列化对象.

I want to write objects in human readable form in a text file, the file gets saved as a normal serialized object with unwanted characters instead.

如何重写程序以保存到人类可读的文本文件中?

How do I rewrite the program for saving into human readable text 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

重写Object类的toString()方法以返回对象的状态,然后使用文件编写器将其输出写入文本文件

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

如果要使用xml形式的表示,请采用JAXB方法

If you want to have xml kind of representatio, go for JAXB approach

更新:-

请忽略以下程序中的语法/编译错误,因为我尚未对其进行测试,但这会给您简要的想法

please ignore syntax/compile errors in below program as i have not tested it but it will give you brief idea

class Book
{
    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


    }
}

现在是您的主班

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();
        }
    }
}

这篇关于如何将对象编写为人类可读的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆