Presentation is loading. Please wait.

Presentation is loading. Please wait.

Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,

Similar presentations


Presentation on theme: "Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,"— Presentation transcript:

1 based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream, OutputStream, FileOutputStream 4 Primitive-data stream files DataInputStream, DataOutputStream 4 Object stream files ObjectInputStream & ObjectOutputStream

2 based on OOP with Java, by David J. Barnes Input-Output2 The File Class  A File object represents an abstract pathname. –Represents both files and directories (folders). –Name, parent directory, size, permissions. –Constructor takes the file’s name: File info = new File("Letter.txt"); –No exception thrown if the file does not exist.

3 based on OOP with Java, by David J. Barnes Input-Output3 Methods of the File Class File info = new File("Letter.txt"); if(info.exists()){ System.out.println("Size of "+info.getName()+ " is "+info.length()); if(info.isDirectory()){ System.out.println("The file is a directory."); } if(info.canRead()){ System.out.println("The file is readable."); } if(info.canWrite()){ System.out.println("The file is writeable."); }

4 based on OOP with Java, by David J. Barnes Input-Output4 FileReader and FileWriter 4 The two main classes for reading and writing text files. –A file is opened when its name is passed to their constructors. –Files should be closed (via close ) when finished with. –Their read and write methods deal with char and char[].

5 based on OOP with Java, by David J. Barnes Input-Output5 Opening and Closing a File 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("Unable to open "+filename); } catch(IOException e){ // The file could not be read or closed. System.out.println("Unable to process "+filename); }

6 based on OOP with Java, by David J. Barnes Input-Output6 Copying a Text File 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 based on OOP with Java, by David J. Barnes Input-Output7 Reading Single Characters 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("Unable to copy a file."); }

8 based on OOP with Java, by David J. Barnes Input-Output8 Buffered Reader and Writers 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 based on OOP with Java, by David J. Barnes Input-Output9 Line-by-Line Copying 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 based on OOP with Java, by David J. Barnes Input-Output10 PrintWriter 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 based on OOP with Java, by David J. Barnes Input-Output11 System.in BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));

12 based on OOP with Java, by David J. Barnes Input-Output12 The StringTokenizer Class  Defined in the java.util package. 4 Splits strings into separate String tokens. –Delimiter characters are user settable (whitespace by default). –Will also return delimiters if required. 4 Simple interface –public boolean hasMoreTokens() –public String nextToken()

13 based on OOP with Java, by David J. Barnes Input-Output13 Finding Words String line = in.readLine(); while(line != null){ StringTokenizer tokenizer = new StringTokenizer(line,",;:.\"' \t"); while(tokenizer.hasMoreTokens()){ String word = tokenizer.nextToken(); // Print the next word. System.out.println(word); } line = in.readLine(); }

14 based on OOP with Java, by David J. Barnes Input-Output14 The Scanner Class  Defined in the java.util package.  A scanner can be created from File, InputStream, or String.  Use useDelimiter to set the delimiting pattern.  Use next to retrieve the next token and use nextInt, etc. to retrieve a token of a certain type.

15 based on OOP with Java, by David J. Barnes Input-Output15 Finding Words String line = in.readLine(); while(line != null){ Scanner sc = new Scanner(line); sc.useDelimiter(",|;|:|\\.|\"|'| |\t"); while(sc.hasNext()){ String word = sc.next(); // Print the next word. System.out.println(word); } line = in.readLine(); }

16 based on OOP with Java, by David J. Barnes Input-Output16 The Stream Classes 4 Several classes that deliver input as a stream of raw bytes, or write raw bytes. –BufferedInputStream, FileInputStream. –BufferedOutputStream,FileOutputStream.  Their read and write methods take byte[] rather than char[] arguments.

17 based on OOP with Java, by David J. Barnes Input-Output17 Manipulating Primitive Data Files 4 DataInputStream –readBoolean(),readChar(),readInt()... 4 DataOutputStream –writeBoolean(),writeChar(),writeInt(),....

18 based on OOP with Java, by David J. Barnes Input-Output18 Reading from URLs import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL yahoo = new URL("http://www.yahoo.com/"); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }

19 based on OOP with Java, by David J. Barnes Input-Output19 Review 4 Input-Output is usually difficult to perform in a platform-independent way.  The java.io package provides the required independence.  The File class supplies details of external files. 4 Use Reader and Writer classes for text files.

20 based on OOP with Java, by David J. Barnes Input-Output20 Review (cont.) 4 Use Buffered classes for input-output efficiency. 4 Use Stream classes when access to binary data is required. 4 Use DataInputStream and DataOutputStream when access to primitive data is required  StringTokenizer is useful for breaking up textual input.


Download ppt "Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,"

Similar presentations


Ads by Google