Results 1 to 4 of 4
  1. #1
    Ujemny is offline Member
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    8
    Rep Power
    0

    Default Writing object to file and then read it in txt

    Hello :)

    I have problem with writing objects to file. The thing i want to do is write object(Measur) and then read it from Notepad++. Not from program, but normally from computer. When i'm opening my file i see patterns(i dont how to call this). I think, that I need write it as a string in some way.

    There is my code:
    Java Code:
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    /**
     *
     * @author PJ
     */
    class Task3 {
        
        public void WriteMeasur(Measur measur) throws FileNotFoundException, IOException{
            FileOutputStream fout = new FileOutputStream("c:\\zadanie.txt", true);
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            
            oos.writeObject(measur);
        }
        
    }
    
    
    class Time implements Serializable{
        int year, month, day, hour, minute;
        
        Time(int r, int m, int d, int h, int min){
            if(r >= 2014 && r < 1990)
                this.year = r;
            if(m <= 12)
                this.month = m;
            if(d <= 30 || (m == 2 && d < 29))
                this.day = d;
            if(h <= 24)
                this.hour = h;
            if(min <= 60)
                this.minute = min;
        }
    }
    
    class Measur implements Serializable{
        Time time;
        double temp;
                
        Measur(Time czas, double temp){
            this.time = czas;
            this.temp = temp;
                    
        }        
    
    }
    
    
    
    public class Main{
        public static void main(String args[]) throws IOException{
            
            //Argumenty klasy czas: rok, miesiac, dzien, godzina, minuty
            // Metody inicjujace klase czas
            
            Time czas_1 = new Time(2013,7,23,20,59);
            Time czas_2 = new Time(2000, 12, 7, 50, 12);
            
            // Metody inicjujace klase pomiar
            
            Measur pomiar_1 = new Measur(czas_1, 25.5);
            Measur pomiar_2 = new Measur(czas_2, -3);
            
            
            Task3 zad3 = new Task3();
            
            zad3.WriteMeasur(pomiar_1);
            zad3.WriteMeasur(pomiar_2);
            
        }
    }
    Any advice?

    Thank you a lot for help :)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: Writing object to file and then read it in txt

    If you want to write something to a file that is readable by notepad (or any other text editor) then do not write it as an object. You need to write is out as text. Use something like OutputFileWriter or perhaps PrintWriter to write text to an output file.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Writing object to file and then read it in txt

    Important to remember to close any streams you opened as this will free up resources and more importantly flush any remaining data in the buffer.

  4. #4
    Ujemny is offline Member
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    8
    Rep Power
    0

    Default Re: Writing object to file and then read it in txt

    Thank you a lot guys! :)

    I've made toString() methods and I have used BufferedWriter. Now it works.

    @Junky: Yess. I had problem with that, too. When i closed stream in WriteMeasur i had error: Stream closed, so i put .close() to Main class.

    Thank you. Problem is solved :)

Similar Threads

  1. Replies: 1
    Last Post: 03-02-2012, 02:58 PM
  2. Replies: 14
    Last Post: 07-15-2011, 06:22 AM
  3. Problem: Read and Writing Variables to a txt file!
    By Swiftnsilent in forum New To Java
    Replies: 11
    Last Post: 05-06-2011, 12:40 AM
  4. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  5. Replies: 4
    Last Post: 11-03-2010, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •