javaIO操作之字节输入流--InputStream
2024-04-07 16:15:06
/**
*<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)+"]");
} }
}
最新文章
- [译]Asp.net MVC 之 Contorllers(二)
- HTML5 canvas 捕鱼达人游戏
- Vue 性能优化track-by
- ios-获取通讯录 姓名和电话
- php 面向对象要点汇总
- JavaScript验证正则表达式大全
- 【HDOJ】4348 To the moon
- android应用如何启动另外一个apk应用
- 使用SigbalR发送通知
- Struts2之标签使用
- bootstrap学习笔记之基础导航条 http://www.imooc.com/code/3111
- 老男孩Python全栈开发(92天全)视频教程 自学笔记21
- IIR滤波器软件实现(Matlab+C++)
- You must reset your password using ALTER USER statement before executing this statement.
- IP 协议
- POJ 3126 - Prime Path - [线性筛+BFS]
- (已解决)jdk安装 系统找不到文件C:\ProgramData\Oracle\Java\javapath\java.exe
- android开发(46) 使用 textview实现文字的阴影效果,浮雕效果
- geoserver 常见问题笔记
- android 开发 碎片Fragment布局例子(用按键切换碎片布局)
热门文章
- CSS美化网页元素
- [BZOJ 1079][SCOI 2008]着色方案
- python全栈学习--day11(函数高级应用)
- web服务器学习4---httpd-2.4.29优化
- C语言第九次作业
- 《结对-HTML贪吃蛇游戏项目-测试过程》
- linux 50个常用命令
- 玩转Leveldb原理及源码--拙见1
- 从一次输入框无法输入的bug,谈如何限制输入框输入类型
- $(function(){})和window.onload的区别