字节流
字节流主要是操作byte类型数据,也byte数组为准,主要操作类就是
* 字节输出流:OutputStream
* 字节输入流:InputStream
在使用字节流之前,我们要先知道输出输入到底是什么
* 什么叫输出?
程序—>文件
* 什么叫输入?
文件—>程序
输入输出的参照物是自己的程序
* 读取/写入文件流程
* 1.绑定数据源文件(要读哪个文件)
* 2.使用read/write方法读/写
* 3.关闭资源
字节输出流:OutputStream
OutPutStream是抽象类,是所有输出流的父类,如果想要使用此类的话,首先必须通过子类实例化对象,如果操作的是一个文件,则可以使用FileOutputStream类
OutputStream类中的常用方法:
方法 描述
public void close() throws IOException 关闭输出流
public void flush() throws IOException 刷新缓冲区
public void write(byte[] b) throws IOException 将一个byte数组写入数据流
public void write(byte[] b,int off,int len)throws IOException 将一个指定范围的byte数组写入数据流
public abstract void write(int b) throws IOException 将一个字节数据写入数据流
代码示例:创建文件并写入字符
public static void fun1() throws FileNotFoundException, IOException {
File file = new File("/Users/lanou/Desktop/haha/wang.txt");
FileOutputStream oStream = new FileOutputStream(file);
// 按ASCII写入
oStream.write(100);
// 按字节数组写入
byte[] b = { 65, 66, 67, 68 };
oStream.write(b);
// 按字节数组的索引和长度写入
oStream.write(b, 1, 2);
// 简单写法
oStream.write("Hello".getBytes());
oStream.write("World".getBytes());
oStream.close();
}
注:
1、 在操作的时候如果文件本身不存在,则会为用户自动创建新文件。
2、 如果要追加的内容需要换行,则在内容中加入“/t”(windows中是\r\n )就可以了。
以上的操作在写入数据之后,文件之前的内容已经不存在了,因为在IO操作中默认的情况是将其进行覆盖的,如果现在想执行追加的功能,则必须设置追加的操作,此时可以通过FileoutputStream向文件中追加内容:
public static void fun2() throws FileNotFoundException, IOException {
/*
* 方法的续写
*/
File file = new File("/Users/lanou/Desktop/haha/long.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("hello".getBytes());
oStream.close();
}
在构造方法中,如果将append的值设置为true,则表示在文件的末尾追加内容。
字节输入流:InputStream
和OutputStream一样,InputStream本身也是一个抽象类,必须依靠其子类,如果现在是从文件中读取,子类肯定是FileInputStream。
InputStream类的常用方法:
方法 描述
public int available() throws IOException 可以取得输入文件的大小
public void close() throws IOException 关闭输入流
public abstract int read() throws IOException 读取内容,以数字的方式读取
public int read(byte[] b) throws IOException 将内容读到byte数组之中,同时返回个数
示例代码:
public static void fun1() throws FileNotFoundException, IOException {
File file =new File("/Users/lanou/Desktop/haha/www.txt");
FileInputStream iStream=new FileInputStream(file);
// 读取
char r1 = (char)iStream.read();
char r2 = (char)iStream.read();
char r3 = (char)iStream.read();
//当读取到文件末尾的时候会返回-1
//相当于你读到-1文件就读完了
int r4 = iStream.read();
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
System.out.println(r4);
iStream.close();
}
public static void fun2() throws FileNotFoundException, IOException {
//根据文件长度读取
File file =new File("/Users/lanou/Desktop/haha/www.txt");
FileInputStream iStream=new FileInputStream(file);
for(int i =0;i<file.length();i++) {
char read = (char)iStream.read();
System.out.print(read);
}
iStream.close();
}
public static void fun3() throws FileNotFoundException, IOException {
//使用字节数组,读取文件
File file =new File("/Users/lanou/Desktop/haha/www.txt");
FileInputStream iStream=new FileInputStream(file);
//声明一个长度为2的空的字节数组
//读取时会把读出来的内容放进字节数组中存储
byte[] b=new byte[2];
iStream.read(b);
System.out.println(new String(b));
iStream.read(b);
System.out.println(new String(b));
iStream.close();
}