如何从Java文件中读取ByteArray?
我在一个文件中写了一个字节数组。然后我试着从同一个文件中读回ByteArray如何从Java文件中读取ByteArray?,java,file,bytearray,Java,File,Bytearray,我在一个文件中写了一个字节数组。然后我试着从同一个文件中读回ByteArray public static void main(String[] args) throws Exception { //.. some code writeFile(allWrittenBytesTest); readFile(); } /** * Write the file in Java * @param byteArray */ public s
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();
}
现在,我无法理解如何从同一个文件中读取ByteArray?下面是我的readFile方法-
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();
}
}
如果您使用的是org.apache.commons.io包,那么您可以使用:
bytes = FileUtils.readFileToByteArray(new File(file));
例如:
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();
}
}
我希望这有帮助。你的问题很有趣,因为如果你与流的概念交互,你几乎总是得到字节而不是字符串。您使用了将文件读入字符串的习惯用法:
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!
}
但这实际上是为了将文件逐行读取为字符串
实际上,您可以只使用普通的FileInputStream,但这次使用另一种习惯用法,或者使用Apache Commons,如上所述:
fis = new FileInputStream(filePath);
int currentBytes = 0
while((currentBytes=fis.read()) > -1){
//...code to use the file stream byte by byte
}
您可以将一个字节2^8的0到255之间的整数值放入字节数组。现在的问题是,您不知道从流中接收多少字节,文件有多大,因此无法确定生成的字节数组的大小。一种解决方案是使用类似ArrayList的列表,然后使用list.toArraynew byte[list.size]方法将其转换为byte[],但我不太喜欢这种方法。还有另一个名为ByteArrayOutputStream的类,它为您透明地处理字节[]的大小调整
代码如下所示:
fis = new FileInputStream(filePath);
int currentBytes = 0
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream()
while((currentBytes=fis.read()) > -1){
byteBuffer.write(currentBytes)
}
byte[] yourByteArray = byteBuffer.toByteArray()
代码没有经过测试,它是从我的大脑中编写的。我希望它能起作用 公共类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){
}
}
}
}您不需要删除。创建FileOutputStream仍将创建一个新文件。@EJP:谢谢提示。'readCount-1'不正确,应为'readCount'。在任何情况下,代码都不是答案。你需要解释一下-但事实并非如此。除非您知道数据是文本,否则根本不应该使用读卡器和写卡器。否则,您可能会损坏数据。未经测试的代码不可能是答案-1@EJP:很好的一点,我一直认为BufferedReader是一个缓冲区,设计用于所有类型的数据,也只是字节,但看起来读写器是为文本数据设计的。嗯,我调整了我的答案,只使用普通的流。