CS 596 Client-Server Programming
Java Files Corrected
[To Lecture Notes Index]
San Diego State University -- This page last updated February 26, 1996
Contents of Java Files Corrected Lecture
- Streams and Files
- InputStream
- PrintStream
- File
- FileInputStream
- FileOutputStream
- Reading Files - Simple Way
- ASCIIInputStream
An abstract class representing an input stream of bytes. All InputStreams are
based on this class.
available() | read() |
close() | read(byte[]) |
mark(int) | read(byte[], int, int) |
markSupported() | reset() |
| skip(long) |
OutputStream
Abstract class representing an output stream of bytes. All OutputStreams are
based on this class.
close() | write(byte[], int, int) |
flush() | write(int) |
write(byte[]) | |
Used for "ascii" output
System.out is a printstream
class PrintStreamExample
{
public static void main( String args[] )
{
System.out.println( "Hi Mom" );
}
}
Constructors
PrintStream(OutputStream)
- Creates a new PrintStream.
PrintStream(OutputStream, boolean)
- Creates a new PrintStream, with auto flushing.
PrintStream Methods
checkError() | print(int) | println(double) |
close() | print(long) | println(float) |
flush() | print(Object) | println(int) |
print(boolean) | print(String) | println(long) |
print(char) | println() | println(Object) |
print(char[]) | println(boolean) | println(String) |
print(double) | println(char) | write(byte[], int, int) |
print(float) | println(char[]) | write(int) |
checkError()
- Flushes the print stream and returns whether or not there was an error on
the output stream.
The toString() Standard
class PoorPrint
{
String name = "sam";
}
class FancyPrint
{
String name = "pete";
public String toString()
{
return "FancyPrint( " + name + " )";
}
}
class TestPrint
{
public static void main( String args[] )
{
PoorPrint outOfIt = new PoorPrint();
FancyPrint cool = new FancyPrint();
System.out.println( outOfIt );
System.out.println( cool );
}
}
Output
PoorPrint@5e300848
FancyPrint( pete )
Constants
pathSeparator ( unix = : )
The system dependent path separator string.
pathSeparatorChar ( unix = : )
The system dependent path separator character.
separator ( unix = / )
The system dependent file separator String.
separatorChar ( unix = / )
The system dependent file separator character.
Constructors
File(String)
Creates a File object.
File(String, String)
Creates a File object from the specified directory.
File(File, String)
Creates a File object (given a directory File object).
File Methods
canRead() | getParent() | length() |
canWrite() | getPath() | list() |
delete() | hashCode() | list(FilenameFilter) |
equals(Object) | isAbsolute() | mkdir() |
exists() | isDirectory() | mkdirs() |
getAbsolutePath() | isFile() | renameTo(File) |
getName() | lastModified() | toString() |
import java.io.*;
class FileExamples
{
public static void main( String args[] )
{
// New files
File classDirectory = new File( "cs596" );
classDirectory.mkdir(); // Did not exist prior
File gradeFile = new File( "cs596", "grades.text" );
gradeFile.mkdir(); // Did not exist prior
File lectureNotes = new File( "cs596/lectures.text" );
lectureNotes.mkdir(); // Did not exist prior
// Existing file
File sourceFile = new File( "FileExamples.java" );
String[] fileList = classDirectory.list();
for ( int fileNum = 0; fileNum < fileList.length; fileNum++ )
System.out.print( fileList[ fileNum ] + "\t" );
System.out.println();
System.out.println( classDirectory.getAbsolutePath() );
System.out.println( gradeFile.exists() );
System.out.println( gradeFile.getParent() );
System.out.println( sourceFile.length() );
System.out.println( sourceFile.lastModified() );
gradeFile.delete();
}
}
Output
grades.text lectures.text
/home/ma/whitney/java/notes/cs596
true
cs596
980
823933450000
import java.io.*;
class MakeDirectoriesExamples
{
public static void main( String args[] )
{
// Does nothing if test/this does not exist
File a = new File( "test/this/out" );
a.mkdir();
// creates all four directories
File b = new File( "second/test/of/this" );
b.mkdirs();
}
}
import java.io.*;
class MachineIndependentFileNames
{
public static void main( String args[] )
{
String gradeFile = "/home/ma/whitney/cs596/grades";
gradeFile = gradeFile.replace( '/', File.separatorChar );
System.out.println( gradeFile );
}
}
File input stream, can be constructed from a file descriptor or a file name.
Constructors
FileInputStream(String)
- Creates an input file with the specified system dependent file name.
FileInputStream(File)
- Creates an input file from the specified File object.
FileInputStream(FileDescriptor)
FileInputStream Methods
available() | read() |
close() | read(byte[]) |
finalize() | read(byte[], int, int) |
getFD() | skip(long) |
File output stream, can be constructed from a file descriptor or a file name.
Constructors
FileOutputStream(String)
- Creates an output file with the specified system dependent file name.
FileOutputStream(File)
- Creates an output file with the specified File object.
FileOutputStream(FileDescriptor)
FileOutputStream Methods
close() | write(byte[]) |
finalize() | write(byte[], int, int) |
getFD() | write(int) |
import java.io.*;
import sdsu.io.*;
class ReadingFileExample
{
public static void main( String args[] ) throws Exception
{
File sourceFile = new File( "ReadingFileExample.java" );
FileInputStream inputFile = new FileInputStream( sourceFile);
ASCIIInputStream cin = new ASCIIInputStream( inputFile );
for ( int k = 1 ; k < 10; k++ )
System.out.println( cin.readWord() );
for ( int k = 1 ; k < 10; k++ )
System.out.println( cin.readLine() );
}
}
import java.io.*;
class WritingToFileExample
{
public static void main( String args[] ) throws Exception
{
FileOutputStream writeFile =
new FileOutputStream( "newFile");
PrintStream cout = new PrintStream( writeFile );
cout.println( "Save this for me");
}
}
ASCIIInputStream(InputStream)
- Creates a new ASCIIInputStream.
ASCIIInputStream Method
bad() | readChar() | readLong() |
eof() | readDouble() | readShort() |
flushLine() | readFloat() | readUnsignedByte() |
read(byte[]) | readFully(byte[]) | readUnsignedShort() |
read(byte[], int, int) | readFully(byte[], int, int) | readUTF() |
readBoolean() | readInt() | readWord() |
readByte() | readLine() | skipBytes(int) |