Studyon Minte9.com
Head First (Java)




I/O



	// File: write, read, append (FileWriter, FileReader)
	// Directory (file.mkdir)
	// Serialize (Game.ser)
	// Stream

*  Write File
	
	Sometimes you need to save data to a plain old text file.

	%java

		package testingapp;
		import java.io.*;

		public class TestingApp {
		    public static void main(String[] args) {
			
			try {
			    FileWriter writer = new FileWriter("Foo.txt");
				// if file not exists, it will be created by FileWriter
			    
			    writer.write("hello foo!");
			    writer.close();
			} catch (Exception ex) {
			
			}
		    }
		}


*  Read File
	
	%java

		package testingapp;
		import java.io.*;

		public class TestingApp {
		    public static void main(String[] args) {
			
			try {
			    BufferedReader reader = new BufferedReader(new FileReader("Foo.txt"));
				// Foo.txt contains 2 lines:
				// hello foo
				// hello foo2
			    
			    String line = null;
			    while((line = reader.readLine()) != null) {
				System.out.println(line);
			    }
			    reader.close();
			} catch (Exception ex) {
			
			}
		    }
		}


* Append File

	%java

		package testingapp;
		import java.io.*;

		public class TestingApp {
		    public static void main(String[] args) {

			try {
			    BufferedWriter bw = new BufferedWriter(new FileWriter("Foo.txt", true));
			    bw.write("\n" + "hello foo3!");
			    bw.close();
			} catch (Exception ex) {

			}
		    }
		}

* Directory

	%java

		package testingapp;
		import java.io.*;

		public class TestingApp {
		    public static void main(String[] args) {

			try {
				File dir = new File("category");
				dir.mkdir();
			} catch (Exception ex) {

			}
		    }
		}

* Serialization

	The Serializable interface is known as a marker or tag interface, because the interface doesn't have 
	any methods to implement. Its 	sole purpose is to announce that the class implementing it is 
	serializable.

	%java

		package testingapp;
		import java.io.*;

		public class TestingApp {
		    public static void main(String[] args) {
			
			// Create objects
			GameCharacter c1 = new GameCharacter ("Elf", new String[] {"bow", "sword"});
			GameCharacter c2 = new GameCharacter ("Troll", new String[] {"bare hands", "big ax"});
			    // The objects must be serializable (implements Serializable)
			
			// Serialize objects
			try {
			       ObjectOutputStream os =
					new ObjectOutputStream(new FileOutputStream("Game.ser")); 
			       os.writeObject(c1);
			       os.writeObject(c2);
			       os.close();
			} catch (Exception ex) {
			      ex.printStackTrace();
			}

			// Deserialize objcets
			try {
			    ObjectInputStream is = new ObjectInputStream(new FileInputStream("Game.ser"));
			    GameCharacter c1Restore = (GameCharacter) is.readObject();
			    GameCharacter c2Restore = (GameCharacter) is.readObject();
			    
			    System.out.println("C1 is type: " + c1Restore.getType());
			    System.out.println("C1 weapons are: " + c1Restore.getWeapons());
			} catch (Exception ex) {
			      ex.printStackTrace();
			}
		    }
		}

		class GameCharacter implements Serializable {
		    String type;
		    String[] weapons;
		    
		    transient String GameContext;
			// User transient when an instant variable can't be serialized:
			// the creator forget to make it serializable, or other reasons 
			
		    
		    public GameCharacter(String t, String[] w) {
			type = t;
			weapons = w;
		    }
		    
		    public String getType() {
			return type;
		    }
		    
		    public String getWeapons() {
			String weaponList = "";
			for (int i=0; i<weapons.length; i++) {
			    weaponList += weapons[i] + ", ";
			}
			return weaponList;
		    }
		}


* Stream

	%java	
		
		package testingapp;
		import java.io.*;

		public class TestingApp {
		    public static void main(String[] args) {
			
			try {
			    InputStreamReader reader = new InputStreamReader(System.in);
			    BufferedReader br = new BufferedReader(reader);
			    while(true) {
				String line = (String) br.readLine();
				if (line.equals("quit")) {
				    break;
				}
			    }
			} catch (Exception e) {
			    e.getStackTrace();
			}
		    }
		}