转转转--Java File和byte数据之间的转换
2024-10-15 08:31:50
package cn.iworker.file; import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class FileTest
{ public static void main(String[] args)
{
file2BetyArray();
fileToBetyArray();
BetyToFile("D:\\Study\\Java\\First.class");
} public static byte[] file2BetyArray()
{
File file = new File("D:\\Study\\Java\\First.class");
if (!file.exists()) {
return null;
}
FileInputStream stream = null;
ByteArrayOutputStream out = null;
try {
stream = new FileInputStream(file);
out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1) {
out.write(b, 0, n);
}
return out.toByteArray();// 此方法大文件OutOfMemory
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
stream.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
} }
return null;
} public static byte[] fileToBetyArray()
{
FileInputStream fileInputStream = null;
File file = new File("D:\\Study\\Java\\First.class");
byte[] bFile = null;
try {
bFile = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
for (int i = 0; i < bFile.length; i++) {
System.out.print((char) bFile[i]);
}
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
bFile.clone();
} catch (IOException e) {
e.printStackTrace();
}
}
return bFile;
} public static File BetyToFile( String filePath)
{
File file = new File(filePath);
BufferedOutputStream stream = null;
FileOutputStream fstream = null;
byte[] data=new byte[(int)file.length()];
try {
fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
if (null != fstream) {
fstream.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
return file;
} }
public static void byte2File(byte[] buf, String filePath, String fileName)
{
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try
{
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory())
{
dir.mkdirs();
}
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
}
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();
}
}
}
}
最新文章
- jquery中attr和prop的区别
- dvb标准
- 《CLR.via.C#第三版》第二部分第10,11章节读书笔记(五)
- [JS]jQuery,javascript获得网页的高度和宽度
- 【转】Flume日志收集
- std::bind和std::function
- [LeetCode] 桶排序的特殊解,例 Sort Color
- 使用栈Stack对整数数值的运算表达式字符串进行运算C#
- php中preg_match用户名正则实例
- Apache中关于页面缓存的设置
- Yii处理流程
- 如何捕获winform程序全局异常?
- oracle11g创建新的用户和改动最大连接数
- 静态方法使用bean
- winform调用cmd命令
- 张高兴的 Windows 10 IoT 开发笔记:RTC 时钟模块 DS3231
- java编程思想第四版第九章总结
- c++随机排序容器中的元素
- 网球pt站 T3nnis 情况说明
- 前段时间碰到的一些问题(免费WiFi设置+fiddler对手机进行抓包+fiddler抓不到https的请求)
热门文章
- SpringBoot使用Redis数据库
- BZOJ 2005: [Noi2010]能量采集(莫比乌斯反演)
- POJ 2699 The Maximum Number of Strong Kings (最大流+枚举)
- python 字典的合并
- ipconfig会出现多个IP地址
- Solaris11 Vsphere client tool 的鼠标问题
- ✅问题:Rails.ajax自定义请求
- UVA-140 Bandwidth (回溯+剪枝)
- UVA-1610 Party Games (构造)
- ablout unbuntu default mysql