Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Package Statement Group related interfaces and classes together

Similar presentations


Presentation on theme: "The Package Statement Group related interfaces and classes together"— Presentation transcript:

1 The Package Statement Group related interfaces and classes together
Purpose: encapsulation and reduces name conflicts private package classes not visible outside the package Classes in different packages can have the same names Creating a package class file The first statement: package <package name>; Where do we store a package’s class files? In sub-folders of the parent package folder Name the sub-folder: <package name>

2 Packages in eclipse Default location for source files is: projectFolder/src. You can make a package directory by r-clicking the project | new package: Notice the folder holding the sources for the new package is a subdirectory of the src directory.

3 The Import Statement Example: import java.awt.event.*;
Import all the classes in the package java.awt.event If you use ‘*’, it must appear immediately following a ‘.’ Java automatically imports the package, java.lang Sub packages are referenced using dot notation. Example: java.awt.Button. Button is a sub-package of java.awt Example: Make new packages pkga and inside pkga, pkgb. Reference pkgb as pkga.pkgb (the fully qualified name of pkgb in java). Eclipse creates the directories src/pkga, src/pkga/pkgb. Fully qualified names are needed if you want to use a class that has the same name in two different packages you are importing.

4 Notes on the Import Statement
Import can reference either a single package class or all classes of a package. Wildcard references do not apply to sub-packages. Import java.awt.Button allows us to WRITE b = new Button(“Clear”); instead of b = new java.awt.Button(“Clear”); Legal uses of import: import java.awt.*; okay import java.awt.*; does not import java.awt.Event.*

5 Input and Output Streams
Streams handle Java input and output. Files are used to connect a stream to a file on the disk rather the keyboard or screen

6 File I/O Concepts Files allow programs to access persistent data.
Data in files is often structured as an ordered list of records. Each record generally contains an ordered list of fields. Example: a file of student records where each record contains a name, major, catalog year: Doe, Jane cs 2017 McGee Jimbob business 2015 Records may or may not be fixed length and contain the same number of fields, but often they do.

7 File I/O Concepts, con’t
File open operations: open, close. Java file traversal methodologies are: Sequential (all except RandomAccessFile) and Random (RandomAccessFile), Kind of data files hold: Text (characters only), Binary (characters and/or numeric data) and Object (serialized representations of java objects) File class: File file = new File(“pathname”) makes a java object describing a particular file on the disk if (!file.exists())  file doesn’t exist on the disk. Exceptions thrown by methods that deal with files. FileNotFoundException, IOException, EOFException

8 Sequential Text File Streams
Operations: sequential reads and writes of chars BufferedReader in = new BufferedReader ( new FileReader(“path”)); strVar = in.readLine(); in.close(); BufferedReader wraps FileReader Reads entire line instead of a character at a time The readLine() method returns null if at the end of file, but it consumes input if not at end of file. The mark() method will throw an exception if there is no more data to read. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(“path”))); out.println(“data”); out.close(); PrintWriter writes chars to text files.

9 Binary File Streams Operations: Reads and Writes of raw bytes of all primitive types DataOutputStream out=new DataOutputStream(new FileOutputStream(“path”)); DataInputStream in=new DataInputStream(new FileInputStream(“path”)); out.writeUTF(“data”); str = in.readUTF(); out.writeInt(x); int var = in.readInt(); out.writeDouble(3.5); double val = in.readDouble(); out.flush(); in.close(); out.close(); UTF stands for Unicode Text Format for writing and reading chars in a compact way. (See interface java.io.DataInput for the representation.) DataOutputStream wraps FileOutputStream to provide more methods for writing data of different types. See demos/WriteDemo.java Hexdump - very useful for looking at binary files. Mac – terminal – hexdump.

10 Random File Read and Write
RandomAccessFile raf = new RandomAccessFile(“path”,”access”); Acts like both a stream and a file, i.e., doesn’t need an additional stream class. Read(byte[]), readBoolean(), readChar(), readDouble(), readFloat(), readInt(), readLine(), readLong(), readShort(), readUTF(), length(), setLength(), getFilePosition(), writeBoolean(), writeChar(), writeDouble(), writeFloat(), writeInt(), writeLine(), writeLong(), writeShort(), writeUTF(),seek(long), close(). “access” notes Can be “rw”, “r”, “w”. If “r”, the file must pre-exist. If “rw” or “w” and the file doesn’t exist, it will be created. “rwd” writes with immediate updates to storage. Seek first and then use the various methods See demos/RAFDemo.java

11 Check if File is Readable
Boolean isReadable(String fileName) { File file = new File(fileName); if (!file.exists()) throw new FileNotFoundException(); if (!file.canRead()) throw new IOException(); }

12 Dialog for Choosing a File
String str = System.getProperty ("user.dir"); JFileChooser chooser = new JFileChooser(str); int result = chooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); String fileName = file.getName(); System.out.println("You selected " + fileName); // Insert code here to open and access data from file } else System.out.println("You cancelled the file dialog"); JFileChooser is part of javax.swing – a set of classes for building Graphical User Interfaces (GUI) See bottom of demos/WriteDemo.java


Download ppt "The Package Statement Group related interfaces and classes together"

Similar presentations


Ads by Google