如果需要针对于文件的具体内容进行操作,则必须使用两类数据流
- 字节流:OutputStream 、InputStream
- 字符流:Writer 、Reader
1、字节输出流:OutputStream
字节输出流采用的是byte数据类进行的操作处理,主要可以进行二进制数据的操作。如果要进行字节数据的输出操作使用OutputStream类完成,该类定义如下:
public abstract class OutputStream
extends Object
implemets Closeable, Flushable
在OutputStream类中提供有三个write()方法,定义如下:
- 输出整个字节数组的数据:public void write(byte[] b) throws IOException
- 输出部分字节数组的数据:public void write(byte[] b, int off, int len) throws IOException
- 输出单个字节:public abstract void write(int b) throws IOException
OutputStream类是一个抽象类,所以要通过子类进行对象的实例化操作,既然要进行文件的处理,所以可以使用FileOutputStream子类为OutputStream父类实例化;构造方法:
- 构造方法(创建新的):public FileOutputStream(File file) throws FileNotFoundException
- 构造方法(从已有内容后追加):public FileOutputStream(File file, boolean append) throws FileNotFoundException
示例
package cn.pyl.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class regexDemo {
public static void main(String[] args) throws Exception {
// 通过File类设置要输出的文件路径
File file = new File("D:" + File.separator + "JAVA" + File.separator
+ "demo.txt");
// 判断父目录是否存在,不存在则创建
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 通过OutputStream的子类为OutputStream实例化
OutputStream outputStream = new FileOutputStream(file, true);
// 定义要输出的数据并将其输出
String msg = "hello world\r\n";
byte[] data = msg.getBytes();// 将字符串变为字节数组
outputStream.write(data);
outputStream.close();
}
}
2、字节输入流:InputStream
InputStream的定义如下:
public abstract class InputStream
extends Object
implements Closeable
在InputStream类中提供有三个读取的操作方法
读取数据:public int read(byte[] b) throws IOException。向字节数组中读取,返回读取的个数,如果没有数据则返回-1
读取数据:public int read(byte[] b, int off, int len) throws IOException.向字节数组中读取部分数据,返回的是读取的数据数量,如果没有则返回-1
读取数据:public abstract int read() throws IOException。返回单个字节数据,如果没有内容则返回-1
使用FileInputStream实例化
- 构造方法:public FileInputStream(File file) throws FileNotFoundException
示例:实现数据的读取
package cn.pyl.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class regexDemo {
public static void main(String[] args) throws Exception {
// 通过File类设置要输出的文件路径
File file = new File("D:" + File.separator + "JAVA" + File.separator
+ "demo.txt");
// 判断文件是否存在并且是一个文件
if (file.exists() && file.isFile()) {
InputStream inputStream = new FileInputStream(file);
byte data[] = new byte[1024];
int len = inputStream.read(data);
System.out.println("[" + new String(data,0,len) + "]");
inputStream.close();
}
}
}
3、字符输出流(writer )
writer是专门负责字符(char、String)输出的操作,其定义如下:
public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable
Writer依然属于抽象方法,所以如果要想操作文件要使用FileWriter子类。Writer里也提供一组的输出方法:
public void writer(String str) throws IOException
public void write(String str,int off,int len) throws IOException
public void write(char[] cbuf) throws IOException
示例
package cn.pyl.test;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.lang.reflect.Field;
public class regexDemo {
public static void main(String[] args) throws Exception {
// 通过File类设置要输出的文件路径
File file = new File("D:" + File.separator + "JAVA" + File.separator
+ "demo.txt");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
Writer out = new FileWriter(file,true);
out.write("hello world\r\n");
out.close();
}
}
4、字符输入流(Reader)
Reader属于字符的输入流操作类,可以使用子类FileReader实现文件的读取处理,在Reader里面如果要进行文件的读取使用如下方法完成:
public int read(char[] cbuf) throws IOException