java读取文件及文件流
来源:互联网 发布:python 2.7 降级2.6 编辑:程序博客网 时间:2024/05/20 01:43
java读取文件或是文件流的代码,涵盖了读取jar文件中的文件流,网络文件流等,有些读取方式为了防止编码转换带来的问题,采取了动态byte[]的方式读取,源码如下 :
import java.io.BufferedInputStream;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Util {
public Util() {
}
/**
* 读取源文件内容
* @param filename String 文件路径
* @throws IOException
* @return byte[] 文件内容
*/
public static byte[] readFile(String filename) throws IOException {
File file =new File(filename);
if(filename==null || filename.equals(""))
{
throw new NullPointerException("无效的文件路径");
}
long len = file.length();
byte[] bytes = new byte[(int)len];
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
int r = bufferedInputStream.read( bytes );
if (r != len)
throw new IOException("读取文件不正确");
bufferedInputStream.close();
return bytes;
}
/**
* 将数据写入文件
* @param data byte[]
* @throws IOException
*/
public static void writeFile(byte[] data,String filename) throws IOException {
File file =new File(filename);
file.getParentFile().mkdirs();
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(data);
bufferedOutputStream.close();
}
/**
* 从jar文件里读取class
* @param filename String
* @throws IOException
* @return byte[]
*/
public byte[] readFileJar(String filename) throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(getClass().getResource(filename).openStream());
int len=bufferedInputStream.available();
byte[] bytes=new byte[len];
int r=bufferedInputStream.read(bytes);
if(len!=r)
{
bytes=null;
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
}
/**
* 读取网络流,为了防止中文的问题,在读取过程中没有进行编码转换,而且采取了动态的byte[]的方式获得所有的byte返回
* @param bufferedInputStream BufferedInputStream
* @throws IOException
* @return byte[]
*/
public byte[] readUrlStream(BufferedInputStream bufferedInputStream) throws IOException {
byte[] bytes = new byte[100];
byte[] bytecount=null;
int n=0;
int ilength=0;
while((n=bufferedInputStream.read(bytes))>=0)
{
if(bytecount!=null)
ilength=bytecount.length;
byte[] tempbyte=new byte[ilength+n];
if(bytecount!=null)
{
System.arraycopy(bytecount,0,tempbyte,0,ilength);
}
System.arraycopy(bytes,0,tempbyte,ilength,n);
bytecount=tempbyte;
if(n<bytes.length)
break;
}
return bytecount;
}
}
转自:http://blog.csdn.net/yi0731/article/details/1902483
- java读取文件及文件流
- java读取文件及文件流
- java读取文件及文件流
- java读取文件及文件流
- JAVA 读取文件及反编译
- java读取文件夹文件及操作
- java读取Properties文件及赋值
- java读取.properties文件及文件复制(通道)
- java IO流 读取文件 && 文件复制
- java读取文件-字符流读取方式
- Java IO流读取文件
- Java IO流读取文件
- Java IO流读取文件
- Java IO 流读取文件
- Java IO流读取文件
- Java IO流读取文件
- Java文件字符流读取
- java读取资源文件--使用文件流的方式读取
- 搭建android开发环境
- 2014-03-11项目4-长方柱类
- linux安装mysql出错( file /usr/share/mysql/czech/errmsg.sys from install of MySQL-server-5.5.31-2.el6.i6)
- 远去的我所期待的我
- 人生
- java读取文件及文件流
- The Architecture of Open Source Applications -- llvm
- Altium Designer 布线快捷键介绍
- 循环队列java
- easyui editor的用法
- 借记卡、准贷记卡与贷记卡的区别
- 百度《2014网报》对于网站建设的启发
- POJ-3660 Cow Contest 传递闭包
- sd卡开始篇-介绍