Saturday, November 1, 2014

Java byte pro



Write byte to file using Buffered Output Stream

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteByteToFile
{
public static void main(String[] args)
{
String strFile = "C:/FileIO/BufferedOutputStreamDemo.txt";
BufferedOutputStream bos = null;
try
{
FileOutputStream fos = new FileOutputStream(strFile);bos = new BufferedOutputStream(fos);byte b = 66;System.out.println("BufferedOutputStream: writing file"); bos.write(b);
System.out.println("BufferedOutputStream : written file");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Specified file not found" + fnfe);
}
catch(IOException ioe)
{
System.out.println("Error while writing to file" + ioe);
}
finally
{
try
{
if(bos != null)
{
bos.flush();
bos.close();
}                                                               
}
catch(Exception e){}                         
     }               
    }
}

Write byte array to file using Buffered Output Stream

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
public class WriteByteArrayToFile
{
public static void main(String[] args)
{
String strFileName = "C:/FileIO/BufferedOutputStreamDemo";
BufferedOutputStream bos = null;
try
{
FileOutputStream fos = new FileOutputStream(new File(strFileName));
bos = new BufferedOutputStream(fos);
                     
String str = "BufferedOutputStream Example";
System.out.println("Writing byte array to file");
bos.write(str.getBytes());
System.out.println("File written");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Specified file not found" + fnfe);
}
catch(IOException ioe)
{
System.out.println("Error while writing file" + ioe);
}
finally
{
if(bos != null)
{
try
{
bos.flush(); bos.close();
}
catch(Exception e){}
      }
    }
   }
}

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 byte array using Byte Array Input Stream

import java.io.ByteArrayInputStream;
public class ByteArrayInputStreamExample
{
public static void main(String[] args)
{
String str = "ByteArrayInputStream Example!";
byte[] bytes = str.getBytes();
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
int ch;
while((ch = bai.read()) != -1)
{
System.out.print((char)ch);
    }
    }
}

No comments:

Post a Comment