Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 – Part 3 Dr. Clincy ------ Lecture.

Similar presentations


Presentation on theme: "Chapter 12 – Part 3 Dr. Clincy ------ Lecture."— Presentation transcript:

1 Chapter 12 – Part 3 Dr. Clincy Lecture

2 The File Class Up to this point, you have entered data into your program from a prompt – the data is temporary and lost after the program terminates. To permanently store the data for the program, it needs to be saved in a file. A file can be transported and read later by other programs. So the File class is introduced – useful for (1) obtaining file/directories properties, (2) deleting files/directories, (3) renaming files/directories, and (4) creating files/directories BTW: a filename is a string .. and the full or absolute filename contains a filename with its complete path and drive letter. For example: C:\\ksuweb\vclincy\1302.html For the relative file name, the directory path is omitted. ie html The File class DOES NOT contain methods for reading and writing file content The File class is a wrapper class for the file name and its directory path. Dr. Clincy Lecture

3 The File Class The File class is a wrapper class for the file name and its directory path. RECALL: A wrapper class is any class that “encapsulates” (or wrap) the functionality of another class. Example: new File (“c\\book”) creates a File object for the directory c:\\book Example: new File (“c:\\book\\test.dat”) creates a File object for the file c:\book\test.dat Once the object is created, various methods can be used Dr. Clincy Lecture

4 Obtaining File Properties and Manipulating File
Dr. Clincy Lecture

5 Example: Using File Class Methods
1 public class TestFileClass { 2 public static void main(String[] args) { java.io.File file = new java.io.File("image/us.gif"); System.out.println("Does it exist? " + file.exists()); System.out.println("The file has " + file.length() + " bytes"); System.out.println("Can it be read? " + file.canRead()); System.out.println("Can it be written? " + file.canWrite()); System.out.println("Is it a directory? " + file.isDirectory()); System.out.println("Is it a file? " + file.isFile()); System.out.println("Is it absolute? " + file.isAbsolute()); System.out.println("Is it hidden? " + file.isHidden()); System.out.println("Absolute path is " + file.getAbsolutePath()); System.out.println("Last modified on " + new java.util.Date(file.lastModified())); 16 } 17 } Dr. Clincy Lecture

6 Text I/O As mentioned, a File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes. The proper objects contain the methods for reading/writing data from/to a file. The Scanner class is used for reading text data from a file. (java.util.Scanner) The PrintWriter class is used for creating a file and writing text data to a file. (java.io.PrintWriter) PrintWriter someoutput = new PrintWriter (somefilename) Now print, println and printf can be used to write data to the file Dr. Clincy Lecture

7 Writing Data Using PrintWriter
Dr. Clincy Lecture

8 Dr. Clincy ------ Lecture

9 Dr. Clincy ------ Lecture

10 Reading Data Using Scanner
ReadData Run Dr. Clincy Lecture

11 Reading Data from the Web
Just like you can read data from a file on your computer, you can read data from a file on the Web if you know the file’s URL (Uniform Resource Locator – unique address for the file). (1) When you enter the URL in your browser (2) The web server sends the data to your browser – which displays graphically Dr. Clincy Lecture

12 Project 1 Difference between a project versus a lab: Work within team (no help outside of team). Mostly cover concepts already covered in the lab assignments. Bulk of the work is expected to done outside of class time. Will pay more attention to “how” the program is developed. Will pay more attention to the “quality” of the program. Will pay more attention to pre-development work: design and team work OBJECTIVE: You will create a program as a team based on some specs, give a presentation on how the program was built, and demonstrate the program in real-time. You will work in teams of 5 to 6 The program is due Monday, Feb 19th at 12pm – upload java files, UML diagrams, and powerpoint slides to DL2 – no need to upload screen shots because you will explain and demo your program on Monday (Feb 19th) during class time Your presentation on Monday (2/19/18) should be 15 minutes – penalty per minutes over or under – team decision on how to handle this – your minutes entails a 10-minute ppt presentation and 5-minute program run and test. The Professor will dictate the testing part of your program – you will only be able to run the code uploaded to D2L. Steps: Meet and Scope out work, Design, Allocate Work, Build program as a team, Test the program, and Prepare Presentation Dr. Clincy Lecture

13 Presentation Your presentation on Monday (2/19/18) should be 15 minutes – penalty per minutes over or under 15 minutes – team decision on how to handle this – your minutes entails a 10-minute ppt presentation and 5-minute program run and test. Every team member must present an equal amount of time. Your presentation should answer the following questions: Does the program fully run and satisfies the specs completely ? If not, what is missing ? Explain the design of your program via UML diagrams and flow charts. Explain why your program is efficient or not. Explain what exception handling was incorporated and why and where. Explain what superclass(es)-subclass(es) was (were) used and why. Explain any negative issues or problems the team may have encountered in working as team in building the program – and if those issues were remedied, how ? Regarding the program test – you will only be able to run the program uploaded to D2L. By doing so, all teams will have had the same deadline. Dr. Clincy Lecture

14 PROJECT 1 TEAMS As a team member, you are responsible for contacting team mates that did not attend class for the Project Rollout – and responsible for getting them up to speed regarding the project requirements and expectations. Dr. Clincy Lecture


Download ppt "Chapter 12 – Part 3 Dr. Clincy ------ Lecture."

Similar presentations


Ads by Google