Java中对二进制以及文本文件的读写操作
日期:2011-10-27 阅读:38 分类:Java资料
Java中对二进制以及文本文件的读写操作,对原生的类库做下简单包装:
public static byte[] readBinFile(String path) throws Exception
{
FileInputStream stream = new FileInputStream(path);
int len = stream.available();
byte[] buffer = new byte[len];
stream.read(buffer);
stream.close();
return buffer;
}
public static String readAscFile(String path) throws Exception
{
FileInputStream stream = new FileInputStream(path);
int len = stream.available();
byte[] buffer = new byte[len];
stream.read(buffer);
stream.close();
return new String(buffer);
}
public synchronized static void writeBinFile(String path, byte[] buffer) throws Exception
{
FileOutputStream output = new FileOutputStream(path, true);
output.write(buffer);
output.flush();
output.close();
}
public synchronized static void writeAscFile(String path, String content) throws Exception
{
FileOutputStream output = new FileOutputStream(path, true);
OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
writer.append(content);
writer.flush();
writer.close();
}
相关文章
- Javascript在chrome中辅助预订火车票的方法 2012-01-06
- 一款不错的日历控件 2008-07-31
- getElementsByClassName 2008-07-21
- Javascript常用函数归档 2008-12-01
- EasyWindow1.0 (更新版) 2008-06-25
- Filer.js:简化HTML5文件操作的开源JS库 2011-12-29
- 在Java7里如何对文件进行操作 2011-10-27