一.byte数组转成File文件方法如下
/**
方法中传入的参数分别为
filePath:为生成的file文件地址,地址后要以\(File.separator)结尾,
fileName:为生成的file文件名称
**/
public static File fileToBytes(byte[] bytes, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
file = new File(filePath + fileName);
if (!file.getParentFile().exists()){
//文件夹不存在 生成
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
二.File文件转成byte数组方法如下
public static void main(String[] args) {
File file = new File("C:\\Users\\Scarlett\\Desktop\\62799425-1.bst");
System.out.println("文件大小为: " + file.length());
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
byte[] data = bos.toByteArray();
bos.close();
System.out.println(decrypt(data,"C:\\D2CommServer\\d2comm_site\\TrueReligion\\batches\\","62799425-2.bst"));
} catch (Exception e) {
e.printStackTrace();
}
}
三.String字符串转换成File文件
public static File stringByFile(String text, File file) {
if(file.exists()){
file.delete();
}
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new StringReader(text));
bw = new BufferedWriter(new FileWriter(file));
char[] buf = new char[1024 * 64]; //字符缓冲区
int len;
while ((len = br.read(buf)) != -1) {
bw.write(buf, 0, len);
}
bw.flush();
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}