Saturday, November 1, 2014

Using in java Buffered Pro



Read File in String Using Java Buffered Input Stream

import java.io.*;
public class ReadFileInToString
{
public static void main(String[] args)
{
File file = new File("C://FileIO//ReadFile.txt");
BufferedInputStream bin = null;
try
{
FileInputStream fin = new FileInputStream(file);
bin = new BufferedInputStream(fin);
byte[] contents = new byte[1024];
int bytesRead=0;
String strFileContents;
while( (bytesRead = bin.read(contents)) != -1)
{
strFileContents = new String(contents, 0, bytesRead);
System.out.print(strFileContents);
   }
}
catch(FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file "+ ioe);
}
finally
{
try
{
if(bin != null)
bin.close();
}
catch(IOException ioe)
{
System.out.println("Error while closing the stream :"+ ioe);
         }
     }
   }
           }

Extract Zip File With Subdirectories Using Command Line Argument

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ExtractFileSubDirectories
{
public static void main(String args[])
{
String strZipFile = args[0];
if(strZipFile == null || strZipFile.equals(""))
{
System.out.println("Invalid source file");
System.exit(0);
}
unzip(strZipFile);
System.out.println("Zip file extracted!");
}
private static void unzip(String strZipFile)
{
try
{
File fSourceZip = new File(strZipFile);
String zipPath = strZipFile.substring(0, strZipFile.length()-4);
File temp = new File(zipPath);
temp.mkdir();
System.out.println(zipPath + " created");
ZipFile zipFile = new ZipFile(fSourceZip);
Enumeration e = zipFile.entries();
while(e.hasMoreElements())
{
ZipEntry entry = (ZipEntry)e.nextElement();
File destinationFilePath = new File(zipPath,entry.getName());
destinationFilePath.getParentFile().mkdirs();
if(entry.isDirectory())
{
continue;
}
else
{
System.out.println("Extracting " + destinationFilePath);
BufferedInputStream bis = new BufferedInputStream(zipFile
.getInputStream(entry));
int b;
byte buffer[] = new byte[1024];
FileOutputStream fos = new FileOutputStream(destinationFilePath);
BufferedOutputStream bos = new BufferedOutputStream(fos,1024);
while ((b = bis.read(buffer, 0, 1024)) != -1) 
{
bos.write(buffer, 0, b);
}
bos.flush();
bos.close();
bis.close();
    }
  }
}
catch(IOException ioe)
{
System.out.println("IOError :" + ioe);
       }
              }
}

Read line of characters from console using InputStream

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadLineFromConsoleExample
{
public static void main(String[] args)
{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
                  
String strLine = null;
System.out.println("Reading line of characters from console");
System.out.println("Enter exit to quit");
try
{
while( (strLine = br.readLine()) != null)
{
if(strLine.equals("exit"))
break;
System.out.println("Line entered : "  + strLine);
}
br.close();                    
}
catch(Exception e)
{
System.out.println("Error while reading line from console : " + e);
     }
   }
}

No comments:

Post a Comment