Lecture 9A
  Overview
  Streams
  Wrappers
  Keybd Example
  Console Example
  Keybd Numbers Ex.
  File Class
  File I/O
  File IO Char Ex.
  File IO Binary Ex.
  
Other I/O Features
  

Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture 9A : Character File I/O Example
Character File I/O Example

We will use the FileReader stream to read character data from text files.

In the example below, the read() method obtains one character at a time from the File. (For the host system this will correspond to one byte at a time.)

TextFileInput.java, .class

// Give the length in bytes of a text file
// passed as a command line argument.
// Use a FileReader to count the bytes directly
// and compare to the File.length() method.
//
// Adapted from exfileread.java from
// "Just Java", P.van der Linden, ch.12
//

import java.io.*;

public class TextFileInput {

  public static void main(String args[]) {
    int i=0;
    int bytesRead=0;
    File f = new File(args[0]);
    if( !f.exists() ) {
      System.out.println("No such file");
      System.exit(0);
    }

    // Read the bytes directly and count them.
    try {
      FileReader fr = new FileReader( f );
      while ( i != -1 ) {
         i = fr.read();
         if (i!=-1) bytesRead++;
      }
    } catch (IOException ioe) {
               System.out.println( "IO error:" + ioe );
    }
    System.out.println("bytes read from file: "+bytesRead);
 

  // Use the File class length() to provide the number
  // of bytes to compare.

    System.out.println("bytes in File.length(): "+f.length() );
  }
}

Similarly, we can use the FileWriter stream to write character data to a text.

TextFileOutput.java, .class

// Write characters input from the keyboard into
// a text file. The name of the text file is
// passed as a command line argument.
// Use a FileWriter
//
//
// Adapted from exfileread.java from
// "Just Java", P.van der Linden, ch.12
//

import java.io.*;

public class TextFileOutput {
  public static void main(String f[]) {
  FileWriter fw;
  int i;
  try {
    // Open a file stream to a text file
    // passed as an argument. The file will
    // be created if it doesn't exist.

    fw = new FileWriter( f[0] );

    while ( (i=System.in.read()) != -1 ) {
    // write one character at a time
      fw.write( (char) i );
    }
    fw.close();
  } catch (IOException ioe) {
    System.out.println( "IO error:" + ioe ); }
  }
}

Lastly, we use the FileWriter stream wrapped with a PrintWriter to write binary data into a text file.

BinToTextFile.java, .class

// Write binary data to a text file.
// The name of the text file is
// passed as a command line argument.
// Use a FileWriter & PrintWriter
//
// Adapted from exfilewrite3.java from
// "Just Java", P.van der Linden, ch.12
//

import java.io.*;

public class BinToTextFile {
  public static void main(String f[]) {
    try {
      FileWriter fw = new FileWriter(f[0]);

      // Wrap the PrintWriter with a PrintWriter
      // for sending the output to the file.

      PrintWriter pw = new PrintWriter(fw);

      boolean b=true;
      int i=27;
      double d=432.5;

      pw.print( b );
      pw.print( i );
      pw.print( d );

      pw.close();
    } catch (IOException ioe) {
               System.out.println( "IO error:" + ioe ); }
  }
}

 

 

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey