Friday, October 31, 2014

Java file Inputstream



Set last modified time of a file or directory

import java.io.*;
import java.util.Date; 
public class SetFileLastModifiedTime
{
public static void main(String[] args)
{
File file = new File("C://FileIO//ReadText.txt");
Date lastModified = new Date(file.lastModified());
System.out.println("Last modified time of file is : " + lastModified );
lastModified = new Date();
boolean blnSuccess = file.setLastModified(lastModified.getTime());
System.out.println("Was last modified time set successfully ?:" + blnSuccess);
System.out.println("File last modification date is : " +
new Date(file.lastModified()));
   }
}

Read file using File Input Stream

import java.io.*;
public class ReadStringFromFile
{
 public static void main(String[] args)
{
 File file = new File("C://FileIO//ReadString.txt");
 int ch;
 StringBuffer strContent = new StringBuffer("");
 FileInputStream fin = null;
 try
fin = new FileInputStream(file);
while( (ch = fin.read()) != -1)
strContent.append((char)ch);
fin.close();    
}
catch(FileNotFoundException e)
{
System.out.println("File " + file.getAbsolutePath() +
" could not be found on filesystem");
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file" + ioe);
System.out.println("File contents :");
System.out.println(strContent);
   }
}

Skip n bytes while reading the file using File Input Stream

import java.io.*;
public class SkipBytesReadFile
{
public static void main(String[] args)
{
File file = new File("C://FileIO//ReadString.txt");
try
{
FileInputStream fin = new FileInputStream(file);
int ch;
fin.skip(10);
while( (ch = fin.read()) != -1 )
System.out.print((char) ch);
}
catch(FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file " + ioe);
     }
  }
}

Read file in byte array using File Input Stream

import java.io.*;
public class ReadFileByteArray
{
public static void main(String[] args)
{
File file = new File("C://FileIO//ReadString.txt");
try
{
FileInputStream fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent);
String strFileContent = new String(fileContent);
System.out.println("File content : ");
System.out.println(strFileContent);
}
catch(FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file " + ioe);
   }
  }
}

Copy binary file using Streams

import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBinaryFile
{
 public static void main(String[] args)
{
String strSourceFile="C:/FileIO/source.dat";
String strDestinationFile="C:/FileIO/dest.dat";
try
{
FileInputStream fin = new FileInputStream(strSourceFile);
FileOutputStream fout = new FileOutputStream(strDestinationFile);
byte[] b = new byte[1024];
int noOfBytes = 0;                     
System.out.println("Copying file using streams");
while( (noOfBytes = fin.read(b)) != -1 )
{
fout.write(b, 0, noOfBytes);
}
System.out.println("File copied!");
fin.close();
fout.close();                  
}
catch(FileNotFoundException fnf)
{
System.out.println("Specified file not found :" + fnf);
}
catch(IOException ioe)
{
System.out.println("Error while copying file :" + ioe);
    }
  }
}

No comments:

Post a Comment