Java Tutorial for Beginners - 47 - Writing out to a file

preview_player
Показать описание
In this tutorial we will take a look at writing out to a file.
Рекомендации по теме
Комментарии
Автор

Another great video, you need some more subscribers for all this work you do!

drewmarsh
Автор

Source:

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.IOException;

// JaSerialization you can stream your Objcte to a sequence of bytes
// anandd then restore these objects from this stream of bytes.  To make a
// jaobject serializable you need to implement the java.io.Serializable interface
// this is only a Marker intereface that tells the JVM or java platform that the object is seriazable.

public class Character implements Serializable {
// your class must implement this interface if they are to be serialized .. marker interface
// a marker interface is a SPECIAL interface that is used as a tag to the java compiler
// so that it can add special behavior to the class implementing it - they HAVE NO METHODS
    
String charName;

public void assignName (String c) {
    
charName = c;  
    
}
 
    public static void main(String[] args) {
        
        Character charObj = new Character ();
        
        charObj.assignName("Walt");
     
        try {
            
           FileOutputStream fileOut = new //create new file output stream
           
           ObjectOutputStream objOut = new ObjectOutputStream (fileOut); 
           // prepares object to be written This class enables you to write java 
           // objects  ... now the object can be read
           
           objOut.writeObject(charObj); // write the object and its values
           
           objOut.flush(); // flush content in underlying streams ... streams buffer some output
           //data in memory the JVM - this will caus an immediate write - so if program crashes data
           //won't be lost ... immedialy write to destination
           
           objOut.close(); // closes and releases system resources from stream
                       
        } catch (IOException ioe) {
            
           System.out.println("An error occurred"); // if an error occurs
     
        }
    }
}

EJMedia
Автор

Good job man, I hope you will continue with java series! 

pantoner
Автор

Guys, if you keep getting an error, change the file directory in FileOutputStream to your desktop

Worked for me. Writing to C drive directly can throw up a permissions exception.

AppleMaster
Автор

this guy is amazing one day his chanel will explode :))

LifeSuxTR
Автор

Great tutorials can you do more on GUI

channelsof
Автор

I'm doing this with Java Eclipse and it seems some thing is not compatible.. "An error occurred" message keeps popping up where 'public class Writefile implements Serializable' saying "The serializable class Writefile does not declare a static final serialVersionUID field of type
long"

dongwookkim
Автор

where is the source code i.e all the code that you put write here

ammarsadeq
Автор

So basically we are making a binary file with a variable assigned with a value "Walt". Then we proceed to do the 5 steps thing:
1)FileOutputStream - to tell the computer where to save the file.
2)ObjectOutputStream- prepare the object ?
3)objOut.writeObject(charObj) - write the content
4)objOut.flush() - write the content now ?
5)objOut.close()- closes & frees system resources.
Correct me if i'm wrong about my first line but i don't understand what the step 2 and step 4 really does. What does it mean by preparing an object and also do the content now?

MrAirPork