1.概念
- I0流用来处理设备之间的数据传输
- Java对数据的操作是通过流的方式
Java用于操作流的类都在I0包中
流按流向分为两种:输入流,输出流。
流按操作类型分为两种:
字节流:字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
字符流:字符流只能操作纯字符数据,比较方便。
- I0流常用父类
字节流的抽象父类:
Inputstream
outputstream
字符流的抽象父类:
Reader
Writer
3.I0程序书写
使用前,导入I0包中的类
使用时,进行I0异常处理
事使用后,释放资源
4.read()方法读取的是 一个字节,为什么返回是int ,而不是byte
因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,如果每次读取都返回byte ,有可能在读到中间的时候遇到1111111
那么1111111是byte类型的-1 ,我们的程序是遇到- 1就会停止不读了,后面的数据就读不到了。所以在读取的时候用int类型接收,如果11111会在其前面补上
24个0凑足4个字节,那么byte类型的- 1就变成int类型的255了这样可以保证整个数据读完,而结束标记的- 1就是int类型
public class IoDemo {
/**
int read() :从该输入流读取一个字节的数据。
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("ere.txt");
int b;
while((b=fis.read())!=-1){
System.out.println(b);
}
fis.close();
}
}
public class IoDemo {
/**
fileoutstream
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("ttt.txt");
fos.write(97);
fos.write(97);
fos.write(98);
fos.close();
}
}
public class IoDemo {
/**
fileoutstream在创建对象的时候,如果文件不存在就会创建一个文件,如文件已经存在了,在每次创建对象的时候就会
把文件的内容进行清空
追加功能,在对象的第二个参数处写明true
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("ttt.txt",true);
fos.write(100);
fos.write(100);
fos.write(980);
fos.close();
}
}
public class IoDemo {
/**
拷贝数据
* @param args
* @throws IOException
* 实际开发中不推荐使用,copy效率太慢
*/
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("java.png");// 创建输入流对象,关联到java.png
FileOutputStream fos = new FileOutputStream("copy.png");
int b;
while((b = fis.read())!=-1){
fos.write(b);
}
fis.close();
fos.close();
}
}
8.public class IoDemo {
/**
int available() 得到其文件的字节大小
拷贝数据
* @param args
* @throws IOException
* 实际开发中也不使用此类的方法,因为copy的文件如果字节数过大,就会造成内存溢出
*/
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("java.png");// 创建输入流对象,关联到java.png
FileOutputStream fos = new FileOutputStream("copy.png");
byte arr[] = new byte[fis.available()]; //根据文件的字节大小,创建一个字节数组
fis.read(arr); //将文件的所有的字节读到arr数组中
fos.write(arr); //将数组中的所有的字节写到copy.jpg文件上
fis.close(); //释放资源
fos.close();
}
}
9.
public class IoDemo {
/**
拷贝数据
* @param args
* @throws IOException
使用小数组控制传输大小
*/
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("ere.txt");// 创建输入流对象,关联到ere.txt
FileOutputStream fos = new FileOutputStream("copy.txt");
int len;
byte arr[] = new byte[1024];
while((len=fis.read(arr))!=-1){
fos.write(arr, 0, len);
}
fis.close(); //释放资源
fos.close();
}
}
public class IoDemo {
/**
拷贝数据
* @param args
* @throws IOException
BufferedInputStream
BufferedOutputStream 底层传输的时候已经封装一个字节数组,大小是8192
*/
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("java.png");// 创建输入流对象,关联到ere.txt
FileOutputStream fos = new FileOutputStream("copy.png");
BufferedInputStream bds = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
while((len=bds.read())!=-1){
bos.write(len);
}
fis.close(); //释放资源
fos.close();
}
}
11.flush方法和close方法的区别
flush()方法
用来刷新缓冲区的,刷新后可以再次写出。(用于需要获取实时数据的开发中)
close( )市法
用来关闭流释放资源的的,如果是带缓冲区的流对象的close( )方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出。
12.public class IoDemo {
/**
拷贝数据
* @param args
* @throws IOException
IO流的标准异常处理方式
*/
public static void main(String[] args) throws IOException {
demo1();
}
//java1.6版本之前的IO流处理异常的
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(“ere.txt”);
fos = new FileOutputStream(“ttt.txt”);
int len = fis.read();
while(len!=-1){
fos.write(len);
}
}finally{
try{
if(fis!=null)
fis.close();
}finally{
if (fos!=null)
fos.close();
}
}
}
}
13.
public class IoDemo {
/**
拷贝数据
* @param args
* @throws IOException
键盘录入指定的文件,拷贝到当前路径下
*/
public static void main(String[] args) throws IOException {
File file = getFile();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
int len;
while((len=bis.read())!=-1){
bos.write(len);
}
bis.close();
bos.close();
}
public static File getFile(){
//键盘录入一个路径
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件路径");
while(true){
String line = sc.nextLine();
File file = new File(line);
if(!file.exists()){
System.out.println("请从新输入一个文件路径,该文件路径不存在");
}else if(file.isDirectory()){
System.out.println("您输入的是文件夹路径,请输入一个文件路径");
}else{
return file;
}
}
}
}