0% found this document useful (0 votes)
27 views5 pages

Stream Exercises

Uploaded by

bgmiv31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Stream Exercises

Uploaded by

bgmiv31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

STREAM EXERCISES

Read all characters from the file :


import java.io.*;
class fileread1
{
public static void main(String arg[])throws Exception
{
DataInputStream d = new DataInputStream (System.in);
String fname;
System.out.println("Enter a file name : ");
fname = d.readLine();
FileInputStream fis = new FileInputStream(fname);
int n;
while( (n=fis.read()) != -1)
{
System.out.print ((char) n);
}
}
}

Read array of bytes from the file :


import java.io.*;
class fileread1
{
public static void main(String arg[])throws Exception
{
DataInputStream d = new DataInputStream (System.in);
String fname;
System.out.println("Enter a file name : ");
fname = d.readLine();
FileInputStream fis = new FileInputStream(fname);
byte b[] = new byte[fis.available()];
fis.read(b);
String s= new String(b);
System.out.println(s);
}
}
Read array of bytes and also use skip function:

import java.io.*;
class fileread1
{
public static void main(String arg[])throws Exception
{
DataInputStream d = new DataInputStream (System.in);
String fname;
System.out.println("Enter a file name : ");
fname = d.readLine();
FileInputStream fis = new FileInputStream(fname);
byte b[] = new byte[20];
fis.read(b);
String s= new String(b);
System.out.println(s);
fis.skip(5);
fis.read(b);
s= new String(b);
System.out.println(s);
}
}

Read file using FilteredStream :


import java.io.*;
class fileread1
{
public static void main(String arg[])throws Exception
{
DataInputStream d = new DataInputStream (System.in);
String fname;
System.out.println("Enter a file name : ");
fname = d.readLine();
FileInputStream fis = new FileInputStream(fname);
DataInputStream dis = new DataInputStream(fis);
String s;
int lc=0, wc=0;
while( (s = dis.readLine()) !=null)
{
System.out.println ( s.toUpperCase() );
String words[] = s.split(" ");
lc++;
wc+= words.length;
}
System.out.println("Line Count = " + lc);
System.out.println("Word Count = " + wc);
}
}

FileOutputStream :
import java.io.*;
class filesave1
{
public static void main(String arg[ ])throws Exception
{
String fname="myfile.txt";
String data="Welcome to java's Output Stream";
FileOutputStream fos = new FileOutputStream(fname);
byte b[ ]=data.getBytes();
fos.write ( b );
fos.close();
System.out.println("File Saved");
}
}

Ex.2 :
import java.io.*;
class filesave1
{
public static void main(String arg[ ])throws Exception
{
DataInputStream d= new DataInputStream (System.in);
String fname,data;
System.out.println("Enter the filename : " );
fname = d.readLine();
FileOutputStream fos = new FileOutputStream(fname);
System.out.println("Enter your messages : ");
do
{
data = d.readLine();
byte b[ ]= data.getBytes();
fos.write(b);
} while( !data.endsWith("."));
fos.close();
System.out.println("File Saved");
}
}

FilteredStreams and FileOutputStream:


import java.io.*;
class filesave1
{
public static void main(String arg[ ])throws Exception
{
DataInputStream d= new DataInputStream (System.in);
String fname,data;
System.out.println("Enter the filename : " );
fname = d.readLine();
FileOutputStream fos = new FileOutputStream(fname);
PrintStream ps=new PrintStream(fos,true);
System.out.println("Enter your messages : ");
do
{
data = d.readLine();
ps.println (data);
} while( !data.endsWith("."));
fos.close();
System.out.println("File Saved");
}
}

FilteredStreams and FileOutputStream:


class filesave1
{
public static void main(String arg[ ])throws Exception
{
DataInputStream d= new DataInputStream (System.in);
String fname,data;
System.out.println("Enter the filename : " );
fname = d.readLine();
FileWriter fw = new FileWriter(fname);

PrintWriter ps=new PrintWriter(fw,true);


System.out.println("Enter your messages : ");
do
{
data = d.readLine();
ps.println (data);
} while( !data.endsWith("."));

fos.close();
System.out.println("File Saved");
}
}

You might also like