File类、字节输入输出流、字符输入输出流-JAVA基础

1 、File类

public boolean isDirectory()测试此抽象路径表示的文件是否是一个目录

public boolean createNewFile() throws IOException创建一个文件

 public boolean delete()删除此抽象路径的文件或者目录,如果路径为一个目录,目录为空的时候才能进行删除

public File[] listFiles()返回抽象路径名数组,这些路径名表示抽象路径目录中的文件

public boolean mkdir()创建抽象路径所指定的目录


import java.io.File;
import java.io.IOException;

public class testFiled {

    /**
     * @param ycy
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File file = new File("d://java创建目录");
        boolean b = file.mkdir();  //创建一个虚拟目录
        if(b){
            file = new File("d://java创建目录//java.txt");
            try {
                boolean b2 = file.createNewFile();   //创建一个文件
                System.out.println(b2+"txt创建成功");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("txt创建不成功");
                e.printStackTrace();
                
            }
            System.out.println(b+"目录创建成功");
        }else{
            System.out.println(b+"目录创建不成功");
        }
        //遍历目录文件
        File file2 = new File("D://javaxiaoli//XMLParser-master");  //遍历目录文件
        File file4[] = file2.listFiles();
        for(int i=0;i<file4.length;i++){
            System.out.println(file4[i]+" 文件是:");  //打印目录文件的路径
        }
        //遍历目录文件
        if(file2 != null){
            if(file2.isDirectory()){                     //isDirectory()是否为目录
                File file5[] = file2.listFiles();
                if(file5 != null){
                    for(int i=0;i<file5.length;i++){
                        System.out.println(file5[i]+" 文件是:");  //打印目录文件的路径
                    }
                }
            }else{
                System.out.println(" 是:文件");
            }
        }
        
        boolean b3 = file.delete();      //删除文件,得目录为空才能被删除,删除是从内向外删除
        System.out.println(b3+"删除成功");
    }

}
2 、字节输入、输出流

1)InputStream读取文件

a.批量读取

硬盘文件读入内存中。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class testStream {

    /**
     * @param ycy
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        File file = new File("D://java2.txt");
        FileInputStream fis = new FileInputStream(file);  //实例化FileInputStream
        
        //byte b[] = new byte[1024];  //此时为固定长度
        int fileLength = (int) file.length();  //获取文件字节长度
        byte b[] = new byte[fileLength];  //更具实际大小定义字节数组长度
        try {
            int length = fis.read(b);   //读入到字节数组中
            fis.close();    //关闭输入流,节省资源
            System.out.println("输出内容: "+new String(b,0,length));  //读取长度,根据长度进行输出,只输出(0,length)的字节
            System.out.println("输出内容: "+new String(b,"UTF-8"));  //编码设置为UTF-8
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

File类、字节输入输出流、字符输入输出流-JAVA基础

b.一个字节一个字节读取

while((tempt = fis.read())!=-1){
                b2[length2++] = (byte) tempt;
            }

2)OutputStream写入文件

从内存到硬盘。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class testOutputStream2 {

    /**
     * @param ycy
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            File file = new File("D://java2.txt");
            FileOutputStream fops = new FileOutputStream(file,true); //FileOutputStream(文件名称,true进行追加)
            String str = "你好!";
            byte b[] = str.getBytes();
            try {
                fops.write(b);  //直接写入,但是会覆盖掉文件原来的内容
                fops.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

File类、字节输入输出流、字符输入输出流-JAVA基础

3)BufferedInputStream和BufferOutputStream

a.BufferedInputStream

BufferedInputStream字节缓冲输入流

构造方法
public BufferedInputStream(InputStream in):
默认缓冲区大小构造缓冲输入流对象
in - 底层输入流。

public BufferedInputStream(InputStream in,int size):
指定缓冲区大小构造缓冲输入流对象
in - 底层输入流。
size - 缓冲区大小。   IllegalArgumentException 如果 size <= 0,则抛出这个异常。

常用方法
public int read()
一次读写一个字节。
下一个数据字节,如果到达流末尾,则返回 -1。

public int read(byte[] b,int off,int len)
一次读取一个字节数组
b - 目标缓冲区。
off - 开始存储字节处的偏移量。

len - 要读取的最大字节数。

public void close() throws IOException
关闭此输入流并释放与该流关联的所有系统资源。

在使输入流的时候,两种方式读取(一次读取一个字节/一次读取一个字节数在),只能用一种方式,否则,会出现错误!

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class testBuffer {
    /**
     * @param ycy
     * 缓冲进行复制文件
     */
    public static void Bufferstream() throws Exception{
        BufferedInputStream binputstream = new BufferedInputStream(new FileInputStream("D://centerzuobiao.txt"));
        BufferedOutputStream boutstream = new BufferedOutputStream(new FileOutputStream("F://java2new.txt"));
        int b =0;
        long startTime = System.currentTimeMillis(); //输出开始当前时间
        while((b=binputstream.read())!=-1){
            boutstream.write(b);
        }
        binputstream.close();
        boutstream.close();
        long endTime = System.currentTimeMillis();  //输出结束当前时间
        
        System.out.println((endTime-startTime)+"缓冲时间");
    }
    /**
     * @param ycy
     * 非缓冲进行复制文件
     */
    public static void stream() throws Exception{
        InputStream inputstream = new FileInputStream("D://centerzuobiao.txt");
        OutputStream outputstream = new FileOutputStream("F://java2xin.txt");
        int b =0;
        long startTime = System.currentTimeMillis(); //输出开始当前时间
        while((b=inputstream.read())!=-1){
            outputstream.write(b);
        }
        inputstream.close();
        outputstream.close();
        long endTime = System.currentTimeMillis();  //输出结束当前时间
        
        System.out.println((endTime-startTime)+"非缓冲时间");
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Bufferstream();
        stream();
    }

}

File类、字节输入输出流、字符输入输出流-JAVA基础

时间差异很大!!

缓冲就是内存开辟空间,当读或者写出现延迟的时候,会把内容保存到缓冲区,方便很快进行读写操作。

4)缓冲和非缓冲的区别以及性能对比

总结:缓冲流是一个增加了内部缓存的流。当一个简单的写请求产生后,数据并不是马上写到所连接的输出流和文件中,而是写入高速缓存。

当缓冲写满或关闭流之后,再一次性从缓存中写入输出流或者文件中。这样可以减少实际写请求的次数,以此提高数据写入文件中的效率。

类似地,从一个带有缓存的输入流读取数据,也可先把缓存读满,随后的读请求直接从缓存中而不是从文件中读取,这种方式大大提高了读取数据的效率。

 3、字符输入输出流

与字节操作单位不同

1)Reader读取文件

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class testReader {

    /**
     * @param ycy
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("D://java2.txt");
        Reader reader = new FileReader(file);
        char c[] = new char[1024];
        int len = reader.read(c);
        reader.close();
        System.out.print("内容是: "+new String(c,0,len));
    }

}

File类、字节输入输出流、字符输入输出流-JAVA基础

2)  Writer写入文件

 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class testReader {

    /**
     * @param ycy
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("D://java2.txt");
        Reader reader = new FileReader(file);
        char c[] = new char[1024];
        int len = reader.read(c);
        reader.close();
        System.out.print("内容是: "+new String(c,0,len));
        
        Writer writer = new FileWriter(file,true);
        String str = "我爱中国";
        writer.write(str);   //字符串写入输出流
        writer.close();
    }

}

File类、字节输入输出流、字符输入输出流-JAVA基础

扫码关注一起随时随地学习!!!就在洋葱攻城狮,更多精彩,等你来!!

File类、字节输入输出流、字符输入输出流-JAVA基础