输入及输出
•输入输出(I/O)
把电脑硬盘上的数据读到程序中,称为输入,即input,进行数据的read操作
从程序往外部设备写数据,称为输出,即output,进行数据的write操作
•InputStream和OutputStream的子类都是字节流
-可以读写二进制文件,主要处理音频、图片、歌曲、字节流,处理单元为1个字节。
•Reader和Writer的子类都是字符流
主要处理字符或字符串,字符流处理单元为1个字符。
字节流将读取到的字节数据,去指定的编码表中获取对应文字。
public static void main(String[] args) {
try {
//创建FileInputStream的对象,指定要输入(读)的文件,文件不存在,抛出异常
FileInputStream in = new FileInputStream("E:\\demo.txt");
//每次read();一次,从输入流中读到一个字节,当读取完后会返回-1
int b = in.read();
System.out.println(b);
int b1 = in.read();
System.out.println(b1);
int b2 = in.read();
System.out.println(b2);
int b3 = in.read();
System.out.println(b3);
int b4 = in.read();
System.out.println(b4);
int b5 = in.read();
System.out.println(b5);
int b6 = in.read();
System.out.println(b6);
} catch (IOException e) {
e.printStackTrace();
}
}
输入输出成对出现
public static void main(String[] args) {
FileInputStream in = null;
FileOutputStream out = null;
try {
/*
创建FileInputStream对象,指定要输入(读)的文件,文件不存在,会抛出异常
*/
in = new FileInputStream("E:\\demo.txt");
/*
创建FileOutputStream对象,会自动创建输出的文件
*/
out = new FileOutputStream("F:\\demo.txt");
int b = 0;
while ((b = in.read()) != -1) {
System.out.println(b);
out.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流对象,释放系统资源
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
有效代码
public static void main(String[] args) throws IOException {
/*
创建FileInputStream对象,指定要输入(读)的文件,文件不存在,会抛出异常
*/
FileInputStream in = new FileInputStream("E:\\demo.txt");
/*
创建FileOutputStream对象,会自动创建输出的文件
*/
FileOutputStream out = new FileOutputStream("F:\\demo.txt");
int b = 0;
while ((b = in.read()) != -1) {
System.out.println(b);
out.write(b);
}
in.close();
out.close();
}
读写效率的提升
package com.ff.javaio.Day2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StreamDemo3 {
public static void main(String[] args) throws IOException {
/*
创建FileInputStream对象,指定要输入(读)的文件,文件不存在,会抛出异常
*/
FileInputStream in = new FileInputStream("E:\\demo.txt");
/*
创建FileOutputStream对象,会自动创建输出的文件
*/
FileOutputStream out = new FileOutputStream("F:\\demo.txt");
/*
read();每次从输入流中读取一个字符 返回字符值 读完返回-1
read(byte[] b)每次从输入流中读取一个byte数组长度个字符,返回数组中实际装入内容个数,读完返回-1
*/
byte [] b = new byte[10];
int length=0;
while((length=in.read(b))!=-1){
/* out.write(b);每次传10个字符,不足10个字符,会自动填充空格*/
out.write(b,0,length);//向外写出一个byte数组个字节,从数组指定位置开始,写length个字节
}
in.read(b);
in.close();
out.close();
}
}
缓冲字节输入/出流
public static void main(String[] args) throws IOException {
//创建输入节点流,负责对文件读写
FileInputStream in = new FileInputStream("D:\\Users\\17509\\Desktop\\Tule - Fearless.mp3");
//创建处理对象,内部有一个缓冲数组,默认为8192个字节,包装输入流,提供缓冲功能,也可以设置缓冲区大小
BufferedInputStream bin = new BufferedInputStream(in);
FileOutputStream out = new FileOutputStream("E:/新建文件.mp3");
BufferedOutputStream bout = new BufferedOutputStream(out);
/* int b= 0;
while ((b=bin.read())!=-1){
bout.write(b);
}*/
int length = 0;
byte[] b = new byte[1024];
while ((length = bin.read(b)) != -1) {
bout.write(b, 0, length);
}
bout.flush();//刷新缓冲区
bout.close();
bin.close();
}