字节流:
主要有InputStream和OutputStream作为基类
字符流:
主要有Reader和Writer作为基类
字节流跟字符流的用法几乎一样,区别在两者操作数据单元不同,字节流操作数据单元是8位的字节,字符流操作16位的字符。
Java的IO流设计40多类,基本都是从以上4个抽象基类派生的。
开发步骤:
1):创建字节文件输入流对象
2)读数据
3)释放资源
文件的字节输入流(InputStream)public FileInputStream(String name)
方法:
1)public int read():从输入流中读取一个字节
2)public int read(byte[] b):一次读取一个字节数组,最多读取b.length个字节的数据并存储在字节数组b中
3)public int read(byte[] b, int off, int len):从输入流中最多读取len个字节的数据,并存储在数组b中。每次的存储都是从off位置开始的,返回实际读取的字节长度
文件的字节输出流(OutputStream)public FileOutputStream(String name)
方法:
1)public void write():写入一个字节到输出流中
2)public void write(byte[] b/char[] buf):一次写入一个字节数组,最多写入b.length个字节的数据输出到输出流中
3)public void write(byte[] b/char[] buf, int off, int len):从off位置开始的,实际读取的字节长度的字节数组或者字符数组,最多输出len个字节的数据到输出流中。
package Day18_Input_output;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Aoman_Hao
*/
public class InOutTest1 {
public static void main(String[] args) throws IOException {
FileInputStream fis_1 = new FileInputStream("d:\\Java\\JavaTest\\Aoman_Hao.txt");
FileInputStream fis_2 = new FileInputStream("d:\\Java\\JavaTest\\Aoman_Hao.txt");
FileOutputStream fos_1 = new FileOutputStream("d:\\Java\\Aoman_1.txt");
FileOutputStream fos_2= new FileOutputStream("d:\\Java\\Aoman_2.txt");
//第一种方法:一个字节一个字节读取
int len=0;
while((len=fis_1.read())!=-1){
//显示数据
// System.out.print(len);//打印数据对应的Ascii码表
System.out.print((char)len);
//输出(写)数据
fos_1.write(len);
}
System.out.println("\r\n"+"------------------------");
//第二种方法:字符数组
byte[] bys=new byte[1024];
int lens=0;
while((lens=fis_2.read(bys))!=-1){
System.out.println(new String(bys,0,lens));
fos_2.write(bys);
}
//关闭流,节省资源
fos_1.close();
fos_2.close();
fis_1.close();
fis_2.close();
}
}
输出:
123
qwerasdf
??°???????
?¨??{}
------------------------
123
qwerasdf
我爱刘诗诗
(){}
注:一个中文字符用两个字节表示,如果用单字节去读取的话,虎出现乱码的现象。
字符输入流(Reader)
方法:
1)public int read():从输入流中读取一个字节
2)public int read(char[] cbuf):一次读取一个字节数组,最多读取b.length个字节的数据并存储在字符数组cbuf中
3)public int read(char[] cbuf, int off, int len):从输入流中最多读取len个字节的数据,并存储在字符数组cbuf中。每次的存储都是从off位置开始的,返回实际读取的字节长度
字符输出流(write)
方法:
1)public void write():写入一个字节到输出流中
2)public void write(byte[] b/char[] buf):一次写入一个字节数组,最多写入b.length个字节的数据输出到输出流中
3)public void write(byte[] b/char[] buf, int off, int len):从off位置开始的,实际读取的字节长度的字节数组或者字符数组,最多输出len个字节的数据到输出流中。
package Day18_Input_output;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
/**
* @author Aoman_Hao
*/
public class ReaderWrite {
public static void main(String[] args) throws IOException{
try {
FileReader fr = new FileReader("d:\\Java\\JavaTest\\Aoman_Hao.txt");
FileOutputStream fos = new FileOutputStream("d:\\Java\\\\JavaTest\\Aoman_3.txt");
char[] chs = new char[1024];
int len = 0;
while((len=fr.read(chs))!=-1){
System.out.println(new String(chs, 0, len));
fos.write(len);
}
fr.close();
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
输出:
123
qwerasdf
我爱刘诗诗
(){}
高效输出:
字节缓冲输入流:BufferedInputStream
字节缓冲输出流:BufferedOutputStream
构造方式:
public BufferedInputStream(InputStream out):默认缓冲区大小
public BufferedOutputStream(OutputStream out):默认缓冲区大小
flush():刷新缓冲区的流,强迫将缓冲字节都写到该流中,刷新之后是可以写数据的.
close():关闭流对象所指向指向的这个资源文件,一旦关闭,写数据,写不进去的!
package Day18_Input_output;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Aoman_Hao
*/
public class BufferedInput_Output {
public static void main(String[] args) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"d:\\Java\\JavaTest\\Aoman_Hao.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
"d:\\Java\\\\JavaTest\\Aoman_3.txt"));
byte[] bys = new byte[1024];
int len = 0;
while((len=bis.read(bys))!=-1){
bos.write(bys, 0, len);
bos.flush();//缓冲输出流在close前必须有flush刷新这一步
}
bos.close();
bis.close();
}
}
便捷类:
字符转换输入:FileReader(String Filename)
字符转化输出:FileWriter(String Filename)
package Day18_Input_output;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author Aoman_Hao
*/
public class SimpleInPut_Output {
public static void main(String[] args) throws IOException{
//便捷类
FileReader fr = new FileReader("d:\\Java\\JavaTest\\Aoman_Hao.txt");
FileWriter fw = new FileWriter("d:\\Java\\\\JavaTest\\Aoman_3.txt");
char[] ch = new char[1024];
int len =0;
while((fr.read(ch))!=-1){
fw.write(ch, 0, len);
fw.flush();
}
fw.close();
fr.close();
}
}