Unit-13 Streams and Files-I

Demonstrate File…

Example : FileDemo

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.*;
class FileDemo
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])
{
File f1=new File("./java/copyright.java");
p("File Name:"+f1.getName());
p("Path:"+f1.getPath());
p("Abs Path:"+f1.getAbsolutePath());
p("Parent:"+f1.getParent());
p(f1.exists()?"exists":"does not exists");
p(f1.canWrite()?"is writeable":"is not writeable");
p(f1.canRead()?"is readable":"is not readable");
p("is"+(f1.isDirectory()?"":"not"+"a directory"));
p(f1.isFile()?"is normal file":"might be a named pipe");
p(f1.isAbsolute()?"is absolute":"is not absolute");
p("File lastmodified:"+f1.lastModified());
p("File size:"+f1.length()+"Bytes");
}
}
import java.io.*; class FileDemo { static void p(String s) { System.out.println(s); } public static void main(String args[]) { File f1=new File("./java/copyright.java"); p("File Name:"+f1.getName()); p("Path:"+f1.getPath()); p("Abs Path:"+f1.getAbsolutePath()); p("Parent:"+f1.getParent()); p(f1.exists()?"exists":"does not exists"); p(f1.canWrite()?"is writeable":"is not writeable"); p(f1.canRead()?"is readable":"is not readable"); p("is"+(f1.isDirectory()?"":"not"+"a directory")); p(f1.isFile()?"is normal file":"might be a named pipe"); p(f1.isAbsolute()?"is absolute":"is not absolute"); p("File lastmodified:"+f1.lastModified()); p("File size:"+f1.length()+"Bytes"); } }
import java.io.*;
class FileDemo
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])
{
File f1=new File("./java/copyright.java");
p("File Name:"+f1.getName());
p("Path:"+f1.getPath());
p("Abs Path:"+f1.getAbsolutePath());
p("Parent:"+f1.getParent());
p(f1.exists()?"exists":"does not exists");
p(f1.canWrite()?"is writeable":"is not writeable");
p(f1.canRead()?"is readable":"is not readable");
p("is"+(f1.isDirectory()?"":"not"+"a directory"));
p(f1.isFile()?"is normal file":"might be a named pipe");
p(f1.isAbsolute()?"is absolute":"is not absolute");
p("File lastmodified:"+f1.lastModified());
p("File size:"+f1.length()+"Bytes");
}
}
Output : FileDemo

Directories in Java…

Example : DirList

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.File;
class DirList
{
public static void main(String args[])
{
String dirname="/java";
File f1=new File(dirname);
if(f1.isDirectory())
{
System.out.println("Directory of"+dirname);
String s[]=f1.list();
for(int i=0;i<s.length;i++)
{
File f=new File(dirname+"/"+s[i]);
if(f.isDirectory())
{
System.out.println(s[i]+"is a directory");
}else
{
System.out.println(s[i]+"is a file");
}
}
}else
{
System.out.println(dirname+"is not a directory");
}
}
}
import java.io.File; class DirList { public static void main(String args[]) { String dirname="/java"; File f1=new File(dirname); if(f1.isDirectory()) { System.out.println("Directory of"+dirname); String s[]=f1.list(); for(int i=0;i<s.length;i++) { File f=new File(dirname+"/"+s[i]); if(f.isDirectory()) { System.out.println(s[i]+"is a directory"); }else { System.out.println(s[i]+"is a file"); } } }else { System.out.println(dirname+"is not a directory"); } } }
import java.io.File;
class DirList
{
public static void main(String args[])
{
String dirname="/java";
File f1=new File(dirname);
if(f1.isDirectory())
{
System.out.println("Directory of"+dirname);
String s[]=f1.list();
for(int i=0;i<s.length;i++)
{
File f=new File(dirname+"/"+s[i]);
if(f.isDirectory())
{
System.out.println(s[i]+"is a directory");
}else
{
System.out.println(s[i]+"is a file");
}
}
}else
{
System.out.println(dirname+"is not a directory");
}
}
}
Output : DirList

File Stream in Java…

ReadBytes in Java…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.*;
class ReadBytes
{
public static void main(String args[])
{
FileInputStream infile=null;
int b;
try
{
infile=new FileInputStream(args[0]);
while((b=infile.read())!=-1)
{
System.out.print((char)b);
}
infile.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
import java.io.*; class ReadBytes { public static void main(String args[]) { FileInputStream infile=null; int b; try { infile=new FileInputStream(args[0]); while((b=infile.read())!=-1) { System.out.print((char)b); } infile.close(); } catch(IOException ioe) { System.out.println(ioe); } } }
import java.io.*;
class ReadBytes
{
	public static void main(String args[])
	{
		FileInputStream infile=null;
		int b;
		try
		{
	infile=new FileInputStream(args[0]);
	while((b=infile.read())!=-1)
	{
		System.out.print((char)b);
	}
	infile.close();
}
catch(IOException ioe)
{	
	System.out.println(ioe);
}
}
}
Output : ReadBytes

WriteBytes in Java…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.*;
class WriteBytes
{
public static void main(String args[])
{
byte cities[]={'d','e','l','h','i','\n','C','h','e','n','n','a','i','\n','L','o','n','d','o','n','\n'};
FileOutputStream ofile=null;
try
{
ofile=new FileOutputStream("city.txt");
ofile.write(cities);
ofile.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
System.exit(-1);
}
}
}
import java.io.*; class WriteBytes { public static void main(String args[]) { byte cities[]={'d','e','l','h','i','\n','C','h','e','n','n','a','i','\n','L','o','n','d','o','n','\n'}; FileOutputStream ofile=null; try { ofile=new FileOutputStream("city.txt"); ofile.write(cities); ofile.close(); } catch(IOException ioe) { System.out.println(ioe); System.exit(-1); } } }
import java.io.*;
class WriteBytes
{
	public static void main(String args[])
	{
byte cities[]={'d','e','l','h','i','\n','C','h','e','n','n','a','i','\n','L','o','n','d','o','n','\n'};
	FileOutputStream ofile=null;
	try
	{
	ofile=new FileOutputStream("city.txt");
	ofile.write(cities);
	ofile.close();
	}
	catch(IOException ioe)
	{
		System.out.println(ioe);
		System.exit(-1);
	}
	}
} 
Output : WriteBytes

ByteArrayInputStreamReset in Java…

Example : ByteArrayInputStreamReset

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.*;
class ByteArrayInputStreamReset
{
public static void main(String args[])throws IOException
{
String tmp="abc";
byte b[]=tmp.getBytes();
ByteArrayInputStream in=new ByteArrayInputStream(b);
for(int i=0;i<2;i++)
{
int c;
while((c=in.read())!=-1)
{
if(i==0)
{
System.out.print((char)c);
}
else
{
System.out.print(Character.toUpperCase((char)c));
}
}
System.out.println();
in.reset();
}
}
}
import java.io.*; class ByteArrayInputStreamReset { public static void main(String args[])throws IOException { String tmp="abc"; byte b[]=tmp.getBytes(); ByteArrayInputStream in=new ByteArrayInputStream(b); for(int i=0;i<2;i++) { int c; while((c=in.read())!=-1) { if(i==0) { System.out.print((char)c); } else { System.out.print(Character.toUpperCase((char)c)); } } System.out.println(); in.reset(); } } }
 import java.io.*;
class ByteArrayInputStreamReset
{
public static void main(String args[])throws IOException
{
String tmp="abc";
byte b[]=tmp.getBytes();
ByteArrayInputStream in=new ByteArrayInputStream(b);
for(int i=0;i<2;i++)
{
int c;
while((c=in.read())!=-1)
{
if(i==0)
{
System.out.print((char)c);
}
else
{
System.out.print(Character.toUpperCase((char)c));
}
}
System.out.println();
in.reset();
}
}
}
Output : ByteArrayInputStreamReset

ByteArrayOutputStream in Java…

Example : ByteArray

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.*;
class ByteArray
{
public static void main(String args[])throws Exception
{
ByteArrayOutputStream f=new ByteArrayOutputStream(12);
System.out.println("Enter 10 characters and press the enter key");
System.out.println("These will be converted to uppercase and displayed");
while(f.size()!=10)
{
f.write(System.in.read());
}
System.out.println("Accepted characters into an array");
byte b[]=f.toByteArray();
System.out.println("Displaying characters in the array");
for(int l=0;l<b.length;l++)
{
System.out.println((char)b[l]);
}
ByteArrayInputStream inp=new ByteArrayInputStream(b);
int c;
System.out.println("Converted to uppercase characters");
for(int i=0;i<1;i++)
{
while ((c=inp.read())!=-1)
{
System.out.println(Character.toUpperCase((char)c));
}
System.out.println();
inp.reset();
}
}
}
import java.io.*; class ByteArray { public static void main(String args[])throws Exception { ByteArrayOutputStream f=new ByteArrayOutputStream(12); System.out.println("Enter 10 characters and press the enter key"); System.out.println("These will be converted to uppercase and displayed"); while(f.size()!=10) { f.write(System.in.read()); } System.out.println("Accepted characters into an array"); byte b[]=f.toByteArray(); System.out.println("Displaying characters in the array"); for(int l=0;l<b.length;l++) { System.out.println((char)b[l]); } ByteArrayInputStream inp=new ByteArrayInputStream(b); int c; System.out.println("Converted to uppercase characters"); for(int i=0;i<1;i++) { while ((c=inp.read())!=-1) { System.out.println(Character.toUpperCase((char)c)); } System.out.println(); inp.reset(); } } }
import java.io.*;
class ByteArray
{
public static void main(String args[])throws Exception
{
ByteArrayOutputStream f=new ByteArrayOutputStream(12);
System.out.println("Enter 10 characters and press the enter key");
System.out.println("These will be converted to uppercase and displayed");
while(f.size()!=10)
{
f.write(System.in.read());
}
System.out.println("Accepted characters into an array");
byte b[]=f.toByteArray();
System.out.println("Displaying characters in the array");
for(int  l=0;l<b.length;l++)
{
System.out.println((char)b[l]);
}
ByteArrayInputStream inp=new ByteArrayInputStream(b);
int c;
System.out.println("Converted to uppercase characters");
for(int i=0;i<1;i++)
{
while ((c=inp.read())!=-1)
{
System.out.println(Character.toUpperCase((char)c));
}
System.out.println();
inp.reset();
}
}
}
Output : ByteArray

DataInputStream in Java…

Example : DataStream

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.*;
class DataStream
{
public static void main(String args[])
{
DataInputStream dis=null;
DataOutputStream dos=null;
File intfile=new File("rand.dat");
try
{
dos=new DataOutputStream(new FileOutputStream(intfile));
for(int i=0;i<20;i++)
dos.writeInt((int)(Math.random()*100));
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
finally{
try
{
dos.close();
}
catch(IOException ioe)
{}
}
try
{
dis=new DataInputStream(new FileInputStream(intfile));
for(int i=0;i<20;i++)
{
int n=dis.readInt();
System.out.print(n+" ");
}
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
finally
{
try
{
dis.close();
}
catch(IOException ioe)
{}
}
}
}
import java.io.*; class DataStream { public static void main(String args[]) { DataInputStream dis=null; DataOutputStream dos=null; File intfile=new File("rand.dat"); try { dos=new DataOutputStream(new FileOutputStream(intfile)); for(int i=0;i<20;i++) dos.writeInt((int)(Math.random()*100)); } catch(IOException ioe) { System.out.println(ioe.getMessage()); } finally{ try { dos.close(); } catch(IOException ioe) {} } try { dis=new DataInputStream(new FileInputStream(intfile)); for(int i=0;i<20;i++) { int n=dis.readInt(); System.out.print(n+" "); } } catch(IOException ioe) { System.out.println(ioe.getMessage()); } finally { try { dis.close(); } catch(IOException ioe) {} } } }
import java.io.*;
class DataStream
{
public static void main(String args[])
{
DataInputStream dis=null;
DataOutputStream dos=null;
File intfile=new File("rand.dat");
try
{
dos=new DataOutputStream(new FileOutputStream(intfile));
for(int i=0;i<20;i++)
dos.writeInt((int)(Math.random()*100));
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
finally{
try
{
dos.close();
}
catch(IOException ioe)
{}
}
try
{
dis=new DataInputStream(new FileInputStream(intfile));
for(int i=0;i<20;i++)
{
int n=dis.readInt();
System.out.print(n+" ");
}
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
finally
{
try
{
dis.close();
}
catch(IOException ioe)
{}
}
}
}
Output : DataStream

Stream Tokenizer in Java…

Example : WordCounter

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.*;
public class WordCounter
{
public static void main(String args[])throws IOException
{
FileReader fr=new FileReader("./java/copyright.java");
StreamTokenizer input=new StreamTokenizer(fr);
int tok;
int count=0;
while((tok=input.nextToken())!=StreamTokenizer.TT_EOF)
if(tok==StreamTokenizer.TT_WORD)
{
System.out.println("Word Found:"+input.sval);
count++;
}
System.out.println("Foun"+count+"words in temp.txt");
}
}
import java.io.*; public class WordCounter { public static void main(String args[])throws IOException { FileReader fr=new FileReader("./java/copyright.java"); StreamTokenizer input=new StreamTokenizer(fr); int tok; int count=0; while((tok=input.nextToken())!=StreamTokenizer.TT_EOF) if(tok==StreamTokenizer.TT_WORD) { System.out.println("Word Found:"+input.sval); count++; } System.out.println("Foun"+count+"words in temp.txt"); } }
import java.io.*;
public class WordCounter
{
public static void main(String args[])throws IOException
{
FileReader fr=new FileReader("./java/copyright.java");
StreamTokenizer input=new StreamTokenizer(fr);
int tok;
int count=0;
while((tok=input.nextToken())!=StreamTokenizer.TT_EOF)
if(tok==StreamTokenizer.TT_WORD)
{
System.out.println("Word Found:"+input.sval);
count++;
}
System.out.println("Foun"+count+"words in temp.txt");
}
}
Output : WordCounter