• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Copying MS Access Database

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
}
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rajesh Nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
It's just a flesh wound! Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic