/////////////////////////////////////////////////////////////////////
// (C) Michael A Smith 1997-2000 University of Brighton //
// http://www.cem.brighton.ac.uk/staff/mas //
// From the book Java An Object-Oriented Language //
// Java 2 Application / Applet //
// Hence may not work with a JDK 1.0.1 / 1.1.* Compiler //
/////////////////////////////////////////////////////////////////////
// Version automatically created Tue 06 Dec 2011 19:52:48 GMT //
/////////////////////////////////////////////////////////////////////
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.BufferedInputStream;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.io.FileNotFoundException;
import java.io.FileDescriptor;
// So if you want to implement serializable can not do it
// by inheriting from Account
class Main
{
public static void main( String argsx[] )
{
System.out.println("Example 1"); Main.main1();
System.out.println("Example 2"); Main.main2();
System.out.println("Example 3"); Main.main3();
System.out.println("Example 4"); Main.main4();
System.out.println("Example 5"); Main.main5();
System.out.println("Example 5"); Main.main6();
String args[] = { "file.dat" };
System.out.println("Example 7"); Main.main7( args );
System.out.println("Example 8"); Main.main8();
System.out.println("Example 9"); Main.main9();
System.out.println("Example 10"); Main.main10();
}
// Binary O OUTPUT
public static void main1()
{
try
{
final int NUMBER = 5;
FileOutputStream ostream = new FileOutputStream("file.dat");
DataOutputStream dos = new DataOutputStream( ostream );
dos.writeInt( NUMBER*2 );
for ( int i=1; i<=NUMBER; i++ )
{
dos.writeInt( i );
}
dos.flush();
ostream.close();
}
catch ( IOException e )
{
System.out.println("IOException : " + e.getMessage() );
}
}
// Binary O OUTPUT append
public static void main2()
{
try
{
final int NUMBER = 5;
FileOutputStream ostream = new FileOutputStream("file.dat", true);
DataOutputStream dos = new DataOutputStream( ostream );
for ( int i=NUMBER+1; i<=NUMBER*2; i++ )
{
dos.writeInt( i );
}
dos.flush();
ostream.close();
}
catch ( IOException e )
{
System.out.println("IOException : " + e.getMessage() );
}
}
// Binary O INPUT
public static void main3()
{
try
{
FileInputStream istream = new FileInputStream("file.dat");
DataInputStream dis = new DataInputStream(istream);
int items = dis.readInt();
for( int i=1; i<=items; i++ )
{
int n = dis.readInt();
System.out.print( n + " " );
}
System.out.println();
istream.close();
}
catch ( IOException e )
{
System.out.println("IOException : " + e.getMessage() );
}
}
// Text O OUTPUT
public static void main4()
{
try
{
final int NUMBER = 5;
FileOutputStream ostream = new FileOutputStream("file.dat");
PrintWriter pw = new PrintWriter( ostream );
pw.println( NUMBER*2 );
for ( int i=1; i<=NUMBER; i++ )
{
pw.print( " " ); pw.print( i );
}
pw.flush();
ostream.close();
}
catch ( IOException e )
{
System.out.println("IOException : " + e.getMessage() );
}
}
// Text O OUTPUT Append
public static void main5()
{
try
{
final int NUMBER = 5;
FileOutputStream ostream = new FileOutputStream("file.dat", true);
PrintWriter pw = new PrintWriter( ostream );
for ( int i=NUMBER+1; i<=NUMBER*2; i++ )
{
pw.print( " " ); pw.print( i );
}
pw.flush();
ostream.close();
}
catch ( IOException e )
{
System.out.println("IOException : " + e.getMessage() );
}
}
// Text O INPUT
public static void main6()
{
try
{
FileInputStream ostream = new FileInputStream("file.dat");
BufferedInputStream bis = new BufferedInputStream( ostream );
int c = bis.read();
while ( c != -1 )
{
System.out.print( (char) c );
c = bis.read();
}
bis.close();
System.out.println();
}
catch ( IOException e )
{
System.out.println("IOException : " + e.getMessage() );
}
}
/*
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
*/
public static void main7( String args[] )
{
for ( int i=0; i<args.length; i++ )
{
try
{
FileInputStream fis = new FileInputStream( args[i] );
try
{
int c;
while ( ( c = fis.read() ) != -1 )
{
System.out.print( (char) c );
}
fis.close();
System.out.println();
}
catch ( IOException name )
{
System.err.println( "Error reading from : " + args[i] );
}
}
catch ( FileNotFoundException e )
{
System.err.println( "Can not open : " + args[i] );
}
}
}
/*
import java.io.StringInputStream;
*/
public static void main8()
{
try
{
String data = "First line\nSecond line\nThird line";
StringReader sr = new StringReader( data );
int c = sr.read();
while ( c != -1 )
{
System.out.print( (char) c );
c = sr.read();
}
System.out.println();
}
catch ( IOException e )
{
System.out.println("IOException : " + e.getMessage() );
}
}
//Using the PrintWriter class
// FileDescriptor .out .in .err
public static void main9()
{
FileOutputStream fos = new FileOutputStream( FileDescriptor.out );
PrintWriter pw = new PrintWriter( fos );
pw.print("Writing string : "); pw.println( "text" );
pw.print("Writing int : "); pw.println( 42 );
pw.print("Writing float : "); pw.println( 3.14f );
pw.flush(); // Must have
}
// Using the PrintWriter class with auto flush
public static void main10()
{
FileOutputStream fos = new FileOutputStream( FileDescriptor.out );
PrintWriter pw = new PrintWriter( fos, true );
pw.print("Writing string : "); pw.println( "text" );
pw.print("Writing int : "); pw.println( 42 );
pw.print("Writing float : "); pw.println( 3.14f );
}
}
|
© M.A.Smith University of Brighton.
Created February 1999 Last modified March 1999 Version 1.1
Comments, suggestions, etc. M.A.Smith at brighton dot ac dot uk [Home page] Printed / Displayed |