FileInputStream 的读取操作
package xinhuiji_day07;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class TestFileInputStream {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 1 ,先定义要操作的文件
String str = File.separator; //得到当前系统的盘符
//String path = File.separator+"home"+File.separator+"han"+File.separator+"FileOutPutStream.txt";
String path = str+"home"+str+"han"+str+"FileOutPutStream.txt";
File file = new File(path);
// 2,通过子类实例化父类
InputStream in = null;
in = new FileInputStream(file);
//byte[] bytes = new byte[1024];//直接这样开辟空间的话,我们并不知道file的实际空间是多大,会造成占用大量内存的
//的结果,我们可以通过File类先获得file的大小,再定义数组的大小
byte[] bytes = new byte[(int) file.length()]; // 定义数组用于存储读到的数据
// 3 读数据
//第一种方法
// int len = in.read(bytes);
// System.out.println("向数组中写入的字符长度:"+len);
//第二种方法
for(int i = 0;i<file.length();i++){
bytes[i] = (byte) in.read();
}
//第三种方法
// String info = new String(bytes,0,len);
String info = new String(bytes);
System.out.println(info);
// 4 。关闭输入流
in.close();
}
}
/////////////////////////////////////////////////////////////////////////////
//第三种方法 。前两种方法都是知道file的长度,如果不知到file的长度就应该使用read()方法
byte[] bytes = new byte[1024];
int len = 0;//用于记录读到的字符个数
int temp = 0;//用于记录in.read()的返回值
//read()方法只有在读到文件末尾时返回值为-1
while((temp = in.read()) != -1){
bytes[len] = (byte) temp;
len++;
}
FileInputStream 的读取操作的更多相关文章
- java 的文件读取操作
/** * @param filePath 文件的全路径 * 返回我们读取到的文件内容 * **/ public static String readFile(String filePath) { F ...
- JAVA之旅(二十六)——装饰设计模式,继承和装饰的区别,LineNumberReader,自定义LineNumberReader,字节流读取操作,I/O复制图片
JAVA之旅(二十六)--装饰设计模式,继承和装饰的区别,LineNumberReader,自定义LineNumberReader,字节流读取操作,I/O复制图片 一.装饰设计模式 其实我们自定义re ...
- Android设备与外接U盘实现数据读取操作
现在越来越多手机支持OTG功能,通过OTG可以实现与外接入的U盘等USB设备实现数据传输.关于OTG,可以参考: http://blog.csdn.net/srw11/article/details/ ...
- Java 实现Excel的简单读取操作
JAVA实现Excel表单的简单读取操作 实现Excel表单的简单读取操作,首先要导入相关的jar包: 如图所示: 此处贴上代码: public static List<List<Stri ...
- access数据库频繁读取操作会出现 System.Data.OleDb.OleDbException 的异常解决
asp.net access数据库 本来想着打开一个access数据库连接后,不关闭,下次操作数据了,直接拿来用,谁知道连着测试64次后(大概这么多次),就会出现System.Data.OleDb.O ...
- Perl中文件读取操作
Perl中文件读取操作 http://blog.csdn.net/yangxuan12580/article/details/51506216
- Java 对文件的读取操作
package pack; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...
- properties配置文件读取操作总结【java笔记】
声明:本文所有例子中的 properties 文件均放在 src 目录下,ecclipse 软件自动增加 一.基本概念 1.1 properties文件,存储格式 键=值. properties文件 ...
- java:OutputStream和InputStream 输出输入流,FileOutputStream,FileInputStream写入读取流
1.在java中stream代表一种数据流(源),javaio的底层数据元,---(想像成水龙头)2.任何有能力产生数据流(源)的javaio对象就可以看作是一个InputStream对象既然它能产生 ...
随机推荐
- [LeetCode] String to Integer (atoi) 字符串
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- android基本控件学习-----ProgressBar
ProgressBar(进度条)讲解 一.常用属性和基础使用实例 (1)常用属性: android:max:进度条的最大值 android:progress:进度条已完成进度值 android:pro ...
- linux下终端录制
主要是以下三步: 一.安装软件:curl -sL https://asciinema.org/install | sh 二.录制终端:asciinema rec filename 三.回放终端:asc ...
- LeetCode OJ-- Sudoku Solver ***
https://oj.leetcode.com/problems/sudoku-solver/ 九宫格数独问题. 一行上为1 2 3 到9 一列上为1 2 3 到9 每个小的3*3格子为 1 2 3 ...
- 2018 L2-027. 名人堂与代金券【结构体排序】
L2-027. 名人堂与代金券 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对于在中国大学MOOC(http://www.i ...
- Algorithm | Sort
Bubble sort Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algo ...
- Codeforces 734 F Anton and School
Discription Anton goes to school, his favorite lessons are arraystudying. He usually solves all the ...
- 聊聊、Zookeeper Windows启动
Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly rel ...
- dedecms调用新闻文章列表
效果如下: 代码如下: <div class="list"> <ul class="d6 ico4"> {dede:list pages ...