Below program explains how to write large data files in java.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileWriting {
public static void main(String[] args) {
System.out.println(“Before Total Memory = “
+ Runtime.getRuntime().totalMemory());
System.out.println(“Before Free Memory = “
+ Runtime.getRuntime().freeMemory());
BufferedOutputStream fileOutputStream = null;
BufferedInputStream aTypeFileIn = null;
String bTypeFileName = “E:\\Test\\BType.xml”;
int fileread= 1024*1024 ;
byte fileBuffer[] = new byte[1024 * 1024];
int byteOfRead = 0;
int offsetOfFile = 0;
String aTypeFile = “E:\\Test\\AType.xml”;
while(true)
{
try {
fileOutputStream = new BufferedOutputStream( new FileOutputStream( bTypeFileName ) );
if( fileOutputStream == null ) {
fileOutputStream = new BufferedOutputStream( new FileOutputStream( bTypeFileName ) );
}
aTypeFileIn = new BufferedInputStream(
new FileInputStream(aTypeFile));
} catch (FileNotFoundException fileNotFound) {
}
do {
try {
byteOfRead = aTypeFileIn.read(fileBuffer, offsetOfFile,fileread);
} catch (IOException ioError) {
break;
}
if (byteOfRead != -1) {
try {
fileOutputStream
.write(fileBuffer, offsetOfFile, byteOfRead);
} catch (IOException ioError) {
break;
}
}
} while (byteOfRead != -1);
try
{
try {
aTypeFileIn.close() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
finally
{
try {
fileOutputStream.flush();
fileOutputStream.close();
System.out.println(“After Total Memory = “
+ Runtime.getRuntime().totalMemory());
System.out.println(“After Free Memory = “
+ Runtime.getRuntime().freeMemory());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
0 Comments:
Post a Comment