Java-io流

一.概念和作用

        流是一组有顺序的,有起点和终点的字节集合,,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

二.io流的分类

        根据处理数据类型的不同分为:字符流和字节流

        根据数据流向不同分为:输入流和输出流 

 

字节流和字符流

字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。

字节流和字符流的区别:

(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。

(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。

结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

 

输入流和输出流:

对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。

java流类图结构:

 

 

 javaIO流对象

1.输入字节流InputStream

从输入字节流的继承图可以看出来:

InputStream 是所有的输入字节流的父类,它是一个抽象类。

实例操作演示:

import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[1024];
       in.read(b);
       in.close();
       System.out.println(new String(b));
    }
}

输出字节流OutputStream

定义和结构说明

OutputStream 是所有的输出字节流的父类,它是一个抽象类。

实例操作演示:

import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f);
       String str="Hello World";
       byte[] b=str.getBytes();
       out.write(b);
       out.close();
    }
}

复制文件

import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       if(args.length!=2){
           System.out.println("命令行参数输入有误,请检查");
           System.exit(1);
       }
       File file1=new File(args[0]);
       File file2=new File(args[1]);
         
       if(!file1.exists()){
           System.out.println("被复制的文件不存在");
           System.exit(1);
       }
       InputStream input=new FileInputStream(file1);
       OutputStream output=new FileOutputStream(file2);
       if((input!=null)&&(output!=null)){
           int temp=0;
           while((temp=input.read())!=(-1)){
                output.write(temp);
           }
       }
       input.close();
       output.close();
    }
}

字符输入流Reader

定义和说明:

Reader 是所有的输入字符流的父类,它是一个抽象类。

实例操作演示:

/**
 * 字符流
 * 从文件中读出内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       char[] ch=new char[100];
       Reader read=new FileReader(f);
       int count=read.read(ch);
       read.close();
       System.out.println("读入的长度为:"+count);
       System.out.println("内容为"+new String(ch,0,count));
    }
}

字符输出流Writer

定义和说明

Writer 是所有的输出字符流的父类,它是一个抽象类。

实例操作演示

/**
 * 字符流
 * 写入数据
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       Writer out =new FileWriter(f);
       String str="hello";
       out.write(str);
       out.close();
    }
}

删除一个文件或者文件夹:

import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       if(f.exists()){
           f.delete();
       }else{
           System.out.println("文件不存在");
       }
         
    }
}

创建一个文件夹:

/**
 * 创建一个文件夹
 * */
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator+"hello";
       File f=new File(fileName);
       f.mkdir();
    }
}

列出目录下的所有文件

/**
 * 使用list列出指定目录的全部文件
 * */
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator;
       File f=new File(fileName);
       String[] str=f.list();
       for (int i = 0; i < str.length; i++) {
           System.out.println(str[i]);
       }
    }
}

 

 

 

posted @ 2020-06-01 16:35  贤哲  阅读(119)  评论(0编辑  收藏  举报