Lab-Chapter 2 Exception H and File
Lab-Chapter 2 Exception H and File
package exception;
package exception;
try
{
//null'ed strings should crash your program
System.out.println(text.length());
}
catch (NullPointerException ex)
{
System.out.println("Exception: cannot get the text's
length");
}
// you will now see this print
System.out.println("done");
}
}
package exception;
import java.util.Scanner;
public class Exception04
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
try
{
System.out.println("Enter number of donuts:");
int donutCount = keyboard.nextInt();
System.out.println("Enter number of glasses of milk:");
int milkCount = keyboard.nextInt();
if (milkCount < 1)
Chapter 2 Exception Handling & File Streams Page |3
System.out.println("End of program.");
}
}
package filestream;
Chapter 2 Exception Handling & File Streams Page |4
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TextFileOutputDemo
{
public static void main(String[] args)
{
String fileName = "out.txt";
PrintWriter outputStream = null;
File file1 = null;
try
{
file1 = new File("out.txt");
outputStream = new PrintWriter(file1);
}
catch(FileNotFoundException fnfE)
{
System.out.println("Error opening the file" +fileName);
System.exit(0);
}
System.out.println("Enter three lines of text:");
Scanner keyboard = new Scanner(System.in);
outputStream.close();
System.out.println("Those lines were written to "
+file1.getAbsolutePath());
}
}
package filestream;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class AppendTextFile
{
public static void main(String[] args)
{
String fileName = "out.txt";
PrintWriter outputStream = null;
try
{
outputStream = new PrintWriter(new
FileOutputStream(fileName,true));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file" +fileName);
System.exit(0);
}
Chapter 2 Exception Handling & File Streams Page |6
package filestream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileInputStream;
public class TextFileInputDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println(" Please Enter a file name to be read");
String fileName = input.next();
Scanner inputStream = null;
try
{
inputStream = new Scanner(new FileInputStream(fileName));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
System.exit(0);
}
while(inputStream.hasNextLine())
{
String line = inputStream.nextLine();
System.out.println(line);
}
inputStream.close();
}
}
package filestream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
{
public static void main(String[] args)
{
String fileName = "numbers.dat";
try
{
ObjectOutputStream outputStream = new
ObjectOutputStream(new FileOutputStream(fileName));
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter nonnegative integers.");
System.out.println("Place a negative number at the "+
"end.");
int anInteger;
do
{
anInteger = keyboard.nextInt();
outputStream.writeInt(anInteger);
} while (anInteger >= 0);
{
System.out.println("Problem with output to file " +
fileName);
}
}
}
package filestream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BinaryInputDemo
{
public static void main(String[] args)
{
String fileName = "numbers.dat";
try
{
ObjectInputStream inputStream = new ObjectInputStream(new
FileInputStream(fileName));
System.out.println("Reading ALL the integers");
System.out.println("in the file " + fileName);
try
{
while(true)
{
Chapter 2 Exception Handling & File Streams P a g e | 10
package filestream;
import java.io.File;
import java.io.IOException;
public class FileOperation
{
public static void main(String[] args) throws IOException
Chapter 2 Exception Handling & File Streams P a g e | 11
{
File f = new File("Test1","xyz.txt");
boolean b = f.createNewFile();
if (b)
{
System.out.println("File is created successfully in " +
f.getAbsolutePath());
}
else
{
System.out.println("File is already existed in " +
f.getAbsolutePath());
}
System.out.println(f.canRead());
System.out.println(f.exists());
System.out.println(f.canWrite());
System.out.println(f.getName());
System.out.println(f.isFile());
System.out.println(f.isDirectory());
System.out.println(f.isAbsolute());
System.out.println(f.lastModified());
System.out.println(f.length()+ " bytes");
}
}
package serialization;
Chapter 2 Exception Handling & File Streams P a g e | 12
package serialization;
import java.io.Serializable;
{
this.accountNo = accountNo;
this.accountName = accountName;
if (balance < 0)
{
throw new ValueTooSmallException("Negative Balance");
}
else
{
this.balance = balance;
}
package serialization;
import java.io.*;
}
}
package serialization;
import java.io.*;
}
catch(IOException | ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}
package filestream.random;
import java.io.RandomAccessFile;
import java.io.IOException;
public class WriteRAF
{
public static void main(String[] args) throws IOException
{
int i;
String text;
RandomAccessFile fileOut;
fileOut = new RandomAccessFile("RAFile1.dat", "rw");
for (i = 1; i < 11; i++)
{
text = "Address of " + i +" person \n";
System.out.println("offset: " + fileOut.length() );
fileOut.seek(fileOut.length());
fileOut.writeChars(text);
}
fileOut.close();
}
Chapter 2 Exception Handling & File Streams P a g e | 16
}
package filestream.random;
import java.io.*;
public class ReadRAF
{
public static void main(String[] args) throws IOException
{
char singleChar;
StringBuffer address;
RandomAccessFile fileIn;
fileIn = new RandomAccessFile("RAFile1.dat", "r");
fileIn.seek(80);
address = new StringBuffer(20);
while ((singleChar = fileIn.readChar()) != '\n')
{
address.append(singleChar);
}
System.out.println(address);
fileIn.close();
}