流是什么
流是一种抽象概念,它代表了数据的无结构化传递。按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列。从流中取得数据的操作称为提取操作,而向流中添加数据的操作称为插入操作。用来进行输入输出操作的流就称为IO流。换句话说,IO流就是以流的方式进行输入输出
流的分类
字节流 | 字符流 | |
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
File类的概述
常用的构造方法public File(String path)
public boolean createNewFile () 创建文件public boolean mkdir () 创建单文件夹public boolean mkdirs () 多级文件夹创建public boolean delete () 删除public boolean renameTo ( File dest )) 重命名功能public boolean isDirectory () 是否是文件夹public boolean isFile () 是否是文件public boolean exists () 是否存在
字节输入流
字节输入流InputStream抽象类,不能实例化
构造方法:
FileInputStream(String name) throws FileNotFoundException 创建一个字节输入流对象,参数就是指定要读取的文件的路径
读数据的方式:
方式1:public int read() throws IOException :一次读取一个字节,返回的实际的字节数
方式2:一次读取一个字节数组public int read(byte[] b)throws IOException
FileInputStream fis = new FileInputStream("a.txt") ;//读取当前项目下的a.txt文件
int by = 0 ;
while((by=fis.read())!=-1){
//一直读,展示数据
System.out.print((char)by); //将字节数--->字符
}
//一次读取一个字节数组,效率高
byte[] bytes = new byte[1024] ;
int len = 0 ;//实际字节是从0开始
while((len=fis.read(bytes))!=-1){
//一直读,每次是从0角标开始读取实际字节数,构造成字符串展示
System.out.println(new String(bytes,0,len));
}
字符转换输入流:字节输入流转换字符输入的桥梁,InputStreamReader是Reader的字符流的具体类
构造方法 InputStreamReader(InputStream in)
public class InputStreamReaderDemo {
public static void main(String[] args) throws Exception {
//不带字符集格式的这个构造方法,默认就是平台字符集
InputStreamReader isr = new InputStreamReader(
new FileInputStream("a.txt")) ;
char[] chs = new char[1024] ;
int len = 0 ; //实际字符数
while((len=isr.read(chs))!=-1){
//展示控制台上
System.out.println(new String(chs,0,len));
}
//释放资源
isr.close();
}
}
字节缓冲输入流BufferedInputStream不能直接操作文件,操作文件需要底层字节流
构造方法
public BufferedInputStream(InputStream in)
public class BufferedInputStreamDemo {
public static void main(String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("c.txt")) ;
byte[] bytes = new byte[1024] ;
int len = 0 ;//实际字节数
while((len=bis.read(bytes))!=-1){
//输出内容
//每次从0开始将字节数转换成字符串
System.out.println(new String(bytes,0,len));
}
//释放资源
bis.close();
}
}
字节输出流
创建一个字节输出流对象FileOutputStream(File file)throws FileNotFoundException
写数据的方法
public void write(byte[] bytes)
public void write(byte[] bytes,int offset,int length)
public void write(int n)
byte[] bytes = {97,98,99,101,109} ;
fos.write("hello,world".getBytes());
fos.write(bytes,2,2);
fos.write(101);
for(int x = 0 ; x <100;x++){
fos.write(("hello"+x).getBytes());
}
以上写法会覆盖原来的数据内容
在源文件上追加内容的写法
FileOutputStream fos = null;
try {
//创建字节输出流对象FileOutputStream
//FileOutputStream fos = new FileOutputStream("a.txt") ;
//ublic FileOutputStream(String name,boolean append)throws FileNotFoundException
fos = new FileOutputStream("a.txt",true);
//循环写入
for(int x = 0 ; x < 15 ; x ++){
fos.write(("hello"+x).getBytes()); //写数据
//每次写一个换行一个
fos.write(("\r\n").getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字符转换输出流:OutputStreamWriter :字节输出流通向字符输出流的桥梁
构造方法:public OutputStreamWriter(OutputStream out)
public OutputStreamWriter(OutputStream out,String charsetName)
写入内容的方法:
void write(char[] buf):写入字符数组
abstract void write(char[] cbuf, int off, int len) :写字符数组的一部分
void write(String str):写入字符串
public class OutputStreamWriterDemo {
public static void main(String[] args) throws Exception {
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("b.txt")) ;//创建字符转换输出流对象
String s = "这是个测试,哈哈哈" ;
osw.write(s);
osw.flush();//刷新
osw.close();//释放资源
}
}
字节缓冲输出流BufferedOutputStream
构造方法
public BufferedOutputStream(OutputStream out)
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("d.txt")) ;//创建默认缓冲区大小的字节缓冲流对象
bos.write("hello,字节缓冲流".getBytes());//写数据
//释放资源
bos.close();
}
}
字符缓冲输入流BufferedReader高效读取
构造方法
BufferedReader(Reader in)
public String readLine() throws IOException一次读取一行内容,它如何判断文件是否到达末尾呢?返回值为null,表示文件读取完毕
public class BufferedReaderDemo {
public static void main(String[] args) throws Exception {
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw.txt")) ;
String line = null ;
while((line= br.readLine())!=null){
//判断,赋值,调用方法一块用 readLine()也是阻塞式方法,只要没有null值,它是一直读
System.out.println(line);
}
//释放资源
br.close();
}
}
字符缓冲输入流BufferedWriter高效写入
构造方法:
BufferedWriter(Writer out) :创建默认缓冲区大小的字符缓冲输出流
写入方法:
write(String str)
public void newLine() throws IOException :写入一个行终止符号,就是换行
public class BufferedWriterDemo {
public static void main(String[] args) throws Exception {
//BufferedWriter(Writer out) :创建默认缓冲区大小的字符缓冲输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt")) ;
//写入字符串
bw.write("hello");
// public void newLine() throws IOException :写入一个行终止符号,就是换行
bw.newLine();
bw.write("world");
bw.newLine();
bw.write("javaEE");
bw.newLine();
//刷新
bw.flush();
//释放资源
bw.close();
}
}