/**
*<li> InputStream类中定义的方法:
* <li>读取的数据保存在字节数组中,返回读取的字节数组的长度:public int read(byte[] b) throws IOException ;
* <li>读取部分数据保存在字节数组中,返回读取数据的长度:public int read(byte[] b,int off,int len)throws IOException ;
* 如果文件内容读取到结尾字返回-1;
*/
package com.java.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class TestDemo {
public static void main(String args[]) throws Exception{
//设置文件路径
File fl = new File("e:"+File.separator+"hello" + File.separator+"demo" +File.separator+"java.txt" );
if(fl.exists()){ //文件存在
InputStream in = new FileInputStream(fl) ;
byte data[] = new byte[1];
int len = in.read(data) ;//将读取的内容保存在字节数组中,并且返回字节长度
System.out.println("【"+new String(data,0,len) +"】");
} }
}

重要的实现方式:public abstract int read() throws IOException ;

/**
*<li>读取单个字节,如果读取到最后则返回-1: public abstract int read()throws IOException ;
*/
package com.java.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class TestDemo {
public static void main(String args[]) throws Exception{
//设置文件路径
File fl = new File("e:"+File.separator+"hello" + File.separator+"demo" +File.separator+"java.txt" );
if(fl.exists()){ //文件存在
InputStream in = new FileInputStream(fl) ;
byte data[] = new byte[1024];
int foot = 0 ;
int temp = 0 ;
while((temp = in.read()) != -1){ //说明存在数据
data[foot ++] = (byte)temp;//将读取的数据保存在字节数组中
}
in.close();
System.out.println("[" + new String(data,0,foot)+"]");
} }
}

最新文章

  1. [译]Asp.net MVC 之 Contorllers(二)
  2. HTML5 canvas 捕鱼达人游戏
  3. Vue 性能优化track-by
  4. ios-获取通讯录 姓名和电话
  5. php 面向对象要点汇总
  6. JavaScript验证正则表达式大全
  7. 【HDOJ】4348 To the moon
  8. android应用如何启动另外一个apk应用
  9. 使用SigbalR发送通知
  10. Struts2之标签使用
  11. bootstrap学习笔记之基础导航条 http://www.imooc.com/code/3111
  12. 老男孩Python全栈开发(92天全)视频教程 自学笔记21
  13. IIR滤波器软件实现(Matlab+C++)
  14. You must reset your password using ALTER USER statement before executing this statement.
  15. IP 协议
  16. POJ 3126 - Prime Path - [线性筛+BFS]
  17. (已解决)jdk安装 系统找不到文件C:\ProgramData\Oracle\Java\javapath\java.exe
  18. android开发(46) 使用 textview实现文字的阴影效果,浮雕效果
  19. geoserver 常见问题笔记
  20. android 开发 碎片Fragment布局例子(用按键切换碎片布局)

热门文章

  1. CSS美化网页元素
  2. [BZOJ 1079][SCOI 2008]着色方案
  3. python全栈学习--day11(函数高级应用)
  4. web服务器学习4---httpd-2.4.29优化
  5. C语言第九次作业
  6. 《结对-HTML贪吃蛇游戏项目-测试过程》
  7. linux 50个常用命令
  8. 玩转Leveldb原理及源码--拙见1
  9. 从一次输入框无法输入的bug,谈如何限制输入框输入类型
  10. $(function(){})和window.onload的区别