简介:
如果要进行输入输出操作一般会按照如下步骤完成:(以文件操作为例)
- 通过File类定义一个要操作文件的路径;
- 通过字节流或字符流的子类对象为父类对象实例化;
- 进行数据的读(输入)写(输出)操作;
- 数据流属于资源操作,资源操作必须关闭;
输出流:OutputStream:
OutputStream 实现了两个接口:Closeable、Flushable;
在OutputStream类里面一共有3个输出方法:
- 输出单个字节:public abstract void write(int b) throws IOException; (传入的是bype)
- 输出全部字节数组:public void write(byte[] b) throws IOException;
- 输出部分字节数组:public void write(byte[] b,int off,int len) throws IOException;
OutputStream 类是一个抽象类;如果需要为抽象类进行对象实例化操作,需要抽象类的子类;因为是文件操作,可以使用FileOutputStream子类;在这个子类中有如下构造方法:
- 创建或覆盖已有文件:public FileOutputStream(File file) throws FileNotFoundException;
- 文件内容追加:public FileOutputStream(File file,boolean append) throws FileNotFoundException;
public class test {
public static void main(String args[]) throws IOException {
// 定义要输出文件的路径
File file = new File("D:" + File.separator + "demo" + File.separator + "test.txt");
// 判断该目录是否存在
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 使用OutputStream和其子类进行对象实例化,此时目录存在而文件不存在;
OutputStream output = new FileOutputStream(file);
// 进行文件内容的输出
String str = "hello";
byte data[] = str.getBytes();// 将字符串变为字节数组;
output.write(data);
output.close();
}
}
在程序中如果想实现换行,使用 \r\n 来实现;
输入流:InputStream:
InputStream实现了接口:Closeable,不需要考虑该接口的存在:
读取数据的方法:
- 读取单个字节:public abstract int read() throws IOException;
返回值:返回读取的字节内容,如果现在已经没有内容;
- 将读取的数据保存在字节数组里:public int read(byte[] b) throws IOException;
返回值:返回读取的数据长度;如果已经读取到结尾了,返回-1;
- 将读取的数据保存在部分字节数组里:public int read(byte[] b,int off,int len) throws IOException;
返回值:返回读取的部分数据长度;如果已经读取到结尾了,返回-1;
//向数组里面读取数据
public class test {
public static void main(String args[]) throws IOException {
File file = new File("D:" + File.separator + "demo" + File.separator + "test.txt");
if (file.exists()) {
InputStream input = new FileInputStream(file);
byte data[] = new byte[1024];
int len = input.read(data);
input.close();
System.out.println("{" + new String(data, 0, len) + "}");
}
}
}
字符输出流:Writer:
Writer实现了3个接口:Closeable、Flushable、Appendable(定义了追加,追加的都是字符串或者字符);
输出方法(部分):
- 输出全部字符数组:public void write(char[] cbuf) throws IOException;
- 输出字符串:public void write(String string) throws IOException;
public class test {
public static void main(String args[]) throws IOException {
File file = new File("D:" + File.separator + "demo" + File.separator + "test.txt");
if (!file.exists()) {
file.getParentFile().mkdirs();
}
Writer out = new FileWriter(file);
String str = "hello world!";
out.write(str);
out.close();
}
}
字符输入流:Reader:
读取内容到字符数组:public int read(char[] cbuf) throws IOException;
返回值:返回读取的数据长度;如果已经读取到结尾了,返回-1;
public class test {
public static void main(String args[]) throws IOException {
File file = new File("D:" + File.separator + "demo" + File.separator + "test.txt");
if (!file.exists()) {
file.getParentFile().mkdirs();
}
Reader in = new FileReader(file);
char data[] = new char[1024];
int len = in.read(data);
in.close();
System.out.println(new String(data, 0, len));
}
}
字节流与字符流的区别?
最大区别:字节流直接与终端进行数据交互,而字符流需要将数据经过缓冲区之后才可以进行输入输出;
如果使用OutputStream输出的数据时,即使没有关闭输出流,也可以正常输出;但是如果使用字符输出流,如果不关闭,那么表示在缓冲区的内容不会被强制性的清空,所以不会输出数据,如果有特殊情况不能关闭字符输出流,可以使用flush()方法,强制清空缓冲区;