java 文件操作之byte数组间的转换
2014-12-01 20:19
197 查看
java 文件操作之byte数组间的转换,本博客针对文本文件而写的,原先在网上找的有几个例子,但是
在获取文本内容时有乱码,自己又重新修改了点东西。具体代码如下
package com.file.until;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileHelper {
/**
* 获取文件内容方式
* @param filePath
* @return
* @throws IOException
*/
public byte[] getContent(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return buffer;
}
/**
* 获取文件内容方式
* @param filePath
* @return
* @throws IOException
*/
public byte[] getContent2(String filePath) throws IOException
{
FileInputStream in=new FileInputStream(filePath);
ByteArrayOutputStream out=new ByteArrayOutputStream(1024);
System.out.println("bytes available:"+in.available());
byte[] temp=new byte[1024];
int size=0;
while((size=in.read(temp))!=-1)
{
out.write(temp,0,size);
}
in.close();
byte[] bytes=out.toByteArray();
System.out.println("bytes size got is:"+bytes.length);
return bytes;
}
/**
* 将byte数组写入文件
* @param path
* @param content
* @throws IOException
*/
public void createFile(String path, byte[] content) throws IOException {
FileOutputStream fos = new FileOutputStream(path);
fos.write(content);
fos.close();
}
}
在获取文本内容时有乱码,自己又重新修改了点东西。具体代码如下
package com.file.until;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileHelper {
/**
* 获取文件内容方式
* @param filePath
* @return
* @throws IOException
*/
public byte[] getContent(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return buffer;
}
/**
* 获取文件内容方式
* @param filePath
* @return
* @throws IOException
*/
public byte[] getContent2(String filePath) throws IOException
{
FileInputStream in=new FileInputStream(filePath);
ByteArrayOutputStream out=new ByteArrayOutputStream(1024);
System.out.println("bytes available:"+in.available());
byte[] temp=new byte[1024];
int size=0;
while((size=in.read(temp))!=-1)
{
out.write(temp,0,size);
}
in.close();
byte[] bytes=out.toByteArray();
System.out.println("bytes size got is:"+bytes.length);
return bytes;
}
/**
* 将byte数组写入文件
* @param path
* @param content
* @throws IOException
*/
public void createFile(String path, byte[] content) throws IOException {
FileOutputStream fos = new FileOutputStream(path);
fos.write(content);
fos.close();
}
}
相关文章推荐
- JAVA 把File文件转换成Byte[]数组的标准写法
- 黑马程序员_学习日记40_605基础加强(自定义泛型、装拆箱、foreach、文件操作、字符串与byte数组转换)
- Java byte数组和文件相互转换
- JAVA中文件与Byte数组相互转换的方法
- Java 文件和byte数组转换
- 读取Java文件到byte数组的三种方式及Java文件操作大全(包括文件加密,String加密)
- java 实现 图片文件 与 byte 数组互相转换
- java 文件和byte数组相互转换
- Java 文件和byte数组转换
- java中byte数组与int类型的转换(两种方式)
- JAVA中实现图片对象转换成图片文件和字节数组(Byte[ ])的方法讨论
- 类型转换问题(java会把byte,short和char的运算操作转换为int类型即低精度自动向高精度转换)
- java 操作xml数据 转换byte spring源码分享
- java中byte数组与int类型的转换(两种方式)
- PHP数组的初始化 访问数组 数组操作符 多维数组的排序 对数组重新排序 从文件载入数组的方法 数组其他有用的操作 统计数组个数 将数组转换称标量变量 extract()
- Java中字符和byte数组之间的相互转换
- java byte数组与文件读写
- java中byte数组与int类型之间的相互转换(两种方式)
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- Java读取文件内容到byte[]数组中