Java Programming Streams and Files - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Java Programming Streams and Files

Description:

An abstract class representing an input stream of bytes. All InputStreams are based on this class. ... readChar; readUTF; readLine(deprecated) DataOutputStream ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 11
Provided by: infmU
Category:

less

Transcript and Presenter's Notes

Title: Java Programming Streams and Files


1
Java Programming Streams and Files
  • Streams and Files
  • Provided by the java.io package for handling
    input/output
  • InputStream
  • An abstract class representing an input stream of
    bytes. All InputStreams are based on this class.
  • Methods(some of)
  • available() close() read() read(byte)
    mark(int) reset() skip(long)
  • OutputStream
  • Abstract class representing an output stream of
    bytes. All OutputStreams are based on this class.
  • Methods(some of)
  • close() write(int) write(byte) write(byte,
    int, int) flush()

2
Java Programming Streams and Files
  • PrintStream - Used for "ascii" output
  • System.out is a printstream
  • class PrintStreamExample
  • public static void main( String args )
  • System.out.println( "Hi Dad" )
  • 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()
    flush print(long) println(float) 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)

3
Java Programming Streams and Files
  • File
  • Represents a file or directory name in a
    platform-independent way
  • 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).
  • (Note. See FileReader and FileWriter in later
    versions of Java)
  • File Methods
  • canRead() getParent() length() canWrite()
    getPath() list() delete() hashCode()
  • list(FilenameFilter) equals(Object) exists()
    isAbsolute() mkdir() isDirectory() mkdirs()
    getAbsolutePath() isFile() renameTo(File)
    getName() lastModified() toString()

4
Java Programming Streams and Files
  • Example
  • import java.io.
  • class FileExamples
  • public static void main( String args )
  • // New files
  • File classDirectory new File(
    "ac461" )
  • classDirectory.mkdir()
    // Did not exist prior
  • File gradeFile new File( "ac461",
    "grades.text" )
  • gradeFile.mkdir()
    // Did not exist prior
  • File lectureNotes new File(
    "ac461/lectures.text" )
  • lectureNotes.mkdir()
    // Did not exist prior
  • String fileList classDirectory.list()
  • for ( int fileNum 0 fileNum lt
    fileList.length fileNum )
  • System.out.print( fileList fileNum
    "\t" )
  • System.out.println()
  • // Output
  • // grades.text lectures.text

5
Java Programming Streams and Files
  • FileInputStream
  • 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)

6
Java Programming Streams and Files
  • FileOutputStream
  • 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)

7
Java Programming Streams and Files
  • DataInputStream
  • Provides methods to read all the standard types
    from a byte-oriented InputStream
  • readBoolean readByte readShort readInt
    readLong readFloat
  • readChar readUTF readLine(deprecated)
  • DataOutputStream
  • Provides methods to write all of the primitive
    language types over a byte-oriented OutputStream
  • writeBooleanl writeByte writeShort writeInt
    writeLong writeFloat
  • writeChar writeUTF

8
Java Programming Streams and Files
  • Java 2 (additions)
  • Reader(abstract class for reading character
    streams)
  • BufferedReader
  • read (reads one or more characters)
  • readLine(reads a line of text)
  • InputStreamReader(bridge from byte to character
    streams)
  • StringReader(character stream whose source is a
    string)
  • FileReader (accessing a file)
  • Writer (abstract class for writing to character
    streams)
  • BufferedWriter
  • write(writes one or more characters)
  • newLine() (line separator)
  • OutputStreamWriter(bridge from character streams
    to byte streams)
  • PrintWriter (print formatted representations of
    objects to text output
  • print println write
  • FileWriter

9
Java Programming Streams and Files
  • Example - reading file
  • import java.io.
  • class ReadingFileExample
  • public static void main( String args )
    throws Exception
  • FileReader sourceFile new
    FileReader( "ReadingFileExample.java" )
  • BufferedReader inputFile new
    BufferedReader( sourceFile)
  • for ( int k 1 k lt 10 k )
  • System.out.println(
    inputFile..readLine() )
  • DataInputStream inputFileFormatted new
    DataInputStream(inputFile)
  • System.out.println(inputFileFormatted.readInt()
    )

10
Java Programming Streams and Files
  • Example - writing to file
  • import java.io.
  • class WritingToFile
  • public static void main( String args )
    throws Exception
  • int x7
  • String name Tom Jones
  • FileWriter writeFile new FileWriter(
    "newFile.txt")
  • BufferedWriter bufferedWriteFile new
    BufferedWriter ( writeFile )
  • PrintWriter out new PrintWriter(
    bufferedWriteFile)
  • out.println(x)
  • out.println(name)
  • out.write( "Distributed Computing")
Write a Comment
User Comments (0)
About PowerShow.com