aspose file tools*
The moose likes I/O and Streams and the fly likes Copying MS Access Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Copying MS Access Database" Watch "Copying MS Access Database" New topic
Author

Copying MS Access Database

Rajesh Nair
Greenhorn

Joined: Aug 22, 2001
Posts: 9
hi all,
i have been trying to take the back-up of a MS Access Database file by using the coding listed below.The Coding
is working well and i am getting a
new mdb file.But while trying to
open the file , it shows some error
( as wrong format file ).Can anybody suggest a solution
for this problem or any other better option for taking
database backup.
Thanx in Advance
Rajesh Nair

Transcriber.java
----------------
import java.io.*;
public class Transcriber
{
public static final String IN_FILE = "C:\\check\\test.mdb";
public static final String OUT_FILE = "d:\\trials\\copy.mdb";
private BufferedReader br;
private FileReader fr;
private BufferedWriter bw;
private FileWriter fw;
public static void main( String[] args )
{
Transcriber t = new Transcriber();
t.transcribe();
}
private void transcribe()
{
String inLine = null;
String outLine = null;
try
{
fr = new FileReader( IN_FILE );
br = new BufferedReader( fr );
fw = new FileWriter( OUT_FILE );
bw = new BufferedWriter( fw );
while ( ( inLine = br.readLine() ) != null )
{
// This line could be anything, but here
// I just copy the line.
outLine = inLine;
bw.write( outLine, 0, outLine.length() );
bw.newLine();
}
bw.close();
br.close();
}
catch ( FileNotFoundException e )
{
e.printStackTrace();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Your using the wrong Streams. Readers and Writers are for text data only. An Access database is stored as binary data. Change this to BufferedInputStream and BufferedOutputStream and read in and write a byte[].
Try working this change and if you still have problems, I'll work up a quick example.
------------------
Hope This Helps
Carl Trusiak, SCJP2


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rajesh Nair
Greenhorn

Joined: Aug 22, 2001
Posts: 9
Carl Trusiak,
Thanks a lot..
I got the result by using the following code.
Please let me know whether this method of coding will
show any problems in future while i am using it for copying a mdb file with lakhs of records.
Rajesh Nair
CopyBin.java
------------
import java.io.*;
public class CopyBin {
public static final String IN_FILE = "C:\\check\\test.mdb";
public static final String OUT_FILE = "d:\\trials\\copy.mdb";
public static void main( String[] args ){
int binaryByte;
BufferedInputStream bufferIn;
BufferedOutputStream bufferOut;
try {
bufferIn = new BufferedInputStream(
new FileInputStream(IN_FILE) );
} catch ( IOException ioErr ){
System.out.println( "Error accessing input file: " + IN_FILE );
return;
}
try {
bufferOut = new BufferedOutputStream(
new FileOutputStream(OUT_FILE) );
} catch ( IOException ioErr ){
System.out.println( "Error accessing output file: " + OUT_FILE );
return;
}
try {
binaryByte = bufferIn.read();
while( binaryByte >= 0 ){
bufferOut.write( binaryByte );
binaryByte = bufferIn.read();
}
} catch (IOException ioErr ){
System.out.println( "Error copying file" );
}
try {
bufferOut.close();
bufferIn.close();
} catch (IOException ioErr ){
System.out.println( "Error closing files" );
}
}
}

Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
That will work but, you will notice that it will be slow for large files. A simple change of
int binaryByte;
...
binaryByte = bufferIn.read();
while( binaryByte >= 0 ){
bufferOut.write( binaryByte );
binaryByte = bufferIn.read();
}
To
byte[] binaryByte= new byte[16 *1024];
//this gives you an 16K buffer for read write operations
int readCount = 0;
...
readCount = bufferIn.read(binaryByte, 0, 16 *1024);
while( readCount >= 0 ){
bufferOut.write( binaryByte, 0 , readCount );
readCount = bufferIn.read(binaryByte, 0, 16 *1024);
}
Will significatly imporve performance.


------------------
Hope This Helps
Carl Trusiak, SCJP2
 
 
subject: Copying MS Access Database