Java Serializable interface: Reading and writing Objects to a file Tutorial

Поделиться
HTML-код
  • Опубликовано: 28 янв 2025

Комментарии • 74

  • @Eletrobot-j5
    @Eletrobot-j5 12 лет назад

    thanks man , good lesson about serializable i'm brazilian java student
    and your learning method is the best!

  • @pewpewpew2432
    @pewpewpew2432 7 лет назад +1

    Bro Good job on pinpointing some of the errors im glad i got to see this video it helps me a lot thank you

  • @danahardziychuk9099
    @danahardziychuk9099 11 лет назад +1

    Good day! Jose, i have created both classes en run them in sap netweaver dev studio, also in eclipse - but still the folder "doc" where the document is supposed to be generated, is not there. what could be a reason? thanks

  • @YoussefElkhayami
    @YoussefElkhayami 11 лет назад

    Great tutorial, this is going to help me on my exam. Thanks!

  • @josemvidal
    @josemvidal  12 лет назад

    Yes you can, just make sure you read them back in the correct (inverse from writing) order. However, generally people just create one Container object and read-write that. It keeps the code simpler.

  • @raulzuniga4382
    @raulzuniga4382 9 лет назад

    Thank you so much for doing this video! It has been very helpful to me.

  • @Shonos47
    @Shonos47 11 лет назад +2

    I've tried this but when my program is reading the 2nd set of objects that I serialized, it encounters a StreamCorruptedException

  • @knutandersstokke1405
    @knutandersstokke1405 9 лет назад +8

    I just finished watching Hitchikers Guide to the Galaxy and came straight to this tutorial where you wrote Arthur Dent.. I'm just so confused right now.

    • @a1vin525
      @a1vin525 6 лет назад

      Google is watching you...

  • @tjabercrombie
    @tjabercrombie 12 лет назад +1

    Thank you for helping me pass my final

  • @josiahls4859
    @josiahls4859 9 лет назад

    Can I use this to show only object of certain attributes? Like if I have several object Task that are due tomorrow while others are due next week, can I retrieve only the ones due tomorrow? Ive been trying to figure out how to store objects and organize them for a whole week now but I am not sure if serializing is the best route.

  • @nkosinathisola3713
    @nkosinathisola3713 5 лет назад

    Great tutorial. How can I read read the values from the txt file and create serializable objects which must be written to a serialized file ???? Please help

  • @7thAttempt
    @7thAttempt 12 лет назад

    Can you serialize more than on object in to a file and read more than one out of a file?

  • @brucelamb6060
    @brucelamb6060 3 года назад

    I know best practice depends on structure and mission of program but...
    Is it a strong practice to have a protected/private constructor whos only purpose is to read data from a "save" file for security? Perhaps the only parameter of this is the file itself, if file not found then construct with a public constructor?

  • @chamodyadayan6471
    @chamodyadayan6471 11 лет назад

    Is there a way to append the objects when writing to the file ?

  • @fahadalkamli
    @fahadalkamli 9 лет назад

    Thank you for an awesome explanation, just one question if i may , you mentioned that you have to know the type of the object before you cast it , is it possible to use instanceOf on an object to find out it's type?

  • @josemvidal
    @josemvidal  11 лет назад

    Yes. The FileOutputStream constructor takes a second argument that, if true, then it appends to the existing file.
    That is 'fos = new FileOutputStream(file, true);' will append to 'file'.

  • @krishnavishwakarma908
    @krishnavishwakarma908 4 года назад

    Sir, how can I read multiple objects if I have written more than 1 one object in that file.?

  • @natanahel09
    @natanahel09 7 лет назад

    Sos el Jose! muchas gracias bombon!

  • @bernardoclaro4499
    @bernardoclaro4499 3 года назад

    Good video, thanks !

  • @ssbarakath
    @ssbarakath 11 лет назад

    Hi, when you sysout, eclipse automatically fills up the rest for u.. System.out.println()
    How did you get this done? Thanks

    • @josemvidal
      @josemvidal  11 лет назад

      See my "Eclipse tips and tricks" video Eclipse Tips and Tricks for Beginners Tutorial

    • @alexmiller1221
      @alexmiller1221 11 лет назад

      you just have to type syso and press ctrl + space..

    • @ssbarakath
      @ssbarakath 11 лет назад

      Thank you Alex

  • @ssbarakath
    @ssbarakath 11 лет назад

    Hi Mr. Jose, when you type 'sysout', eclipse automatically fills up the rest for u.. System.out.println()
    How did you get this done? Thanks

  • @BrandonDyer64
    @BrandonDyer64 9 лет назад

    Thanks so much for the tutorial!

  • @7thAttempt
    @7thAttempt 12 лет назад +1

    Thanks for this - great tute :)

  • @joeyerland3145
    @joeyerland3145 10 лет назад

    How do i serialize and deserialize a JSpinner? For a GUI?

  • @BlindIllusionist
    @BlindIllusionist 9 лет назад

    Very helpful. Thank you!

  • @josemvidal
    @josemvidal  11 лет назад

    If you got an error when running the program then that error message should tell you more about the problem.
    If you did not get an error then the file (probably) was written. It might be on a different folder than you expect, because of partial path names.

  • @MyStuff774
    @MyStuff774 12 лет назад

    Thanks, very nice explanation

  • @escobara12
    @escobara12 11 лет назад

    Great....Tks a Lot Jose.....Muchas Gracias

  • @AmanVerma-mg2ym
    @AmanVerma-mg2ym 5 лет назад

    helped a lot bro

  • @fireblade3682
    @fireblade3682 5 лет назад

    És grande, Zé

  • @josemvidal
    @josemvidal  11 лет назад

    Ugh! I don't know. That means there is a problem with the file data. Maybe you added some extra data to the file? sometimes, if you open the file with some text editors they will add some whitespace to it. Or, maybe you are trying to read more objects than you wrote? I don't know.

  • @xadam123
    @xadam123 10 лет назад

    this was very helpful. thank you.

  • @goncalomartins6923
    @goncalomartins6923 8 лет назад

    I just can save one object per file? Lets say i want to create a person called john after the arthur, can i save it in the same file? If so, when it reads, how java knows if he should read arthur or john?

    • @josemvidal
      @josemvidal  8 лет назад

      +Gonçalo Martins Yes, you can write another object after the first, just make sure you append it to the file (not overwrite the existing file). The objects will be stored in the order you wrote them. So, when you go back to read them you will read them in the same order you wrote them.

    • @goncalomartins6923
      @goncalomartins6923 8 лет назад

      Jose Vidal Thank you, it worked :)

    • @goncalomartins6923
      @goncalomartins6923 8 лет назад +2

      You just call it twice.
      The first is for the first object, the second for the second etc...

    • @kingmanzhang
      @kingmanzhang 7 лет назад

      good question and good answers

  • @misterdubsteppa
    @misterdubsteppa 9 лет назад

    It doesnt creates an folder "docs" with the data.bin in it.
    It's just creating the data.bin and Eclipse doesn't show me the file. Why?

    • @goncalomartins6923
      @goncalomartins6923 8 лет назад

      +misterdubsteppa Refresh your project. The folder docs it was already there.

  • @scottallen6125
    @scottallen6125 10 лет назад

    Whats the point of creating objectinput stream when you're just creating a simple line listing the outputs (p.name , p.age) I thought it would return the saved file by just using (fileName))

    • @josemvidal
      @josemvidal  10 лет назад

      Scott Allen So, the point of the video is just to show how to read and write objects to a file. The code itself has no purpose beyond being used as an example in the video. More generally, writing objects to files is very useful when you have an object with many properties (data members). For example, say your object is an instance of MusicLibrary which contains an ArrayList of Songs, each one of which has .title, .artist, etc. You can read/write that whole MusicLibrary to disk with the couple of lines in the video, rather than having to write your own serializer and parser.

  • @rishabhgarg2983
    @rishabhgarg2983 12 лет назад

    hay here you make a data.txt file instead of data.bin and write in character not in binary?????

  • @TMDStudios
    @TMDStudios 5 лет назад

    very helpful, thanks

  • @brianlaudrupchannel
    @brianlaudrupchannel 9 лет назад

    can you save linked lists?

    • @josemvidal
      @josemvidal  9 лет назад

      billyblackburn87 Yes, any Serializable object can be saved.

    • @brianlaudrupchannel
      @brianlaudrupchannel 9 лет назад

      Jose Vidal wow thankyou for the quick reply

  • @iknowyourerightbut6246
    @iknowyourerightbut6246 8 лет назад

    around 9:00 mins, adding 'transient' keyword in front of a variable makes it non-serializable.

  • @jade101ism
    @jade101ism 11 лет назад +15

    The title of the Video says "Objects" but you are only doing 1 object.

    • @andyjiang8988
      @andyjiang8988 6 лет назад +1

      The input object and output object are two different objects. The reading and writing diminished the reference pointers of the two objects. That's why we need to cast the input to Person, and if you try arthur.equals(p), it's false, clearly representing there are two objects.

  • @superdahoho
    @superdahoho 7 лет назад

    thank you my man

  • @sshubhamprasoon8286
    @sshubhamprasoon8286 7 лет назад

    nice explaination but do it for multiple objects

  • @jaimeandres5000
    @jaimeandres5000 5 лет назад

    IntelliJ wasn't adding the ClassNotFoundException for me so watch out for that took me longer than I'm proud to admit to catch that.

  • @alastairbowie
    @alastairbowie 11 лет назад

    Thanks man, helped alot. :)

  • @bob39rocks
    @bob39rocks 12 лет назад

    u can juz make it into a txt file and use scanner and fileWriter

  • @tony_foti
    @tony_foti 11 лет назад

    Thanx a lot really perfect (Y)

  • @akshaychincholkar
    @akshaychincholkar 11 лет назад

    thank you sir.

  • @rominhalltari3785
    @rominhalltari3785 8 лет назад

    Thanks !

  • @goncalomartins6923
    @goncalomartins6923 8 лет назад

    Hello again.
    Im having a problem.
    Im working with the bukkit API, and i need to implement this in my project. But before that i created a random project, so i can test it before. I have a command /dog and when i run it, it will create a new dog (my object) with the name Billy and the age 15 and will save it in a file. When i do /mydog it will return the information. The problem is that, if i make /dog and then i reload(enable and disable the plugin with the onEnable and onDisable) the server, it will clean my file.
    Main - pastebin.com/73PXu91f
    Dog - pastebin.com/P5DmwQGm
    PS: Are you portuguese? If so you can reply in portuguese.

    • @goncalomartins6923
      @goncalomartins6923 8 лет назад

      +Gonçalo Martins Nevermind, bukkit api provides a way of do it ;) I solved it.

  • @lolakapuri
    @lolakapuri 11 лет назад

    thanks!

  • @MGSPakistan
    @MGSPakistan 12 лет назад

    harvey dent?

  • @kurtcassar6372
    @kurtcassar6372 11 лет назад

    thanks!!

  • @80amnesia
    @80amnesia 11 лет назад

    thanks

  • @trevisgamuchirayichipunza9646
    @trevisgamuchirayichipunza9646 9 лет назад

    I still don't get all
    this

    • @goncalomartins6923
      @goncalomartins6923 8 лет назад

      +Trevis Gamuchirayi Chipunza This tutorial wasnt really useful for this example. But when you will have your own objects (classes e.g) stored in maps and lists, this is a great way of saving and loading them.

  • @Bilal.I
    @Bilal.I 2 года назад

    watching this video Im feeling like in 2006

  • @spdrt0202
    @spdrt0202 11 лет назад

    I wish that this tutorial was in spanish =(

  • @brianlaudrupchannel
    @brianlaudrupchannel 9 лет назад

    can you save linked lists?

  • @ddfyn2393
    @ddfyn2393 3 года назад

    Thx