从指定文件(字节数组)获取内容以及获取长度
2016-12-07 11:20
316 查看
package cn.felay.io; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; /** * @author <a mailto:felayman@163.com>felayman</a> * @timer 2014年6月10日 下午3:46:19 */ public class InputStreamDemo { /** * 关闭输入流 * * @param in */ public void freeInputStream(InputStream in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 获取输入流 * * @param fileName * @return */ public InputStream getInputStream(String fileName) { InputStream in = null; try { in = new FileInputStream(fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } return in; } /** * 从指定的文件中获取内容 * * @param fileName * @return */ public String getContentFromFile(String fileName) { InputStream in = this.getInputStream(fileName); byte[] b = new byte[1024]; try { while (in.read(b) != -1) { } } catch (IOException e) { e.printStackTrace(); } finally { this.freeInputStream(in); } String content = new String(b); content = content.trim(); return content; } /** * 获取文件中字节长度 * * @param fileName * @return */ public int getLenFromFile(String fileName) { InputStream in = null; int len = 0; try { in = new FileInputStream(fileName); len = in.available(); } catch (FileNotFoundException e) { System.out.println(e.getLocalizedMessage()); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return len; } /** * 从字节数组中获取字节长度 * * @param b * @return */ public int getLenFromByte(byte[] b) { InputStream in = null; in = new ByteArrayInputStream(b); int len = 0; try { len = in.available(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return len; } public byte[] getContentFromString(String str) { byte[] b = str.getBytes(); return b; } public static void main(String[] args) { // 获取文件中字节长度 InputStreamDemo isd = new InputStreamDemo(); String fileName = "src/res/test1.text"; int fileLen = isd.getLenFromFile(fileName); System.out.println("文件长度为:" + fileLen); // 从指定文件获取内容 String content = isd.getContentFromFile(fileName); System.out.println("获取的内容为:" + content); } }
相关文章推荐
- 1.实验 5.2.5 文件定位 用lseek()函数实现以下功能 1. 获取文件大小 2. 为文件添加指定长度的空洞 3. 在指定位置写入指定内容 4. 读出指定位置的内容 1. 获取文件大小
- [Java] 利用xpdf库获取pdf文件的指定范围文本内容
- [Java] 利用xpdf库获取pdf文件的指定范围文本内容
- [Java] 利用xpdf库获取pdf文件的指定范围文本内容
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byt
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- C#实现 获取指定字节长度 中英文混合字符串 的方法
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- python读取指定字节及位置的文件内容
- 探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解
- C#实现 获取指定字节长度 中英文混合字符串 的方法
- [Java] 利用xpdf库获取pdf文件的指定范围文本内容
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- .net c#中获取数据库中指定长度的字段以及字段的前几位
- 获取文件字节数组byte[]
- C#实现 获取指定字节长度 中英文混合字符串 的方法
- C#实现 获取指定字节长度 中英文混合字符串 的方法
- MVC 以内容字节数组形式 实现文件上传 下载
- 探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解
- [Java] 利用xpdf库获取pdf文件的指定范围文本内容