1. Transfer File Content One to Another
Code>>
import java.io.*;
class jFile
{
public static void main(String args[])
{
File inFile=new File("Input.txt");//Existing File
File outFile=new File("Output.txt");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(inFile);
outs=new FileWriter(outFile);
int ch;
while((ch=ins.read())!=-1)
{
outs.write(ch);
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}finally
{
try{
ins.close();
outs.close();
}catch(Exception ex)
{}
}
}
}
2. Writing Bytes to FILE
Code>>
import java.io.*;
class jBytes
{
public static void main(String args[])
{
try{
byte cites[]={'D','E','L','H','I','\n','K','O','L','K','A','T','A'};
FileOutputStream outfile=null;
outfile=new FileOutputStream("City.txt");
outfile.write(cites);
outfile.close();
}catch(Exception e)
{}
}
}
3. Reading Bytes from FILE
Code>>
import java.io.*;
public class jBytesCopy
{
public static void main(String args[])
{
try
{
FileInputStream inFile=null;
int b;
inFile=new FileInputStream("city.txt");
while((b=inFile.read())!=-1)
{
System.out.println((char)b);
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Code>>
import java.io.*;
class jFile
{
public static void main(String args[])
{
File inFile=new File("Input.txt");//Existing File
File outFile=new File("Output.txt");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(inFile);
outs=new FileWriter(outFile);
int ch;
while((ch=ins.read())!=-1)
{
outs.write(ch);
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}finally
{
try{
ins.close();
outs.close();
}catch(Exception ex)
{}
}
}
}
2. Writing Bytes to FILE
Code>>
import java.io.*;
class jBytes
{
public static void main(String args[])
{
try{
byte cites[]={'D','E','L','H','I','\n','K','O','L','K','A','T','A'};
FileOutputStream outfile=null;
outfile=new FileOutputStream("City.txt");
outfile.write(cites);
outfile.close();
}catch(Exception e)
{}
}
}
3. Reading Bytes from FILE
Code>>
import java.io.*;
public class jBytesCopy
{
public static void main(String args[])
{
try
{
FileInputStream inFile=null;
int b;
inFile=new FileInputStream("city.txt");
while((b=inFile.read())!=-1)
{
System.out.println((char)b);
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Comments
Post a Comment