cisc 323 week 9
play

CISC 323 (Week 9) Programming Project Design of a Weather The next - PowerPoint PPT Presentation

CISC 323 (Week 9) Programming Project Design of a Weather The next three assignments form a Program & Java File I/O programming project. Assignment #3: Object-oriented analysis Assignment #4: Detailed project design


  1. CISC 323 (Week 9) Programming Project Design of a Weather � The next three assignments form a Program & Java File I/O programming project. � Assignment #3: Object-oriented analysis � Assignment #4: Detailed project design � Assignment #5: Project implementation Jeremy Bradbury Teaching Assistant March 8 & 10, 2004 bradbury@cs.queensu.ca Jeremy Bradbury (CISC 323) Mar 8 & 10, 2004 Assignment #4 Assignment #4 � Task 1: Big Class Diagram of program [only class � Due Date: Monday, March 15, 2003 (1:00 pm) names] (6 marks) � Move from a statement of the requirements to � Task 2: Detailed Class Diagram for each class in a design of how your program will be Task 1 (6 marks) structured to accomplish these goals. � Task 3: GUI Design [rough pictures of frames and dialogs] (3 marks) � Use layered approach – business, access, � Task 4: Sequence diagram [based on use case and view (each class should belong to exactly diagram] (3 marks) one layer!) � Task 5: Activity diagram [for a method with non-trivial loop] (2 marks) � TOTAL = 20 marks Jeremy Bradbury (CISC 323) Jeremy Bradbury (CISC 323) Mar 8 & 10, 2004 Mar 8 & 10, 2004

  2. Streams Java File I/O A stream is an object that transfers data. � Disclaimer: The following overview of Java File I/O and exceptions are slides from CISC 124 (Fall 2003) written by M. Lamb. Input stream : transfers data from a source to the program Output stream : transfers data from the program to a destination Jeremy Bradbury (CISC 323) Mar 8 & 10, 2004 CISC 124, fall 2003, I/O 4 Exceptions Output Streams An OutputStream is an object that I/O can throw lots of exceptions: device error knows how to send bytes to a destination file doesn't exist A FileOutputStream is an object that file is protected knows how to send bytes to a file end of file (in some cases) bytes can be any binary values IOException method in OutputStream : void write(int b) throws IOException FileNotFoundException EOFException These are not subclasses of RuntimeException. creating a FileOutputStream : Compiler will complain if you don't handle them. new FileOutputStream(String name) new FileOutputStream(String name, boolean append) Difficult to test some error conditions (device errors) Both can throw FileNotFoundException Still should write code to handle them CISC 124, fall 2003, I/O 5 CISC 124, fall 2003, I/O 6

  3. File Names Simple Example Write a short message to a file: In Java, a file name is a String that would be a valid file name on OutputStream outStream = null; // keep compiler happy your operating system. try { myFile.txt outStream = new FileOutputStream("OutputTest.txt"); c:\users\Margaret\Assignment3.java } catch (IOException e) { System.out.println("Error in opening output file"); Warning: backslash is the escape character in Java System.exit(1); String fileName = "c:\CISC124\data.txt"; } // end try/catch Gets compiler error. // write to the output stream try { Two ways around this: // write individual bytes 1. Double backslash means a real backslash in the string outStream.write('h'); outStream.write('e'); outStream.write('l'); outStream.write('l'); String fileName = "c:\\CISC124\\data.txt"; outStream.write('o'); } 2. Java accepts forward slash instead catch (IOException e) { String fileName = "c:/CISC124/data.txt"; System.out.println("Error in writing to output file"); System.exit(1); } // end try/catch CISC 124, fall 2003, I/O 7 CISC 124, fall 2003, I/O 8 Output Buffering Finishing the Previous Example try { If we run the preceeding program, the file may not contain all outStream.close(); the characters we wrote – common student debugging problem } Reason: output buffering catch (IOException e) { System.out.println("error in closing output file"); Buffer in memory File on disk } // end try/catch data Program data Now file OutputTest.txt will exist and contain the line: hello When we're done writing to file, we must close the file. This flushes the buffer and frees up system resources OutputStream method: void close() throws IOException CISC 124, fall 2003, I/O 9 CISC 124, fall 2003, I/O 10

  4. Folders Text Files A FileOutputStream writes individual bytes. Previous example created file with name Very flexible: bytes can be any values at all "OutputTest.txt" not great for writing text files Where does this file go (what folder)? Text File : contains ASCII (or Unicode) characters. Conceptually separated into lines by newline characters ( '\n' ) Answer: same folder as your program (Java files) Examples: .txt , .java , .bat , .html Good enough for assignments Operating systems have conventions about text files If you want to create a file in a different folder, must put folder into file name: Binary File : contains data coded in binary "subfolder/OutputTest.txt" Examples: .class, .exe, .pdf, .doc "../otherFolder/OutputTest.txt" (will discuss later) "c:/myfolder/OutputText.txt" CISC 124, fall 2003, I/O 11 CISC 124, fall 2003, I/O 12 Writer Creating a PrintWriter (1) A PrintWriter object uses an OutputStream a Writer writes characters to a text file Formats the output – figures out what characters to write – and sends the characters to the OutputStream value: "hello" PrintWriter class PrintWriter : writes formatted text output to a stream methods will look familiar: sequence of bytes: 'h','e','l','l','o',end of line print println Overloaded to take any primitive type or objects OutputStream Also a close method. None of these methods throw exceptions. CISC 124, fall 2003, I/O 13 CISC 124, fall 2003, I/O 14

  5. Creating a PrintWriter (2) Creating a PrintWriter (3) To create a PrintWriter , first create an OutputStream More compressed version: OutputStream outStream = null; // keep compiler happy PrintWriter writer = null; try { // create the output stream writer = new PrintWriter( try { new FileOutputStream("outputfile.txt")); outStream = new FileOutputStream(fileName); } } catch (IOException e) { catch (IOException e) { System.out.println("Error in opening output file"); System.out.println("Error in opening output file"); System.exit(1); System.exit(1); } } // end try/catch // create PrintWriter to write formatted output PrintWriter writer = new PrintWriter(outStream); CISC 124, fall 2003, I/O 15 CISC 124, fall 2003, I/O 16 Using the PrintWriter The Standard Output // write a string Writing to the "standard output" (the computer screen): writer.println("hello, world!"); System.out.print // write some other types System.out.println writer.print("the year is "); writer.print(2001); System is a class in the API. writer.print(", the current temperature is "); System.out is a static field in System writer.println(15.4); type of System.out is PrintStream writer.print("and my boolean variable is "); PrintStream is similar to PrintWriter boolean b = true; writer.println(b); writer.close() The file will contain: hello, world! the year is 2001, the current temperature is 15.4 and my boolean variable is true CISC 124, fall 2003, I/O 17 CISC 124, fall 2003, I/O 18

  6. The File Class Example a File object contains a file name many useful methods: File destination = new File("outputfile.txt"); if (destination.exists()) { boolean canRead() System.out.println("Output file exists."); boolean canWrite() System.out.print( boolean delete() "Do you want to overwrite it (y/n)? "); boolean exists() String answer = Stdin.readString(); long length() if (!answer.equalsIgnoreCase("y")) { System.out.println("aborting"); System.exit(1); constructors: } // end if File(String pathname) } // end if File(String parent, String child) .... File(File parent, String child) outStream = new FileOutputStream(destination); Can use instead of a file name to create OutputStreams & other I/O objects CISC 124, fall 2003, I/O 19 CISC 124, fall 2003, I/O 20 Text Input Using FileReaders The preceding slides told you how to write to a text file. Reader has method: int read() throws IOException Now we need to know how to read from a text file. Reads one character Returns -1 if at end of file Usually don't use FileReader directly a FileReader reads single characters from a text file. Create using a file name or File : FileReader(String fileName) throws FileNotFoundException FileReader(File file) throws FileNotFoundException CISC 124, fall 2003, I/O 21 CISC 124, fall 2003, I/O 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend