输入/输出流的分类
类型 |
字节流 |
字符流 |
输入流 |
InputStream |
Reader |
输出流 |
OutputStream |
Writer |
- 字节流:最原始的流,读出来就是0101……
- 字符流:一个字符一个字符读,一个字符为两个字节
- 节点流:从一个特定的数据源(节点)读写数据(如:文件、内存)
- 处理流:当我们在读取数据时,需要进行一些处理,通过对数据的处理为程序提供更为强大的读写功能,然后在返回给我们,可以理解为一根管道,在管道上嵌套一个或多个管道
InputStream的基本用法
//读取一个字节并以整数的形式返回(0~255),
//如果返回-1已到输入流的末尾
int read() throws IOException
//读取一系列字节并存储到一个数组buffer,
//返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
//字节数组,将这个数组装满数据在进行处理 ,以减少对硬盘的访问次数
int read(byte[] buffer) throws IOException
//读取length个字节
//并存储到一个字节数组buffer,从length位置开始
//返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
int read(byte[] buffer,int offset,int length) throws IOException
//关闭流释放内存资源
void close() throws IOException
//跳过n个字节不读,返回实际跳过的字节数
OutputStream的基本用法
//向输出流中写入一个字节数据,该字节数据为参数b的第8位
void writer(int b) throws IOException
//将一个字节类型的数组中的数据写入输出流
void write(byte[] b) throws IOException
//将一个字节类型的数组中的从指定位置(off)开始的
//len个字节写入到输出流
void write(byte[] b,int off,int len) throws IOException
//关闭流释放内存资源
void close() throws IOException
//将输出流中缓冲的数据全部写出到目的地
void flush() throws IOException
Reader的基本用法
//读取一个字符并以整数的形式返回(0~255),
//如果返回-1已到输入流的末尾
int read() throws IOException
//读取一系列字符并存储到一个数组buffer
//返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
int read(char[] cbuf) throws IOException
//读取lenght个字符
//并存储到一个数组buffer,从length位置开始
//返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
int read(char[] cbuf,int offset,int length) throws IOException
//关闭流释放内存资源
void close() throws IOException
//跳过n个字符不读,返回实际跳过的字节数
long skip(long n) throws IOException
Writer的基本用法
//向输出流中写入一个字符数据,该数据为参数b的第16位
void write(int c) throws IOException
//将一个字符类型的数组中的数据写入输出流
void write(char[] cbuf) throws IOException
//将一个字符类型的数组中的从指定位置(offset)开始的
//length个字符写入到输出流
void write(char[] cbuf,int offset,int length) throws IOException
//将一个字符串中的字符写入到输出流
void write(String string) throws IOException
//将一个字符串从offset开始的length个字符写入到输出流
void write(String string.int offset,int length) throws IOException
//关闭流释放资源
void close() throws IOException
//将输出流中缓冲的数据全部写出到目的地
void flush() throws IOException
节点流类型:字节的输入/输出流
类型 |
字节流 |
字符流 |
File(文件) |
FileInputStream/FileOutputStream |
FileReader/FileWriter |
Memory Array |
ByteArrayInputStream/ByteArrayOutputStream |
CharArrayReader/CharArrayWriter |
Memory String |
- |
StringReader/StringWriter |
Pipe(管道) |
PipedInputStream/PipedOutputStream |
PipedReader/PipedWriter |
通过具体的例子了解流
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestFileInputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
try {
in = new FileInputStream("/home/root1/Desktop/TestFileInputStream.java");//读取文件内容,如果文件中有中文,读出来的中文都是'?' 可以使用Reader来读字符
} catch (FileNotFoundException e){
//如果没有此文件则退出
System.out.println("找不到指定文件");
System.exit(-1);
}
//有,继续执行
try {
long num = 0;//计数,记录读取多少个字节
while ((b=in.read())!=-1){//所有内容读取完后值为-1
System.out.println((char)b);//这里强制转换为char类型
num++;
}
in.close();//关闭流
System.out.println();
System.out.println("共读取了"+num+"字节");
}catch (IOException e1){
System.out.println("文件读取错误");
System.exit(-1);
}
}
}
package com.site.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//
public class TestFileOutputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in =null;
FileOutputStream out = null;
try{
in = new FileInputStream("/home/root1/Desktop/TestFileInputStream.java");
out = new FileOutputStream("/home/root1/Desktop/ok.java");//写入到此目录下,如果没有就创建这个文件
while ((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件");
} catch (IOException e1){
System.out.println("文件复制错误");
System.exit(-1);
}
System.out.println("文件已复制");
}
}
package com.site.file;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TestFileReader {
public static void main(String[] args) {
FileReader fr = null; // 定义FileReader
int c = 0;
try{
fr = new FileReader("/home/root1/Desktop/test.java");//读取的文件路径
int ln = 0;
while((c = fr.read())!= -1) {//等于-1时文件读取完毕
System.out.print((char)c);//强转为char类型
}
fr.close();
} catch (FileNotFoundException e){
System.out.println("找不到指定文件");
} catch (IOException e){
System.out.println("文件读取错误");
}
}
}
package com.site.file;
import java.io.FileWriter;
import java.io.IOException;
public class tFileWriter {
public static void main(String[] args) {
FileWriter fw = null;
try{
fw = new FileWriter("/home/root1/Documents/新建文本.txt");//如果没有这个文件自动创建,但不可以创建目录
for (int i = 0; i <=50000 ; i++) {
fw.write(i);//将所有字符全部写入
}
System.out.println("写入完毕");
fw.close();
}catch (IOException e){
e.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);
}
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。