הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

IO Lesson Examples

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
 
public class SaveRestore {
	public static void main(String [] args) throws IOException {
		File f = new File("state");
		if ( f.exists() ) {
			FileInputStream fin = new FileInputStream(f);
			ObjectInputStream oin = new ObjectInputStream(fin);
			int pastValue = oin.readInt();
			System.out.println("Current state: " + pastValue);
			oin.close();
		} else {
			System.out.println("Welcome, Earthling");
			int x = (int) (Math.random() * 10);
			System.out.println("You got " + x);
 
			FileOutputStream fout = new FileOutputStream(f);
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			oout.writeInt(x);
			oout.close();
		}
	}
}
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
 
public class Main {
	public static void main(String [] args) throws IOException, ClassNotFoundException {
		Car c1 = new Car("Yello", 1974);
		Car c2 = new Car("Black", 1904);
 
		FileOutputStream fout = new FileOutputStream("cars");
		ObjectOutputStream cout = new ObjectOutputStream(fout);
		cout.writeObject(c1);
		cout.writeObject(c2);
		cout.close();
 
		FileInputStream fin = new FileInputStream("cars");
		ObjectInputStream cin = new ObjectInputStream(fin);
		Car d1 = (Car) cin.readObject();
		Car d2 = (Car) cin.readObject();
 
		System.out.println("Got them back !" + d1.getColor());
	}
}	
 
import java.io.Serializable;
 
 
public final class Car implements Serializable {
	private String color;
	private int    year;
 
	public Car(String color, int year) {
		this.color = color;
		this.year = year;
	}
 
	public String getColor() {
		return this.color;
	}
 
	public int getYear() {
		return this.year;
	}
 
	public void paint(String newColor) {
		this.color = newColor;
	}
}
course: