从指定文件(字节数组)获取内容以及获取长度
来源:互联网 发布:淘宝垃圾短信 编辑:程序博客网 时间:2024/05/29 03:27
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);}}
0 0
- 从指定文件(字节数组)获取内容以及获取长度
- 获取文件字节数组byte[]
- java从指定路径获取指定文件
- OpenCL获取文件长度及内容
- 获取字符串长度、字符长度、字节长度
- 获取字符串长度、字符长度、字节长度
- Python 获取文件指定行内容
- js substring从右边获取指定长度字符串
- 如何获取NSString的字符长度以及如何获取指定字符长度字符的字符串
- 1.实验 5.2.5 文件定位 用lseek()函数实现以下功能 1. 获取文件大小 2. 为文件添加指定长度的空洞 3. 在指定位置写入指定内容 4. 读出指定位置的内容 1. 获取文件大小
- java获取字节的长度
- java获取字节的长度.
- C++ vector二维数组初始化以及获取数组长度
- 从文件中获取指定字符串源码
- python 从指定目录获取文件
- Java 从网页指定URL获取文件
- 获取指定网页内容
- java按照字节长度获取字符串长度
- 筛选菜单
- linux命令大全——文档编辑
- markDown学习笔记
- 【9931】火车票
- Eclipse C++出现Launch failed,Binary not found问题安装MinGW编译器解决方案(Win7)
- 从指定文件(字节数组)获取内容以及获取长度
- 大数加法
- 前端技术-布局解决方案
- 关于前端面试
- Tomcat运行状态下 修改Velocity嵌套的页面时报 java.lang.OutOfMemoryError: PermGen space及其解决方法
- 合法性检查
- caffe编译问题汇总
- 4.4之后无法在外置SD卡根目录创建文件夹
- linux 下打开162端口命令