Java Examples
OOPS |
Exception |
RMI |
String |
Thread |
io |
Util |
Net |
AWT |
JDBC |
|
|
| IO PROGRAM:: |
| 1.File Program |
publiimport java.io.*;
import java.io.*;
public class FileEx {
public static void main (String[] args) {
File f = new File("d:/jan1.1/jan/03 IO/filefilt.java");
System.out.println("File Exists : " + f.exists());
System.out.println("Is File : " + f.isFile());
System.out.println("File Name : " + f.getName());
System.out.println("File Path : " + f.getPath());
System.out.println("Can Read : " + f.canRead());
System.out.println("Can Write : " + f.canWrite());
System.out.println("Is Directory : " + f.isDirectory());
System.out.println("Set Read Only : " + f.setReadOnly());
System.out.println("After setting Read Only : " + f.canRead());
System.out.println("Get the Parent File : " + f.getParentFile());
System.out.println("Get the Parent : " + f.getParent());
System.out.println("Length of the File : " + f.lastModified());
System.out.println("get Absolute path : " + f.getAbsolutePath());
System.out.println("Get Last Modified : " + f.lastModified());
System.out.println("Is a Hidden File : " + f.isHidden());
System.out.println();
File f1 = new File("d:/jan1.1/jan/03 IO");
System.out.println("Is Directory : " + f1.isDirectory());
File[] files = f1.listFiles();
System.out.println("Files in the GIven Directory :");
System.out.println();
for(int i=0;i<files.length;i++)
System.out.println("File "+ i + files[i].getName());
}
}
|
| 2.Buffered IO Stream Program |
import java.io.*;
public class BufferedIOStream {
public static void main(String[] args) throws IOException {
String s="This is a © copyright symbol "+"but this is © not.\n";
byte buf[]=s.getBytes();
ByteArrayInputStream in=new ByteArrayInputStream(buf);
BufferedInputStream f=new BufferedInputStream(in);
int c;
boolean marked=false;
while((c=f.read())!=-1) {
switch(c) {
case '&':
if(!marked){
f.mark(32);
marked=true;
}
else {
marked=false;
}
break;
case ';':
if(marked) {
marked=false;
System.out.print("(c)");
}
else
System.out.print((char)c);
break;
case ' ':
if(marked) {
marked=false;
f.reset();
System.out.print("&");
}
else
System.out.print((char)c);
break;
default:
if(!marked)
System.out.print((char)c);
break;
}
}
}
}
|
| 3.BufferedReaderWriter Program |
import java.io.*;
public class BufferedReaderWriter {
public static void main(String[] args) throws IOException {
String str="This is a © copyright symbol "+
"but this is © not.\n";
char buf[]= new char[str.length()];
str.getChars(0,str.length(),buf,0);
CharArrayReader in=new CharArrayReader(buf);
BufferedReader f=new BufferedReader(in);
int c;
boolean marked=false;
while((c=f.read())!=-1) {
switch(c) {
case '&':
if(!marked){
f.mark(32);
marked=true;
}
else {
marked=false;
}
break;
case ';':
if(marked) {
marked=false;
System.out.print("(c)");
}
else
System.out.print((char)c);
break;
case ' ':
if(marked) {
marked=false;
f.reset();
System.out.print("&");
}
else
System.out.print((char)c);
break;
default:
if(!marked)
System.out.print((char)c);
break;
}
}
}
}
|
| 4.File ReaderWriter Program |
// Example for File Input - Output Stream
import java.io.*;
public class FileReaderWriter {
public static void main(String[] args) throws IOException{
Reader in = new FileReader("d:/jan1.1/jan/03 IO/FileEx.java");
int ch=0;
while(ch!=-1) {
ch=in.read();
System.out.print((char)ch);
}
in.close();
Reader in1 = new FileReader("d:/jan1.1/jan/03 IO/FileEx.java");
Writer out = new FileWriter("e:app.txt");
ch=0;
while(ch!=-1) {
ch=in1.read();
out.write(ch);
}
in1.close();
out.close();
}
}
|
| 5.ByteArray IO Stream Program |
// Example for ByteArray Input and Output Stream
import java.io.*;
public class ByteArrayIOStream {
public static void main(String[] args) throws IOException{
String str = " Input for the Byte Array Input and Ouyput Stream : ";
byte[] b = str.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(b);
int ch=0;
while(ch!=-1) {
ch=in.read();
System.out.print((char)ch);
}
System.out.println();
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(b);
System.out.println("Buffer to String :"+ out.toString());
System.out.println("Buffer to Byte Array :"+ (new String(out.toByteArray())));
OutputStream file = new FileOutputStream("e:/byte_2_file.txt");
out.writeTo(file);
in.close();
out.close();
}
}
|
| 6.PushBackReader Program |
import java.io.*;
public class PushBackReader {
public static void main(String[] args) throws Exception {
String str = "if (a == 4) a = 0 ;\n ";
char[] buf = new char[str.length()];
str.getChars(0,str.length(),buf,0);
CharArrayReader in = new CharArrayReader(buf);
PushbackReader f = new PushbackReader(in);
int c;
while((c= f.read()) != -1) {
case '=' :
if ((c=f.read())=='=')
System.out.print(".eq.");
else {
System.out.print(">-");
f.unread(c);
}
break;
default :
System.out.print((char)c);
break;
}
}
}
}
|
| 7.PushBackStream Program |
import java.io.*;
public class PushBackStream {
public static void main(String[] args) throws Exception {
String str = "if (a == 4) a = 0 ;\n ";
byte[] buf = str.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
PushbackInputStream f = new PushbackInputStream(in);
int c;
while((c= f.read()) != -1) {
switch(c) {
case '=' :
if ((c=f.read())=='=')
System.out.print(".eq.");
else {
System.out.print(">-");
f.unread(c);
}
break;
default :
System.out.print((char)c);
break;
}
}
}
}
|
|
Back to Top |