Java io

2,989 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,989
On SlideShare
0
From Embeds
0
Number of Embeds
38
Actions
Shares
0
Downloads
191
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Java io

  1. 1. The java.io Package <ul><li>Text files </li></ul><ul><ul><ul><li>Reader and Writer classes </li></ul></ul></ul><ul><li>Byte stream files </li></ul><ul><ul><ul><li>InputStream, FileInputStream, OutputStream, FileOutputStream </li></ul></ul></ul><ul><li>Primitive-data stream files </li></ul><ul><ul><ul><li>DataInputStream, DataOutputStream </li></ul></ul></ul><ul><li>Object stream files </li></ul><ul><ul><ul><li>ObjectInputStream & ObjectOutputStream </li></ul></ul></ul>based on OOP with Java, by David J. Barnes Input-Output
  2. 2. The File Class <ul><li>A File object represents an abstract pathname . </li></ul><ul><ul><li>Represents both files and directories (folders). </li></ul></ul><ul><ul><li>Name, parent directory, size, permissions. </li></ul></ul><ul><ul><li>Constructor takes the file’s name: </li></ul></ul><ul><ul><ul><li>File info = new File(&quot;Letter.txt&quot;); </li></ul></ul></ul><ul><ul><li>No exception thrown if the file does not exist. </li></ul></ul>based on OOP with Java, by David J. Barnes Input-Output
  3. 3. Methods of the File Class based on OOP with Java, by David J. Barnes Input-Output File info = new File(&quot;Letter.txt&quot;); if(info.exists()){ System.out.println(&quot;Size of &quot;+info.getName()+ &quot; is &quot;+info.length()); if(info.isDirectory()){ System.out.println(&quot;The file is a directory.&quot;); } if(info.canRead()){ System.out.println(&quot;The file is readable.&quot;); } if(info.canWrite()){ System.out.println(&quot;The file is writeable.&quot;); } }
  4. 4. FileReader and FileWriter <ul><li>The two main classes for reading and writing text files. </li></ul><ul><ul><li>A file is opened when its name is passed to their constructors. </li></ul></ul><ul><ul><li>Files should be closed (via close ) when finished with. </li></ul></ul><ul><ul><li>Their read and write methods deal with char and char[]. </li></ul></ul>based on OOP with Java, by David J. Barnes Input-Output
  5. 5. Opening and Closing a File based on OOP with Java, by David J. Barnes Input-Output try{ // Try to open the file. FileReader inputFile = new FileReader(filename); // Process the file's contents. ... // Close the file now that it is finished with. inputFile.close(); } catch(FileNotFoundException e){ System.out.println(&quot;Unable to open &quot;+filename); } catch(IOException e){ // The file could not be read or closed. System.out.println(&quot;Unable to process &quot;+filename); }
  6. 6. Copying a Text File based on OOP with Java, by David J. Barnes Input-Output static void copyFile(FileReader inputFile, FileWriter outputFile) throws IOException { final int bufferSize = 1024; char[] buffer = new char[bufferSize]; // Read the first chunk of characters. int numberRead = inputFile.read(buffer); while(numberRead > 0){ // Write out what was read. outputFile.write(buffer,0,numberRead); numberRead = inputFile.read(buffer); } outputFile.flush(); }
  7. 7. Reading Single Characters based on OOP with Java, by David J. Barnes Input-Output static void copyFile(FileReader inputFile, FileWriter outputFile){ try{ // Read the first character. int nextChar = inputFile.read(); // Have we reached the end of file? while(nextChar != -1){ outputFile.write(nextChar); // Read the next character. nextChar = inputFile.read(); } outputFile.flush(); } catch(IOException e){ System.out.println(&quot;Unable to copy a file.&quot;); } }
  8. 8. Buffered Reader and Writers based on OOP with Java, by David J. Barnes Input-Output try{ FileReader in = new FileReader(infile); BufferedReader reader = new BufferedReader(in); FileWriter out = new FileWriter(outfile); BufferedWriter writer = new BufferedWriter(out); ... reader.close(); writer.close(); } catch(FileNotFoundException e){ System.out.println(e.getMessage()); } catch(IOException e){ System.out.println(e.getMessage()); }
  9. 9. Line-by-Line Copying based on OOP with Java, by David J. Barnes Input-Output BufferedReader reader = new BufferedReader(...); // Read the first line. String line = reader.readLine(); // null returned on EOF. while(line != null){ // Write the whole line. writer.write(line); // Add the newline character. writer.newLine(); // Read the next line. line = reader.readLine(); }
  10. 10. PrintWriter based on OOP with Java, by David J. Barnes Input-Output try{ FileWriter out = new FileWriter(outfile); PrintWriter writer = new PrintWriter(out); writer.println(…); writer.format(“%d…”,…); writer.close(); } catch(IOException e){ System.out.println(e.getMessage()); }
  11. 11. System.in based on OOP with Java, by David J. Barnes Input-Output BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
  12. 12. The StringTokenizer Class <ul><li>Defined in the java.util package. </li></ul><ul><li>Splits strings into separate String tokens. </li></ul><ul><ul><li>Delimiter characters are user settable (whitespace by default). </li></ul></ul><ul><ul><li>Will also return delimiters if required. </li></ul></ul><ul><li>Simple interface </li></ul><ul><ul><li>public boolean hasMoreTokens() </li></ul></ul><ul><ul><li>public String nextToken() </li></ul></ul>based on OOP with Java, by David J. Barnes Input-Output
  13. 13. Finding Words based on OOP with Java, by David J. Barnes Input-Output String line = in.readLine(); while(line != null){ StringTokenizer tokenizer = new StringTokenizer(line,&quot;,;:.&quot;' &quot;); while(tokenizer.hasMoreTokens()){ String word = tokenizer.nextToken(); // Print the next word. System.out.println(word); } line = in.readLine(); }
  14. 14. The Scanner Class <ul><li>Defined in the java.util package. </li></ul><ul><li>A scanner can be created from File , InputStream , or String . </li></ul><ul><li>Use useDelimiter to set the delimiting pattern. </li></ul><ul><li>Use next to retrieve the next token and use nextInt , etc. to retrieve a token of a certain type. </li></ul>based on OOP with Java, by David J. Barnes Input-Output
  15. 15. Finding Words based on OOP with Java, by David J. Barnes Input-Output String line = in.readLine(); while(line != null){ Scanner sc = new Scanner(line); sc.useDelimiter(&quot;,|;|:||&quot;|'| | &quot;); while(sc.hasNext()){ String word = sc.next(); // Print the next word. System.out.println(word); } line = in.readLine(); }
  16. 16. The Stream Classes <ul><li>Several classes that deliver input as a stream of raw bytes, or write raw bytes. </li></ul><ul><ul><li>BufferedInputStream, FileInputStream . </li></ul></ul><ul><ul><li>BufferedOutputStream,FileOutputStream. </li></ul></ul><ul><li>Their read and write methods take byte[] rather than char[] arguments. </li></ul>based on OOP with Java, by David J. Barnes Input-Output
  17. 17. Manipulating Primitive Data Files <ul><li>DataInputStream </li></ul><ul><ul><li>readBoolean(),readChar(),readInt()... </li></ul></ul><ul><li>DataOutputStream </li></ul><ul><ul><li>writeBoolean(),writeChar(),writeInt(),.... </li></ul></ul>based on OOP with Java, by David J. Barnes Input-Output
  18. 18. Reading from URLs based on OOP with Java, by David J. Barnes Input-Output import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream()));   String inputLine;   while ((inputLine = in.readLine()) != null) System.out.println(inputLine);   in.close(); } }
  19. 19. Review <ul><li>Input-Output is usually difficult to perform in a platform-independent way. </li></ul><ul><li>The java.io package provides the required independence. </li></ul><ul><li>The File class supplies details of external files. </li></ul><ul><li>Use Reader and Writer classes for text files. </li></ul>based on OOP with Java, by David J. Barnes Input-Output
  20. 20. Review (cont.) <ul><li>Use Buffered classes for input-output efficiency. </li></ul><ul><li>Use Stream classes when access to binary data is required. </li></ul><ul><li>Use DataInputStream and DataOutputStream when access to primitive data is required </li></ul><ul><li>StringTokenizer is useful for breaking up textual input. </li></ul>based on OOP with Java, by David J. Barnes Input-Output

×