1. 读文件
public static byte[] getByteArrayFromFile(String fileName)
{
File file = null;
try
{
file = new File(fileName);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
if ( !file.exists() || !file.isFile() || !file.canRead() )
{
return null;
}
byte[] byteArray = null;
try
{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int count;
byte buffer[] = new byte[1024];
while ( (count = fis.read(buffer)) > 0 )
{
baos.write(buffer, 0, count);
}
byteArray = baos.toByteArray();
fis.close();
baos.flush();
baos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return byteArray;
public static boolean saveByteArrayToFile(String fileName, byte[] data)
{
boolean success = false;
if ( null != fileName )
{
String name = fileName;
int count = 0;
File file = new File(name);
while ( file.exists() )
{
count++;
name = fileName + count;
file = new File(name);
}
//file.getParentFile().mkdirs();
try
{
FileOutputStream fs = new FileOutputStream(fileName);
fs.write(data);
fs.close();
success = true;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return success;
}
else
{
return success;
}
}