Class FileWriter

Class used to write to files residing in filesystem.

Implemented Interfaces

OutputStream

Constructors

Methods

Example

main
  FileWriter fw = null
  try
    fw = new FileWriter "existingFile.dat" append true
  catch IOException e
    print "Could not open file for writing."
    return

  // contentsToWrite holds byte data which needs to be written.
  Byte[] contentsToWrite
  print "Initial file size: " & fw.get size
  fw.write contentsToWrite
  print "Size after write: " & fw.get size
  fw.close
            

(fileWriter) new fileWriter <string filename> [append <bool appendToEnd = false>]

Opens a file for writing. The file will be truncated unless appendToEnd parameter is true.

Parameters

Exceptions

Example

main
  FileWriter fw = null
  try
    fw = new FileWriter "existingFile.dat" append true
  catch IOException e
    print "File could not be opened."
    return

  print "File open successful."
  fw.close
                

set position to <int newPosition>

Sets the current position. The newPosition should be non-negative and not more than the current size of the file.

Parameters

Exceptions

Example

main
  FileWriter fw = new FileWriter "file.dat"
  try
    fw.set position to 1000
  catch IOException e
    print "File position could not be set."
    return

  print "Position set successfully."
                

(int) get position

Gets the current file position.

Return value

Exceptions

Example

main
  FileWriter fw = new FileWriter "file.dat"
  print "Current file position: " & fw.get position
  fw.close
                

(int) get size

Gets the size of the file.

Return value

Exceptions

Example

main
  FileWriter fw = new FileWriter "file.dat"
  print "File size: " & fw.get size
  fw.close