filmov
tv
Saving Objects in Java - Serialization (for Kieren)

Показать описание
0:00 Introduction and implementation
13:00 Code cleaning
In this video I briefly explain the process of saving a java object to a file. This is important when preserving data and is the foundation for simple persistent programs.
Do note that this is a very basic introduction to the idea and should be treated as a starting board for more complicated implementations in future.
If you have any questions please comment, message me, or if you're Kieren just let me know.
Code used in the video follows (with the exception of angle brackets, YouTube doesn't like them)
public class Main {
public static void main(String... args) {
Dog rover = new Dog("Rover", 5);
try{
}catch (IOException e){
}
Dog loadedRover = null;
try{
} catch (IOException e) {
} catch (ClassNotFoundException e){
}
// WATCH OUT FOR THIS
}
public static void saveObject(Serializable obj, String filename) throws IOException{
FileOutputStream filesaver = new FileOutputStream(filename);
ObjectOutputStream objectsaver = new ObjectOutputStream(filesaver);
}
// If you copied this from youtube you'll need to put angle brackets around the first 'T'
public static T T loadObject(String filename) throws IOException, ClassNotFoundException{
FileInputStream fileloader = new FileInputStream(filename);
ObjectInputStream objectloader = new ObjectInputStream(fileloader);
return loadedObject;
}
}
/**
* Who's a good boy??
* You are!
*/
public class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
}
public String getName() {
return name;
}
public void setName(String name) {
}
public int getAge() {
return age;
}
public void setAge(int age) {
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
13:00 Code cleaning
In this video I briefly explain the process of saving a java object to a file. This is important when preserving data and is the foundation for simple persistent programs.
Do note that this is a very basic introduction to the idea and should be treated as a starting board for more complicated implementations in future.
If you have any questions please comment, message me, or if you're Kieren just let me know.
Code used in the video follows (with the exception of angle brackets, YouTube doesn't like them)
public class Main {
public static void main(String... args) {
Dog rover = new Dog("Rover", 5);
try{
}catch (IOException e){
}
Dog loadedRover = null;
try{
} catch (IOException e) {
} catch (ClassNotFoundException e){
}
// WATCH OUT FOR THIS
}
public static void saveObject(Serializable obj, String filename) throws IOException{
FileOutputStream filesaver = new FileOutputStream(filename);
ObjectOutputStream objectsaver = new ObjectOutputStream(filesaver);
}
// If you copied this from youtube you'll need to put angle brackets around the first 'T'
public static T T loadObject(String filename) throws IOException, ClassNotFoundException{
FileInputStream fileloader = new FileInputStream(filename);
ObjectInputStream objectloader = new ObjectInputStream(fileloader);
return loadedObject;
}
}
/**
* Who's a good boy??
* You are!
*/
public class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
}
public String getName() {
return name;
}
public void setName(String name) {
}
public int getAge() {
return age;
}
public void setAge(int age) {
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}