输入 input :输入到java程序
输出 output :java程序输出
分类:
按照传输内容分:字节流、字符流
按照传输方向分:输入流、输出流
主要是传输字节(byte)数据,例如:音视频、图片等二进制文件
InputStream 是 字节输入流的所有类的超类。是抽象类
FIleInputStream 是 字节输入流的常用类
构造方法:
演示:
public void testConstructor() throws FileNotFoundException{
//构造方法1 : 传入String类型的文件路径
FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
//构造方法2 :传入File类型的文件对象参数
FileInputStream fis2 = new FileInputStream(new File("E:/A/B2/a.txt"));
}
常用方法:
public int read([byte[] b[, int off, int len]]) throws IOException :
读取字节:
演示:
//读取一个字节
public void testInput() throws IOException {
FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
//读取一个字节
int b1 = fis1.read();
System.out.println((char)b1);
//读取一个字节
int b2 = fis1.read();
System.out.println((char)b2);
//读取一个字节
int b3 = fis1.read();
System.out.println((char)b3);
}
public void testInputPlus() throws IOException {
FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
try{
while((int b = fis1.read()) != -1){
System.out.print((char)b);
}
}finally{
fis1.close();//关流
}
}
//读取字节数组
public void testInputArray() throws IOException {
FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
//定义一个数组,长度是文件字节大小,如果长度小于字节大小时,每次读入数组长度
byte[] arr = new byte[10];
try{
//int num = fis1.read(byte[] b);num是字节的个数
while((int b = fis1.read(arr)) != -1){
System.out.print("读入的字节个数为:" + b);
}
System.out.println(Array.toString(arr));
}finally{
fis1.close();//关流
}
}
public void testInputArrayPlus() throws IOException {
FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
byte[] arr = new byte[3];
try{
while((int b = fis1.read(arr,0,arr.length)) != -1){
System.out.print("读入的字节个数为:" + b);
}
System.out.println(Array.toString(arr));
}finally{
fis1.close();//关流
}
}
public void testAvailable() throws IOException {
FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
int available = fis1 .available();//返回文件字节数
System.out.println(available);
}
OutputStream 输出字节流,抽象类.
常用实现类 FileOutputStream 字节输出流.
构造方法:
注意:
public void testConstructor() throws FileNotFoundException{
//构造方法1
FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt",true);
//构造方法2
FileOutputStream fos2 = new FileOutputStream((new File("E:/A/B2/a.txt"));
}
常用方法:
public void write(int b || [byte[] b[, int off, int len]]) throws IOException :
public void addDate() throws Exception{
FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt");
//输出一个字节
fos1.write(97);
fos1.close();
}
public void addArray() throws Exception{
FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt",true);
byte[] bytes = {66,67,68,69,70};
//输出一个数组内容的数据
fos1.write(bytes);
fos1.close();
}
public void addArrayPlus() throws Exception{
FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt",true);
byte[] bytes = {71,72,73,74,75};
//输出一个数组内容的数据
/**
*参数1 bytes 存储数据的数组
*参数2 偏移量 从数组的那个下标开始
*参数3 长度 读取数据的个数
*/
fos1.write(bytes,1,3);
fos1.close();
}
在创建 BufferedInputStream 时,会创建一个内部缓冲区数组
public BufferedInputStream(InputStream in, int size) :创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。创建一个长度为 size 的内部缓冲区数组并将其存储在 buf 中。 可以不指定size,默认为8K。
public void test1() throws Exception{
FileInputStream fis = new FileInputStream("E:/A/B2/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
while((int b = bis.read())!=-1){
System.out.println(b);
}
bis.close();
}
public BufferedOutputStream(OutputStream out, int size) :创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
public void flush() throws IOException :刷新此缓冲的输出流。这迫使所有缓冲的输出字节被写出到底层输出流中。可以不指定size,默认为8K。
public void test1() throws Exception{
FileOutputStream fos = new FileOutputStream("E:/A/B2/a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fis);
while((int b = bos.read())!=-1){
System.out.println(b);
}
fos.close();
bos.close();
}
Reader抽象类,FileReader实现类
构造方法:
常用方法:
public abstract int read(char[] cbuf, int off, int len) throws IOException :
public void testRead() throws Exception{
//输入字符流
FileReader fr = new FileReader("E:/A/B2/a.txt");
//读一个字符,返回值是int
int read = fr.read();
System.out.println(read);
//连续多次,一次读取一个字符
int b = 0;
while((b = fr.read())!=-1){
System.out.println((char)b);
}
fr.close();
}
public void testReadPlus() throws Exception{
//输入字符流
FileReader fr = new FileReader("E:/A/B2/a.txt");
//一次读取字符数组长度的字符
char[] chars = new char[];
int b = 0;
while((b = fr.read(chars))!=-1){
System.out.println(chars);
}
fr.close();
}
Writer抽象类,FileWriter实现类
构造方法:
public void testWrite() throws Exception{
//创建输出字符流
FileWriter fw = new FileWriter("E:/A/B2/aa.txt");
//输出单个字符
fw.write('我');
char[] chars = new char[]{'你','好','j','a','v','a'};
//输出字符数组
fw.write(chars);
//输出字符串
fw.write("你好,java!");
//输出字符数组中指定内容
fw.write(chars,2,4);
//输出字符串中指定内容
fw.write("你好,java!",2,6);
}
public BufferedReader(Reader in, int sz) :创建一个使用指定大小输入缓冲区的缓冲字符输入流。可以不指定sz,默认为8K。
public String readLine() throws IOException :读取一个文本行。通过下列字符之一即可认为某行已终止:换行 (’\n’)、回车 (’\r’) 或回车后直接跟着换行。返回:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("C:\\A\\B\\d.txt");
BufferedReader br = new BufferedReader(fr);
int b = 0;
// 从缓冲区中一次读取一个 输出
// while((b = br.read()) != -1) {
// System.out.println((char)b);
// }
// fr.close();
// 从缓冲区中一次读一行 输出,不会读取换行符
String s = null;
while((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
}
public BufferedReader(Reader in, int sz) :创建一个使用指定大小输入缓冲区的缓冲字符输入流。可以不指定sz,默认为8K。
public static void main(String[] args) throws IOException {
/**
* 使用缓冲字符流 进行 文本复制
*/
// 创建 缓冲输入字符流
FileReader fr = new FileReader("C:\\A\\B\\d.txt");
BufferedReader br = new BufferedReader(fr);
// 创建 缓冲输出字符流
FileWriter fw = new FileWriter("C:\\A\\B\\d_copy.txt");
BufferedWriter bw = new BufferedWriter(fw);
// int b = 0;
// // 从缓冲区读一个字符,会读取 换行符
// while((b = br.read()) != -1) {
// bw.write(b);
// }
String s = null;
// 从缓冲区读一行.没有读取换行符
while((s = br.readLine()) != null) {
// 写一行
bw.write(s);
// 换行
bw.newLine();
}
bw.close();
br.close();
}