Professional Documents
Culture Documents
Tutorial 4
+File(pathname: String) Creates a File object for the specified pathname. The pathname may be a
directory or a file.
+File(parent: String, child: String) Creates a File object for the child under the directory parent. child may be a
filename or a subdirectory.
+File(parent: File, child: String) Creates a File object for the child under the directory parent. parent is a File
object. In the preceding constructor, the parent is a string.
+exists(): boolean Returns true if the file or the directory represented by the File object exists.
+canRead(): boolean Returns true if the file represented by the File object exists and can be read.
+canWrite(): boolean Returns true if the file represented by the File object exists and can be written.
+isDirectory(): boolean Returns true if the File object represents a directory.
+isFile(): boolean Returns true if the File object represents a file.
+isAbsolute(): boolean Returns true if the File object is created using an absolute path name.
+isHidden(): boolean Returns true if the file represented in the File object is hidden. The exact
definition of hidden is system-dependent. On Windows, you can mark a file
hidden in the File Properties dialog box. On Unix systems, a file is hidden if
its name begins with a period character '.'.
+getAbsolutePath(): String Returns the complete absolute file or directory name represented by the File
object.
+getCanonicalPath(): String Returns the same as getAbsolutePath() except that it removes redundant
names, such as "." and "..", from the pathname, resolves symbolic links (on
Unix platforms), and converts drive letters to standard uppercase (on Win32
platforms).
+getName(): String Returns the last name of the complete directory and file name represented by
the File object. For example, new File("c:\\book\\test.dat").getName() returns
test.dat.
+getPath(): String Returns the complete directory and file name represented by the File object.
For example, new File("c:\\book\\test.dat").getPath() returns c:\book\test.dat.
+getParent(): String Returns the complete parent directory of the current directory or the file
represented by the File object. For example, new
File("c:\\book\\test.dat").getParent() returns c:\book.
+lastModified(): long Returns the time that the file was last modified.
+delete(): boolean Deletes this file. The method returns true if the deletion succeeds.
+renameTo(dest: File): boolean Renames this file. The method returns true if the operation succeeds.
Manipulating a file by showing
file information
Please refer course material Unit 3.1,on page
7 – Figure 3.1 FileExpert.java & Figure 3.2
TestFile.java
The program will demonstrate the use of the
methods in the File class to obtain their
properties.
Understanding of Input/Output
stream
FileReader input = new FileReader("temp.txt");
int code = input.read();
System.out.println((char)code);
InputStreamReader FileReader
Reader
BufferedReader
Object BufferedWriter
PrintWriter
FileReader
To construct a FileReader, use the following
constructors:
public FileReader(String filename)
A java.io.FileNotFoundException would
}
FileWriter
To construct a FileWriter, use the following constructors:
If the file does not exist, a new file would be created. If the file
already exists, the first two constructors would delete the
current contents in the file.
To retain the current content and append new data into the file,
use the last two constructors by passing true to the append
parameter.
Sample
Code TestFileWriter.java
import java.io.*;
public class TestFileWriter {
public static void main(String[] args) throws IOException {
// Create an output stream to the file
FileWriter output = new FileWriter("temp.txt", true);
}
Binary I/O Classes
FileInputStream
DataInputStream
InputStream FilterInputStream
BufferedInputStream
ObjectInputStream
Object
FileOutputStream BufferedOutputStream
ObjectOutputStream PrintStream
FileInputStream
To construct a FileInputStream, use the following
constructors:
public FileInputStream(String filename)
If the file does not exist, a new file would be created. If the file
already exists, the first two constructors would delete the
current contents in the file. To retain the current content and
append new data into the file, use the last two constructors by
passing true to the append parameter.
Key Concepts of Unit 5
Push 2. 3. Pop.
3.
2 2
5. Push 7. 7
5. Pop. Stack is empty again.
2
8. Pop. 7. Push 7.
2 7
9. Etc. …
11. Push 5. 5
2
14. Push 5. 5
5
2
Activity 5.2
Say “N Q”.
Dequeue: Remove a value at the front.
Say “D Q”.
The first item added is the first removed.
9. Enqueue 5. 7 5
F B 9. Etc. …
11. Enqueue 5.
7 5 5
F B
Equivalent statements:
name = new String("Lisa Johnson");
name = "Lisa Johnson";
The class String
A String object is an instance of class String.
System.out.println(message);
System.out.println(myname);
System.out.println(college);
}
}
Example: the length()
method
public class LengthDemo {
System.out.println(message.charAt(0));
}
}
Example: String
Concatenation
public class ConcatDemo {
System.out.println(s3);
System.out.println(s1.concat(s2));
}
}
Example: the substring()
method
public class SubstringDemo {
}
}
Example: equals() and ==
operator
public class EqualsDemo {
if(s1.equals(s2)){
System.out.println("s1 and s2 have the same content");
}
else{
System.out.println("s1 and s2 do not have the same content");
}
if(s1 == s2){
System.out.println("s1 and s2 have the same reference");
}
else{
System.out.println("s1 and s2 do not have the same reference");
}
}
}
Example: compareTo()
method
public class CompareToDemo {
if(s1.compareTo(s2)> 0){
System.out.println("s1 is greater than s2");
}else if(s1.compareTo(s2) == 0){
System.out.println("s1 and s2 have the
same contents");
}else{
System.out.println("s1 is less than s2");
}
}
}
Example: String
Conversions
public class ConversionsDemo {
public static void main(String[] args) {
String s1 = new String("Welcome");
String s2 = " to ";
String s3 = " Java ";
System.out.println(s1.toUpperCase());
System.out.println(s1.toLowerCase());
System.out.println(s2);
System.out.println(s3.trim());
System.out.println(s3.replace("a","o"));
}
}
Example: the indexOf()
method
public class indexOfDemo {
public static void main(String[] args) {
String s1 = new String("Welcome to Java");
System.out.println(s1.indexOf('W'));
System.out.println(s1.indexOf('x'));
System.out.println(s1.indexOf('o',5));
System.out.println(s1.indexOf("come"));
System.out.println(s1.indexOf("Java",5));
System.out.println(s1.lastIndexOf('a'));
}
}
Self-test 5.5
Q1,Q2
Question 1