Java Exp 31 and 32
Java Exp 31 and 32
*;
class FileIOStream
{
public static void main(String[] args)
{
int ch;
try
{
FileOutputStream fileOutput = new FileOutputStream("mydata.dat");
//Create an instance of the output statream
for(int i=1;i<=10;i++) //write data to the stream
fileOutput.write(i);
fileOutput.close(); //close the output stream
}
catch(IOException e) { e.printStackTrace(); }
try
{
FileInputStream fileInput=new FileInputStream("mydata.dat");
//Create an instance of the input stream
while((ch=fileInput.read())!= -1) //reading data from a file, unil Eof is reached
System.out.print(ch + " ");
fileInput.close(); //close the input stream
}
catch(IOException e) { e.printStackTrace(); }
}
}
s
Ans 1.
Java stream classes can be categorised into two groups based on the data type one which
they operate:
1. Byte streams
2. Character Streams
Classes for file operations are:
● FileInputStream: Input stream that reads from a file
● FileOutputStream: Output stream that write to a file.
● FileReader: Input stream that reads from a file
● FileWriter : Output stream that write to a file.
Ans 2:
InputStream and OutputStream Classes
Methods:
public abstract int read() Reads a byte and returns as a integer 0-255
public int read(byte[] buf, int Reads and stores the bytes in buf starting at offset.
offset, int count) Count is the maximum read.
public int read(byte[] buf) Same as previous offset=0 and length=buf.length()
public long skip(long count) Skips count bytes.
public int available() Returns the number of bytes that can be read.
public void close() Closes stream
public abstract void write(int b) Write b as bytes.
public void write(byte[] buf, int Write count bytes starting from offset in buf.
offset, int count)
public void write(byte[] buf) Same as previous offset=0 and count = buf.length()
public void close() Closes stream
Ans 3:
● The Java Reader (java.io.Reader) and Java Writer class (java.io.Writer) in Java IO
work much like the InputStream and OutputStream with the exception that Reader and
Writer are character based. They are intended for reading and writing text.
Reader:
● The Java Reader is the base class of all Reader's in the Java IO API. Subclasses include
a BufferedReader, PushbackReader, InputStreamReader, StringReader and several
others.
FileReader reader = new FileReader("c:\\data\\myfile.txt");
int data = reader.read();
while(data != -1)
{
char dataChar = (char) data;
data = reader.read();
}
Writer:
● The Java Writer class is the base class of all Writers in the Java IO API. Subclasses
include BufferedWriter and PrintWriter among others.
FileWriter writer = new FileWriter("c:\\data\\file-output.txt");
writer.write("Hello World Writer");
writer.close();
Ans 4:
Compile time error: no suitable constructor found for FileOutputStream(no arguments)
(actual and formal argument lists differ in length)
Ans 1:
import java.io.*;
class CopyDataFiletoFile
{
public static void main(String args[])throws IOException
{
FileReader Fread =new FileReader("Hello.txt");
FileWriter Fwrite=new FileWriter("Hello1.txt");
System.out.println("File is Copied");
int c;
while((c=Fread.read())!=-1)
Fwrite.write((char)c);
Fread.close();
Fwrite.close();
}
}
Ans 2:
import java.io.*;
class WriteByteToFile
{
public static void main(String[] args)
{
int ch;
try
{
//Create an instance of the output statream
FileOutputStream fileOutput = new FileOutputStream("mydata.dat");
//write data to the stream
for(int i=1;i<=10;i++)
fileOutput.write(i);
fileOutput.close(); //close the output stream
}
catch(IOException e)
{ e.printStackTrace(); }
try
{
//Create an instance of the input stream
FileInputStream fileInput=new FileInputStream("mydata.dat");
//reading data from a file, unil Eof is reached
while((ch=fileInput.read())!= -1)
System.out.print(ch + " ");
fileInput.close(); //close the input stream
}
catch(IOException e)
{ e.printStackTrace(); }
}
}
Ans 3:
import java.io.*;
public class FileDisplay {
public static void main (String[] args)
{
if(args.length != 1)
{
System.out.println("Error: in sufficient arguments");
System.out.println("Usage - java FileDisplay SourceFile");
System.exit(-1);
}
try {
FileReader srcFile = new FileReader(args[0]);
int ch;
while((ch=srcFile.read()) != -1)
System.out.print((char)ch);
srcFile.close();
}
catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}
}
}