Get Some Free Products at your Door Step. Just feed ur Address in dis Link...!!

Showing posts with label File Handling in Java. Show all posts
Showing posts with label File Handling in Java. Show all posts

Thursday, October 6, 2011

File Handling in Java

//creating a blank file.....!!
package secondtrm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JOptionPane;

public class Secondtrm
{
public static void main(String[] args) throws IOException
{
Secondtrm st = new Secondtrm();
//st.createNewFile();
//st.writeintofile();
//st.readFile();
//st.deleteFile();
//st.renameFile();
//st.CreateDirectory();
//st.DeleteDirectory();
//st.compareFile();
//st.countFileInFoler();
}
private FileInputStream InputStream;
public void createNewFile() throws IOException
{
File f = new File("abc.txt");
f.createNewFile();
}
public void writeintofile() throws IOException
{
File f = new File("abc.txt");
FileWriter fw=new FileWriter(f,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(JOptionPane.showInputDialog("Enter Text to write:"));
bw.newLine();
bw.close();
}
public void readFile() throws FileNotFoundException, IOException
{
File f = new File("abc.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
JOptionPane.showMessageDialog(null,br.readLine());
br.close();
}
public void deleteFile()
{
File f = new File("abc.txt");
if(f.exists()) f.delete();
else System.out.print("File doesn't Exist");
}
public void renameFile()
{
File f = new File("abc.txt");
File f2 = new File("newabc.txt");
f.renameTo(f2);
}
public void copyFile() throws IOException
{
File f = new File("newfile.txt");
File f2 = new File("newfile2.txt");
InputStream is= new FileInputStream(f);
OutputStream os= new FileOutputStream(f2,true);
int len;
byte[] buf = new byte[1024];
while((len=is.read(buf))>0)
{
os.write(buf,0,len);
}
is.close();
os.close();
System.out.print("Files Copies successfully");
}
public void CreateDirectory()
{
File f = new File("new Folder");
f.mkdir();
}
public void DeleteDirectory()
{
File f = new File("new Folder");
f.delete();
}
public void getFileProperties()
{
File f= new File("abc.txt");
System.out.println(f.canExecute());
System.out.println(f.canRead());
System.out.println(f.canExecute());
System.out.println(f.exists());
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.isHidden());
System.out.println(f.getAbsolutePath());
System.out.println(f.getFreeSpace());
System.out.println(f.getName());
System.out.println(f.getTotalSpace());
System.out.println(f.getUsableSpace());
}
public void setFileProperties()
{
File f = new File("abc.txt");
f.setExecutable(true);
// f.setLastModified("11:11:11");
f.setReadable(true);
f.setWritable(true);
}
public void compareFile()
{
File f = new File("abc.txt");
File f2= new File("cde.txt");
if(f.compareTo(f2)<0) System.out.println("f file is larger..!!"); if(f.compareTo(f2)>0) System.out.println("f file is smaller..!!");
if(f.compareTo(f2)==0) System.out.println("both files are equal in size");
}
public void countFileInFoler()
{
File f = new File("New Folder");
File[] files = f.listFiles();
System.out.println("No. of files in folder:"+files.length);
for(int i=0; i {
System.out.println("Contents of file "+(i+1)+": "+files[i].getName());
}
}
}