java.io.InputStream;
java.io.FileInputStream;文件字节输入流
以下程序的缺点是,一次只读一个字节,并且效率低,还需要频繁访问磁盘
按照字节方式读取文件
import java.io.*;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream f1 = null;
//1.要读取某文件,首先与这个文件创建一个“输入流”
/*
* 创建方法,看FileInputStream的构造方法
* FileInputStream(String name); 其中name是想读取文件的路径
* */
//文件路径
/*
* String filePath = "temp.txt";//相对路径,相对当前而言,在当前路径下寻找.直接输入文件名
*/
String filePath2 = "D:\\eclispe\\Io流\\src\\fileInputStream\\temp.txt"; // ‘/’在java语言里面有转义字符,所以要用‘//’. 同时使用'\'也是可以的
try {
//如果有汉字的话,一个汉字需要两个字节才能存下
//该方法是需要抛出异常的
f1 = new FileInputStream(filePath2);
int i1=f1.read();//以字节的方式读取,读第一个
int i2=f1.read();//读第二个
System.out.println(i1);
}catch (Exception e){
e.printStackTrace();
}finally {
//为了保证一定会释放,所以在finally语句中执行
if(f1!=null)
{
try {
f1.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
以下是将方法在主函数直接抛出,没有使用try…catch
当文件读到最后没有数据时,将会返回-1
String namePath ="D:\\eclispe\\Io流\\src\\fileInputStream\\temp.txt";
FileInputStream ne = new FileInputStream(namePath);
int tme = 0;
while((tme = ne.read())!=-1)
{
System.out.println(tme);
}
ne.close();
2.int read(byte[] bytes) 该方法返回的时int类型,代表读取的字节数
读取之前在内存中准备一个byte数组,每次读取多个字节存储到byte数组中。一次读取多个字节,不是单字节读取了
这样效率高
备注:实现在文件里写入的是abcdefg
FileInputStream ne1 = new FileInputStream(namePath);
//2.开始读
//准备一个byte数组
byte[] bytes = new byte[3];//代表一次最多读取三个字节
//int read(byte[] bytes);该方法返回的Int类型的值代表的是,这次读取多少个字节
int i1 = ne1.read(bytes); //3
//将byte数组转换成为字符串
System.out.prinln(new String(bytes)); //abc
int i2 = ne1.read(bytes); //3
//将byte数组转换成为字符串
//数组容量只有3,所以新来的3个会覆盖原先的三个
System.out.prinln(new String(bytes)); //def
int i3 = ne1.read(bytes); //1
//将byte数组转换成为字符串
//文档最后只剩一个,会读取一个,覆盖的时候,只覆盖原来的第一个位置
System.out.prinln(new String(bytes)); //gef
int i4 = ne1.read(bytes); //-1
//所有元素读完了,返回的数量为-1
循环读取
//循环读取
while(true) {
int tmp2 = ne2.read(b2);
//将读取的有效数据显示出来
System.out.println(new String(b2,0,tmp2));
//如果文档已经全部读取完毕,则跳出循环
if(tmp2 == -1) {
break;
}
}
//升级循环
while(true){
int tmp = 0;
if((tmp=ne2.read(b2))!=0)
{
System.out.println(new String(b2,0,tmp));
}
}
skip和available方法
//available和skip方法
FileInputStream ne3 = new FileInputStream(namePath);
System.out.println(ne3.available()); //输出7
ne3.read();
//available的作用,返回流中剩余的估计字节数
System.out.println(ne3.available()); //输出6
//跳过两个字节
ne3.skip(2);
System.out.println(ne3.read());//读取跳过两个字节之后的值了
ne3.close();
文件字节输出流
java.io.OutputStream;
java.io.FileOutputStream; 文件字节输出流
将计算机内存中的数据写入硬盘文件中
import java.io.*;
public class FileOutputStreamTest{
public static void main(String[] args) throws Exception {
//1.创建自己输出流
String path ="D:\\IOTest\\test.txt";
FileOutputStream n1 = new FileOutputStream(path);//这种不是追加方式写入,会覆盖之前写入的内容
//2.开始写
String name = "hello world";
//想要将数组写入,先要将字符串打散成byte数组
byte[] bytes = name.getBytes();
//全部写入bytes中的数据
n1.write(bytes);
//将byte数组中的前三个写入文件怎么实现
n1.write(bytes,0,3)
//推荐最后的时候为了保证数据完全写入硬盘,所以要刷新
n1.flush();
n1.close();
//以追加的形式写入数据
//再次运行后,向文件里写入的数据,不会覆盖之前的数据
FileOutputStream n2 = new FileOutputStream(path,true);
n2.write(bytes);
n2.flush();
n2.close();
}
}