- public class IOTest {
- public static void main(String[] args) {
- IOTest io = new IOTest();
- try {
- long startTime = System.currentTimeMillis();
- io.readWrite("d:/file/aa.txt", "d:/file/bb.txt");
- long endTime = System.currentTimeMillis();
- System.out.println("Time taken for reading and writing using default behaviour : "
- + (endTime - startTime) + " milli seconds");
- long startTime1 = System.currentTimeMillis();
- io.readWriteBuffer("d:/file/aa.txt", "d:/file/bb.txt");
- long endTime1 = System.currentTimeMillis();
- System.out.println("Time taken for reading and writing using buffered streams : "
- + (endTime1 - startTime1) + " milli seconds");
- long startTime2 = System.currentTimeMillis();
- io.readWriteArray("d:/file/aa.txt", "d:/file/bb.txt");
- long endTime2 = System.currentTimeMillis();
- System.out.println("Time taken for reading and writing using custom buffering : "
- + (endTime2 - startTime2) + " milli seconds");
- long startTime3 = System.currentTimeMillis();
- io.readWriteCustom("d:/file/aa.txt", "d:/file/bb.txt");
- long endTime3 = System.currentTimeMillis();
- System.out.println("Time taken for reading and writing using custom buffering : "
- + (endTime3 - startTime3) + " milli seconds");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void readWrite(String fileFrom, String fileTo)
- throws IOException {
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new FileInputStream(fileFrom);
- out = new FileOutputStream(fileTo);
- while (true) {
- int bytedata = in.read();
- if (bytedata == -1)
- break;
- out.write(bytedata);
- }
- }
- finally {
- if (in != null)
- in.close();
- if (out != null)
- out.close();
- }
- }
- public static void readWriteBuffer(String fileFrom, String fileTo)
- throws IOException {
- InputStream inBuffer = null;
- OutputStream outBuffer = null;
- try {
- InputStream in = new FileInputStream(fileFrom);
- inBuffer = new BufferedInputStream(in);
- OutputStream out = new FileOutputStream(fileTo);
- outBuffer = new BufferedOutputStream(out);
- while (true) {
- int bytedata = inBuffer.read();
- if (bytedata == -1)
- break;
- out.write(bytedata);
- }
- }
- finally {
- if (inBuffer != null)
- inBuffer.close();
- if (outBuffer != null)
- outBuffer.close();
- }
- }
- public static void readWriteArray(String fileFrom, String fileTo)
- throws IOException {
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new FileInputStream(fileFrom);
- out = new FileOutputStream(fileTo);
- int availableLength = in.available();
- byte[] totalBytes = new byte[availableLength];
- int bytedata = in.read(totalBytes);
- out.write(totalBytes);
- }
- finally {
- if (in != null)
- in.close();
- if (out != null)
- out.close();
- }
- }
- public static void readWriteCustom(String from, String to)
- throws IOException {
- InputStream in = new FileInputStream(from);
- OutputStream out = new FileOutputStream(to);
- try {
- byte[] bytes = new byte[128 * 1024];
- int len;
- while ((len = in.read(bytes)) > 0) {
- out.write(bytes, 0, len);
- }
- } finally {
- if (in != null)
- in.close();
- if (out != null)
- out.close();
- }
- }
- }
public
void
performCopying(){
Writer writer = null ;
InputStream inputStream = null ;
try {
writer = new FileWriter( " test.dat " );
inputStream = getClass().getResourceAsStream( " ./test.resource " );
CopyUtils.copy(inputStream,writer);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(inputStream);
}
}
Writer writer = null ;
InputStream inputStream = null ;
try {
writer = new FileWriter( " test.dat " );
inputStream = getClass().getResourceAsStream( " ./test.resource " );
CopyUtils.copy(inputStream,writer);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(inputStream);
}
}