如何从文件中读取short
阵列音频或视频文件?我怎样才能把它写回文件?将文件转换为短阵列,反之亦然
0
A
回答
0
如果可能,我真的怀疑是否SHORT。不过,您可以查看Apache Commons File Utils读取文件byte[]
,反之亦然。
public static byte[] readFileToByteArray(File file) throws IOException
0
下面的代码片段使用FileInputStream读取文件,并使用FileOutputStream将文件写入给定路径。
Java代码:
byte[] fileBArray = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(fileBArray);
FileOutputStream fos = new FileOutputStream("C:\\abc.jpg");
fos.write(fileBArray);
0
这是更好的例子:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
Converting binary data into different forms.
<P>Reads binary data into memory, and writes it back out.
(If your're actually copying a file, there are better ways to do this.)
<P>Buffering is used when reading and writing files, to minimize the number
of interactions with the disk.
*/
public final class BytesStreamsAndFiles {
/** Change these settings before running this class. */
private static final String INPUT_FILE_NAME = "C:\\TEMP\\cottage.jpg";
private static final String OUTPUT_FILE_NAME = "C:\\TEMP\\cottage_copy.jpg";
/** Run the example. */
public static void main(String... aArgs) {
BytesStreamsAndFiles test = new BytesStreamsAndFiles();
//read in the bytes
byte[] fileContents = test.read(INPUT_FILE_NAME);
//test.readAlternateImpl(INPUT_FILE_NAME);
//write it back out to a different file name
test.write(fileContents, OUTPUT_FILE_NAME);
}
/** Read the given binary file, and return its contents as a byte array.*/
byte[] read(String aInputFileName){
log("Reading in binary file named : " + aInputFileName);
File file = new File(aInputFileName);
log("File size: " + file.length());
byte[] result = new byte[(int)file.length()];
try {
InputStream input = null;
try {
int totalBytesRead = 0;
input = new BufferedInputStream(new FileInputStream(file));
while(totalBytesRead < result.length){
int bytesRemaining = result.length - totalBytesRead;
//input.read() returns -1, 0, or more :
int bytesRead = input.read(result, totalBytesRead, bytesRemaining);
if (bytesRead > 0){
totalBytesRead = totalBytesRead + bytesRead;
}
}
/*
the above style is a bit tricky: it places bytes into the 'result' array;
'result' is an output parameter;
the while loop usually has a single iteration only.
*/
log("Num bytes read: " + totalBytesRead);
}
finally {
log("Closing input stream.");
input.close();
}
}
catch (FileNotFoundException ex) {
log("File not found.");
}
catch (IOException ex) {
log(ex);
}
return result;
}
/**
Write a byte array to the given file.
Writing binary data is significantly simpler than reading it.
*/
void write(byte[] aInput, String aOutputFileName){
log("Writing binary file...");
try {
OutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(aOutputFileName));
output.write(aInput);
}
finally {
output.close();
}
}
catch(FileNotFoundException ex){
log("File not found.");
}
catch(IOException ex){
log(ex);
}
}
/** Read the given binary file, and return its contents as a byte array.*/
byte[] readAlternateImpl(String aInputFileName){
log("Reading in binary file named : " + aInputFileName);
File file = new File(aInputFileName);
log("File size: " + file.length());
byte[] result = null;
try {
InputStream input = new BufferedInputStream(new FileInputStream(file));
result = readAndClose(input);
}
catch (FileNotFoundException ex){
log(ex);
}
return result;
}
/**
Read an input stream, and return it as a byte array.
Sometimes the source of bytes is an input stream instead of a file.
This implementation closes aInput after it's read.
*/
byte[] readAndClose(InputStream aInput){
//carries the data from input to output :
byte[] bucket = new byte[32*1024];
ByteArrayOutputStream result = null;
try {
try {
//Use buffering? No. Buffering avoids costly access to disk or network;
//buffering to an in-memory stream makes no sense.
result = new ByteArrayOutputStream(bucket.length);
int bytesRead = 0;
while(bytesRead != -1){
//aInput.read() returns -1, 0, or more :
bytesRead = aInput.read(bucket);
if(bytesRead > 0){
result.write(bucket, 0, bytesRead);
}
}
}
finally {
aInput.close();
//result.close(); this is a no-operation for ByteArrayOutputStream
}
}
catch (IOException ex){
log(ex);
}
return result.toByteArray();
}
private static void log(Object aThing){
System.out.println(String.valueOf(aThing));
}
}
更多细节,请访问:Reading and writing binary files
相关问题
- 1. 将列转换成行,反之亦然
- 2. 将ansi转换为utf8,反之亦然
- 3. 将float2转换为double2,反之亦然
- 4. 将lonlat转换为xyz,反之亦然
- 5. 将NSString转换为NSData,反之亦然
- 6. 将HTML转换为Xliff,反之亦然
- 7. 将JavaUtilDate转换为NSDate,反之亦然?
- 8. 将RSAPrivateKey转换为PrivateKey,反之亦然
- 9. 为什么要将JSON文件转换为XML,反之亦然?
- 10. C#将PDF文件转换为Word文档,反之亦然
- 11. 转换为字节阵列,反之亦然
- 12. 如何将矩阵转换为3D数组或反之亦然?
- 13. 将图像转换为矩阵,反之亦然?
- 14. 将音频,mp3文件转换为字符串,反之亦然
- 15. 将DataTable转换为XML文件并反之亦然
- 16. 如何将字符串转换为Java文件,反之亦然?
- 17. 将Andriod strings.xml文件转换为XLIFF,反之亦然
- 18. 将二进制文件转换为字符串,反之亦然
- 19. 行到列,反之亦然转换
- 20. DTO转换为实体,反之亦然
- 21. 将序列转换为字典,反之亦然
- 22. .NET - 将/映射列表转换为对象,反之亦然
- 23. 将字符串转换为位序列(反之亦然)
- 24. Eclipse CDT将“普通文件夹”转换为“源文件夹”或反之亦然
- 25. 我如何转换PDF文件,图像文件,反之亦然
- 26. 使用Erlang将二进制文件转换为文本,反之亦然
- 27. 转换位图的ByteArray反之亦然
- 28. 转换浮到ndarray或反之亦然
- 29. 转换Unicode字符串,反之亦然
- 30. 单为Android JavaList转换成列表,反之亦然
使用'ObjectOutputStream'和'ObjectInputStream'。 – 2013-02-12 07:13:43