Hello,
I'm developing an application on J2ME that needs to read from files.
I want to use binary files instead text files, because I think that in this way I have two benefits:
1. reducing the jar size
2. speeding up the reading process.
that's the starting point...and I hope this approach is right.
I've developed a small class in J2SE just as an utility to convert .txt files in .bin files and...GULP!!!...a txt file of 22KB becomes 44KB converted in .bin...there is something wrong, but I don't know.
here is the code I wrote:
package texttobin;
import java.io.*;
public class Main {
public static void main(String[] args) {
File inFile = null; //the file to read
File outFile = null; //the file to write
//read the path from commandline
if (args.length > 0)
{
inFile = new File(args[0]);
outFile = new File(args[1]);
}
//some checkings
if (inFile == null) return;
if (outFile == null) return;
try {
//setup the streams
FileOutputStream outputStream = new FileOutputStream (outFile);
DataOutputStream dataOutputStream = new DataOutputStream (outputStream);
FileInputStream sourceStream = new FileInputStream(inFile);
byte readingByte[] = new byte[1];
while(true) {
if (sourceStream.read(readingByte)!=-1) {
//to be sure: I convert the readed input byte in a string
//and pass the char inside to the writeChar method...
dataOutputStream.writeChar(new String(readingByte).charAt(0));
//the same result is achieved with:
//dataOutputStream.writeChars(new String(readingByte));
}
else break;
}
dataOutputStream.close();
outputStream.close();
sourceStream.close();
}
catch(FileNotFoundException fnfe)
{
System.out.println (fnfe);
return;
}
catch(IOException ioe)
{
System.out.println (ioe);
return;
}
}
}
WHAT'S WRONG?!
THANKS A LOT!!!
daniele