I have written a ByteArray
in a file. And then I am trying to read that ByteArray
back from that same file..
public static void main(String[] args) throws Exception {
//.. some code
writeFile(allWrittenBytesTest);
readFile();
}
/**
* Write the file in Java
* @param byteArray
*/
public static void writeFile(byte[] byteArray) {
try{
File file = new File("bytearrayfile");
boolean success = file.delete();
if(!success) {
System.out.println("not able to delete the file");
}
FileOutputStream output = new FileOutputStream(file);
IOUtils.write(byteArray, output);
} catch (Exception ex) {
ex.printStackTrace();
}
Now, I am not able to understand how to read that ByteArray back from that same file? Below is my readFile method-
public static void readFile() {
BufferedReader reader = null;
try {
File file = new File("bytearrayfile");
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
// this doesn't work I know but not sure how to read that?
DataInputStream inTest = new DataInputStream(new ByteArrayInputStream(line));
// some other code to deserialize that ByteArray
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Well if you are using org.apache.commons.io package you could use just use:
bytes = FileUtils.readFileToByteArray(new File(file));
Example:
public static void readFile() {
BufferedReader reader = null;
try {
File file = new File("bytearrayfile");
reader = new BufferedReader(new FileReader(file));
byte[] bytes = FileUtils.readFileToByteArray(file);
System.out.println(new String(bytes, "UTF-8").toCharArray());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I hope this helps.
public class FileToByteArray {
public static void main(String a[]){
String fileName = "C:/MyFile.txt";
InputStream is = null;
try {
is = new FileInputStream(fileName);
byte content[] = new byte[2*1024];
int readCount = 0;
while((readCount = is.read(content)) > 0){
System.out.println(new String(content, 0, readCount-1));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(is != null) is.close();
} catch(Exception ex){
}
}
}
}
Your question is funny because if you interact with the concept of streams you almost always get bytes instead of an string. You used the idiom of reading a file to a string:
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
// ...code to use each line of a file in string format!
}
But this actually is made to read a file line by line to a string.
You can actually use just the plain FileInputStream but this time with another idiom (or you use Apache Commons like described above):
fis = new FileInputStream(filePath);
int currentBytes = 0
while((currentBytes=fis.read()) > -1){
//...code to use the file stream byte by byte
}
You could just put the integer (value between 0 and 255 for one byte 2^8) into an byte array. Now the problem is you don't know how many bytes you receive from the stream (how big the file is) and therefore you can't determine the size of your resulting byte array. One solution is to use a list (like ArrayList) and afterwards convert it to an byte[] with the list.toArray(new byte[list.size()]) method but I don't like this so much. There is an other class called ByteArrayOutputStream which handles the resizing of an byte[] transparently for you.
The code would look like:
fis = new FileInputStream(filePath);
int currentBytes = 0
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream()
while((currentBytes=fis.read()) > -1){
byteBuffer.write(currentBytes)
}
byte[] yourByteArray = byteBuffer.toByteArray()
The code is not tested, it is written from my brain. I hope it works.