Chapter 7 Input Output Stream

40 388 0
Chapter 7 Input Output Stream

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter Input/Output Stream Outline  Input/Output overview  Writing and reading text files  Object serialization Input/Output streams  A stream is a sequence of bytes that flow from a source to a destination  In a program, we read information from an input stream and write information to an output stream  Package: java.io Standard I/O  There are three standard I/O streams: • standard output – defined by System.out • standard input – defined by System.in • standard error – defined by System.err  We use System.out when we execute println statements  System.in typically represents keyboard input, which we've used many times with Scanner objects  System.out and System.err typically represent a particular window on the monitor screen Exception in I/O  FileNotFoundException is thrown if the file cannot be located (when the FileReader constructor is executed)  Operations performed by some I/O classes may throw an IOException • A file might not exist • Even if the file exists, a program may not be able to find it • The file might not contain the kind of data we expect  An IOException is a checked exception Outline  Input/Output overview  Writing and reading text files  Object serialization Writing text files open a stream write information close the stream FileWriter BufferedWriter Writing text files  The BufferedWriter class: • write method: allows to write a content to a file • newLine method: allows to break a line WriteFile.java import java.io.*; public class WriteFile { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter ("Lop.txt"); BufferedWriter bw = new BufferedWriter (fw); bw.write("TCTH30A - Trung cap tin hoc 30A"); bw.write("TCTH30B - Trung cap tin hoc 30B"); bw.write("CDTH7K - Cao dang tin hoc 7K"); bw.close(); } } WriteFile.java import java.io.*; public class WriteFile { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter ("Lop.txt“); BufferedWriter bw = new BufferedWriter (fw); bw.write("TCTH30A - Trung cap tin hoc 30A\r\n"); bw.write("TCTH30B - Trung cap tin hoc 30B\r\n"); bw.write("CDTH7K - Cao dang tin hoc 7K\r\n"); bw.close(); } } ReadCountryInfo0.java import java.io.*; public class ReadCountryInfo0 { public static void main (String[] args) throws Exception { CountryInfo country = new CountryInfo(); FileInputStream file = new FileInputStream ("countries.dat"); ObjectInputStream inStream = new ObjectInputStream (file); // Deserialize the objects country = (CountryInfo) inStream.readObject(); inStream.close(); // Print the objects System.out.println (country); } } Read an object CountryInfo Output Example programs  Write – Read a CountryInfo object  Write – Read an array of CountryInfo objects by separately  Write – Read an array object  Write – Read an ArrayList WriteCountryInfo.java import java.io.*; public class WriteCountryInfo { public static void main (String[] args) throws IOException { CountryInfo[] countries = new CountryInfo[5]; countries[0] = new CountryInfo ("United States of America", "USA", "Washington, D.C.", 9629091L, 278058900L); countries[1] = new CountryInfo ("Russia", "RUS", "Moscow", 17075200L, 145470200L); countries[2] = new CountryInfo ("Italy", "ITA", "Rome", Write an object separately 301230L, 57679800L); countries[3] = new CountryInfo ("Sweden", "SWE", "Stockholm", 449964L, 8875100L); countries[4] = new CountryInfo ("Poland", "POL", "Warsaw", 312685L, 38633900L); FileOutputStream file = new FileOutputStream ("countries.dat"); ObjectOutputStream outStream = new ObjectOutputStream (file); // Serialize the objects to a file for (int i = 0; i < countries.length; i++) outStream.writeObject (countries[i]); outStream.close(); } } ReadCountryInfo.java import java.io.*; public class ReadCountryInfo { public static void main (String[] args) throws Exception Read an object separately { CountryInfo[] countries = new CountryInfo[5]; //? FileInputStream file = new FileInputStream ("countries.dat"); ObjectInputStream inStream = new ObjectInputStream (file); // Deserialize the objects for (int i = 0; i < countries.length; i++) countries[i] = (CountryInfo) inStream.readObject(); inStream.close(); // Print the objects for (int j = 0; j < countries.length; j++) System.out.println (countries[i]); } } ReadCountryInfo.java import java.io.*; public class ReadCountryInfo { public static void main (String[] args) throws Exception Read an object separately { CountryInfo[] countries = new CountryInfo[5]; //? FileInputStream file = new FileInputStream ("countries.dat"); ObjectInputStream inStream = new ObjectInputStream (file); // Deserialize the objects for (int i = 0; i < countries.length; i++) countries[i] = (CountryInfo) inStream.readObject(); inStream.close(); // Print the objects for (CountryInfo c : countries) System.out.println (c); } } Output Example programs  Write – Read a CountryInfo object  Write – Read an array of CountryInfo objects by separately  Write – Read an array object  Write – Read an ArrayList WriteCountryInfo2.java import java.io.*; public class WriteCountryInfo2 { public static void main (String[] args) throws IOException { CountryInfo[] countries = new CountryInfo[5]; countries[0] = new CountryInfo ("United States of America", "USA", "Washington, D.C.", 9629091L, 278058900L); countries[1] = new CountryInfo ("Russia", "RUS", "Moscow", 17075200L, 145470200L); countries[2] = new CountryInfo ("Italy", "ITA", "Rome", Write an array of object 301230L, 57679800L); countries[3] = new CountryInfo ("Sweden", "SWE", "Stockholm", 449964L, 8875100L); countries[4] = new CountryInfo ("Poland", "POL", "Warsaw", 312685L, 38633900L); FileOutputStream file = new FileOutputStream ("countries.dat"); ObjectOutputStream outStream = new ObjectOutputStream (file); // Serialize the objects to a file outStream.writeObject (countries); outStream.close(); } } ReadCountryInfo2.java import java.io.*; public class ReadCountryInfo2 { Read an array of object public static void main (String[] args) throws Exception { CountryInfo[] countries; FileInputStream file = new FileInputStream ("countries.dat"); ObjectInputStream inStream = new ObjectInputStream (file); // Deserialize the objects countries = (CountryInfo[]) inStream.readObject(); inStream.close(); // Print the objects for (CountryInfo c : countries) System.out.println (c); } } Output Example programs  Write – Read a CountryInfo object  Write – Read an array of CountryInfo objects by separately  Write – Read an array object  Write – Read an ArrayList Write – Read an ArrayList  The ArrayList class also implements the Serializable interface, so we can store an entire list of objects in one operation  If we had stored the CountryInfo objects in an ArrayList, we should write the entire set of objects out in one operation  Likewise, we should then read the entire ArrayList of CountryInfo objects from the file in one operation  Keep in mind that the objects stored in the ArrayList must also implement the Serializable interface for this to work WriteCountryInfo3.java import java.io.*; import java.util.ArrayList; public class WriteCountryInfo3 { public static void main (String[] args) throws IOException { ArrayList arrCountries = new ArrayList(); CountryInfo c; c = new CountryInfo ("United States of America", "USA", "Washington, D.C.", 9629091L, 278058900L); arrCountries.add(c); c = new CountryInfo ("Russia", "RUS", "Moscow", 17075200L, 145470200L); arrCountries.add(c); Write an ArrayList object c = new CountryInfo ("Italy", "ITA", "Rome", 301230L, 57679800L); arrCountries.add(c); c = new CountryInfo ("Sweden", "SWE", "Stockholm", 449964L, 8875100L); arrCountries.add(c); arrCountries.add (new CountryInfo ("Poland", "POL", "Warsaw", 312685L, 38633900L)); FileOutputStream file = new FileOutputStream ("countries.dat"); ObjectOutputStream outStream = new ObjectOutputStream (file); // Serialize the object arraylist to a file outStream.writeObject (arrCountries); outStream.close(); System.out.println("Save completed"); } } ReadCountryInfo3.java import java.io.*; import java.util.ArrayList; public class ReadCountryInfo3 { Read an ArrayList object public static void main (String[] args) throws Exception { ArrayList arrCountries = new ArrayList(); FileInputStream file = new FileInputStream ("countries.dat"); ObjectInputStream inStream = new ObjectInputStream (file); // Deserialize the objects arrCountries = (ArrayList) inStream.readObject(); inStream.close(); // Print the objects System.out.println ("DOC MOT TAP CAC DOI TUONG VAO ARRAYLIST"); System.out.println ("+++++++++++++++++++++++++++++++++++++++++++++++++"); for (Object c : arrCountries) System.out.println (c); } } ... Writing Reading open a stream open a stream write information read information close the stream close the stream FileOutputStream FileInputStream OutputStreamWriter InputStreamWriter BufferedWriter... 9629091L, 278 058900L); FileOutputStream file = new FileOutputStream ("countries.dat"); ObjectOutputStream outStream = new ObjectOutputStream (file); // Serialize the object to a file outStream.writeObject... CountryInfo(); FileInputStream file = new FileInputStream ("countries.dat"); ObjectInputStream inStream = new ObjectInputStream (file); // Deserialize the objects country = (CountryInfo) inStream.readObject();

Ngày đăng: 13/05/2014, 10:42

Từ khóa liên quan

Mục lục

  • Slide 1

  • Outline

  • Input/Output streams

  • Standard I/O

  • Exception in I/O

  • Slide 6

  • Writing text files

  • Slide 8

  • WriteFile.java

  • Slide 11

  • Reading text files

  • Slide 14

  • ReadFile.java

  • Writing - Reading text files (Unicode)

  • WriteFile_Uni.java (Unicode)

  • ReadFile_Uni.java (Unicode)

  • Slide 19

  • Object serialization

  • Serialization - Deserialization

  • Slide 22

Tài liệu cùng người dùng

Tài liệu liên quan