字节操作流:OutputStream,InputStream
字符操作流:Writer,Reader
OutputStream字节输出流
public abstratc class OutputStream
extends Object
implements Closeable,Flushable{}
public interface Closeable
extends AutoCloseable{
public void close() throws IOException;
}
public interface Flushable{
public void flush() throws IOException;
}
OutputStream类常用方法
| 方法 | 描述 |
|---|---|
| public abstract void wirte(int b) throws IOException | 输出单个字节数据 |
| public void wirte(byte[] b) throws IOException | 输出一组字节数据 |
| public void wirte(byte[] b,int off,int len) throws IOException | 输出部分字节数据 |
| public void close() throws IOException | 关闭输出流 |
| public void flush() throws IOException | 刷新缓冲区 |
FIleOutputStream类的主要目的就是为OutputStream父类实例化,以为OutputStream是抽象类
| 方法 | 描述 |
|---|---|
| public FIleOutputStream(File file) throws FileNotFoundException | 采用覆盖的形式创建文件输出流 |
| public FIleOutputStream(File file,boolean append) throws FileNotFoundException | 采用追加的形式创建文件输出流 |
public class javaIODemo {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("f:" + File.separator + "javaIODemo" + File.separator + "message"
+ File.separator + "xiyang.txt");
if(!file.getParentFile().exists()) {
file.mkdirs();
}
//使用AutoCloseable自动关闭接口
//自动关闭输出流
try(OutputStream outputStream = new FileOutputStream(file,true)){
String string = "www.baidu.com";
outputStream.write(string.getBytes());
}
}
}
InputStream字节输出流
| 方法 | 描述 |
|---|---|
| public abstract int read() throws IOException | 读取单个字节数据,读到底返回-1 |
| public int read(byte[] b) throws IOException | 读取一组字节数据,返回的是读取的个数,读到底返回-1 |
| public int read(byte[] b ,int off,int len) throws IOException | 读取一组字节数据(只占数组的一部分) |
| public void close() throws IOException | 关闭输出流 |
| public byte[] readAlllBytes() throws IOException | 读取输入流全部字节数据(JDK1.9之后) |
| public long transferTo(OutputStream out) throws IOException | 输入流转为输出流(JDK1.9之后新增) |
public class javaIODemo {
public static void main(String[] args) throws IOException {
File file = new File("f:" + File.separator + "javaIODemo" + File.separator + "message"
+ File.separator + "xiyang.txt");
if(file.exists()) {
InputStream input = new FileInputStream(file);
byte data[] = new byte[1024];//数据读取缓冲区
int len = input.read(data);
System.out.println("[" + new String(data,0,len) +"]");
input.close();
}
}
}
Writer字符输出流
writer类常用方法:
| 方法 | 描述 |
|---|---|
| public Writer append(CharSequence csq) throws IOException | 追加内容 |
| public void write(char[] cbuf) throws IOException | 输出字符数组 |
| public void write(int c) throws IOException | 输出单个字符 |
| public void wirte(String str) throws IOException | 输出字符串 |
| public abstract void close() throws IOException | 关闭输入流 |
| public abstract void flush() throws IOException | 刷新缓冲区 |
FileWriter类常用方法:
| 方法 | 描述 |
|---|---|
| public FileWriter(File file) throws IOException | 覆盖的形式创建文件输出流 |
| public FileWriter(File file,boolean append) throws IOException | 追加的形式创建文件输出流 |
public class javaIODemo {
public static void main(String[] args) throws IOException {
File file = new File("f:" + File.separator + "javaIODemo" + File.separator + "message"
+ File.separator + "xiyang.txt");
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
Writer out = new FileWriter(file);
out.write("hello world");
out.append(":do a java test");
out.close();//字符流关闭时会自动刷新缓冲区
}
}
Reader字符输入流
| 方法 | 描述 |
|---|---|
| public int read() throws IOException | 读取单个字符,无数据读取时返回-1 |
| public int read(char cbuf) throws IOException | 读取多个字符,并返回读取个数 |
| public long skip(long n) throws IOException | 跳过指定字符个数后读取 |
| public boolean ready() throws IOException | 是否可以开始读取数据 |
| public abstract void close() throws IOException | 关闭输入流 |
public class javaIODemo {
public static void main(String[] args) throws IOException {
File file = new File("f:" + File.separator + "javaIODemo" + File.separator + "message"
+ File.separator + "xiyang.txt");
if(file.exists()) {
Reader in = new FileReader(file);
char []data = new char[1024];
in.skip(3);
int len = in.read(data);
System.out.println(new String(data,0,len));
in.close();
}
}
}
转换流
java.io中提供了两种转换流:OutputStreamWriter和InputStreamReader。
OutputStreamWriter:
public class OutputStreamWriter extends Writer{}
//[构造方法]public OutputStreamWriter(OutputStream out)
InputStreamReader:
public class InputStreamReader extends Reader{}
//[构造方法]public InputStreamReader(InputStream in)
InputStream和Reader的转换
public static void main(String[] args) throws IOException {
File file = new File("f:" + File.separator + "javaIODemo" + File.separator + "message"
+ File.separator + "xiyang.txt");
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
InputStream in = new FileInputStream(file);
Reader reader = new InputStreamReader(in);
char data[] = new char[1024];
int len = reader.read(data);
System.out.println(new String(data,0,len));
reader.close();
in.close();
}
版权声明:本文为qq_45346915原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。