java基础-IO流1

来源:互联网 发布:阿里旺旺淘宝版下载 编辑:程序博客网 时间:2024/06/15 19:28

      • File基本信息
      • Fileinputstream-fileoutputstream
      • FileReader-FileWriter
      • 缓冲流
      • 转换流标准输入输出流

这里写图片描述

File基本信息

package com.atguigu;import java.io.File;import java.io.IOException;import java.util.Date;import org.junit.Test;public class TestFile {    @Test    public void test2() {        File file1 = new File("d:/io/helloworld.txt");        System.out.println(file1.exists());        System.out.println(file1.canWrite());        System.out.println(file1.canRead());        System.out.println(file1.isFile());        System.out.println(file1.isDirectory());        System.out.println(new Date(file1.lastModified()));        System.out.println(file1.length());        System.out.println();    }    @Test    public void test1() {        File file1 = new File("d:/io/helloworld.txt");        System.out.println(file1.getName());        System.out.println(file1.getPath());        System.out.println(file1.getAbsoluteFile());        System.out.println(file1.getParent());        System.out.println(file1.getAbsolutePath());        System.out.println();    }}

输出如下:

truetruetruetruefalseSat Oct 18 10:12:45 CST 20143helloworld.txtd:\io\helloworld.txtd:\io\helloworld.txtd:\iod:\io\helloworld.txt
    @Test    public void test3() throws IOException{        File file1 = new File("d:/io/helloworld.txt");        System.out.println(file1.delete());        if(!file1.exists()){            boolean b = file1.createNewFile();            System.out.println(b);        }        File file2 = new File("d:\\io1\\io2");        if(!file2.exists()){            boolean b = file2.mkdirs();            System.out.println(b);        }        File file3 = new File("d:\\teach");        String[] strs = file3.list();        for(int i = 0;i < strs.length;i++){            System.out.println(strs[i]);        }        System.out.println();        File[] files = file3.listFiles();        for(int i = 0;i < files.length;i++){            System.out.println(files[i].getName());        }    }

Fileinputstream-fileoutputstream

package atguigu;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.junit.Test;public class TestFileInputOutputStream {    // 从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)    @Test    public void testFileInputOutputStream() {        File file1 = new File("G:\\wyf.jpg");        File file2 = new File("G:\\2.jpg");        FileInputStream fis = null;        FileOutputStream fos = null;        try {            fis = new FileInputStream(file1);            fos = new FileOutputStream(file2);            byte[] b = new byte[20];            int len;            while ((len = fis.read(b)) != -1) {                fos.write(b, 0, len);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (fos != null) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (fis != null) {                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    // 1.创建一个File对象,表明要写入的文件位置。    // 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖    @Test    public void testFileOutputStream() {        File file = new File("hello2.txt");        FileOutputStream fos = null;        try {            fos = new FileOutputStream(file);            fos.write(new String("I love China!").getBytes());        } catch (Exception e) {            e.printStackTrace();        } finally {            if (fos != null) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    // 读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。    @Test    public void testFileInputStream3() {        FileInputStream fis = null;        try {            File file = new File("hello.txt");            fis = new FileInputStream(file);            byte[] b = new byte[5];            int len;// 每次读入到byte中的字节的长度            while ((len = fis.read(b)) != -1) {                // 或者用下面的方法                // for (int i = 0; i < len; i++) {                // System.out.print((char) b[i]);                // }                String str = new String(b, 0, len);                System.out.print(str);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fis != null) {                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        System.out.println();    }    // 使用try-catch的方式处理如下的异常更合理:保证流的关闭操作一定可以执行    @Test    public void testFileInputStream1() throws Exception {        File file = new File("hello.txt");        FileInputStream fis = new FileInputStream(file);        // 或者下面的方法也行        // int b = fis.read();        // while(b != -1){        // System.out.print((char)b);        // b = fis.read();        // }        int b;        while ((b = fis.read()) != -1) {            System.out.print((char) b);        }        fis.close();    }}

FileReader-FileWriter

package atguigu;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import org.junit.Test;public class TestFileReaderWriter {    @Test    public void testFileReaderWriter() {        FileReader fr = null;        FileWriter fw = null;        try {            // 不能实现非文本文件的复制            File src = new File("hello.txt");            File dest = new File("hello1.txt");            // 2.            fr = new FileReader(src);            fw = new FileWriter(dest);            // 3.            char[] c = new char[24];            int len;            while ((len = fr.read(c)) != -1) {                fw.write(c, 0, len);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (fw != null) {                try {                    fw.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (fr != null) {                try {                    fr.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    @Test    public void testFileReader() {        FileReader fr = null;        try {            File file = new File("hello.txt");            fr = new FileReader(file);            char[] c = new char[24];            int len;            while ((len = fr.read(c)) != -1) {                String str = new String(c, 0, len);                System.out.print(str);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fr != null) {                try {                    fr.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

缓冲流

package atguigu;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import org.junit.Test;/* * 抽象基类         节点流(文件流)            缓冲流(处理流的一种,可以提升文件操作的效率) * InputStream      FileInputStream         BufferedInputStream * OutputStream     FileOutputStream        BufferedOutputStream  (flush()) * Reader           FileReader              BufferedReader  (readLine()) * Writer           FileWriter              BufferedWriter  (flush()) */public class TestBuffered {    @Test    public void testBufferedReader() {        BufferedReader br = null;        BufferedWriter bw = null;        try {            File file = new File("hello.txt");            File file1 = new File("hello2.txt");            FileReader fr = new FileReader(file);            FileWriter fw = new FileWriter(file1);            br = new BufferedReader(fr);            bw = new BufferedWriter(fw);            // char[] c = new char[1024];            // int len;            // while((len = br.read(c))!= -1){            // String str = new String(c, 0, len);            // System.out.print(str);            // }            String str;            while ((str = br.readLine()) != null) {                bw.write(str + "\n");                bw.flush();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (bw != null) {                try {                    bw.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (br != null) {                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    @Test    public void testCopyFile() {        long start = System.currentTimeMillis();        String src = "hello.txt";        String dest = "hello1.txt";        copyFile(src, dest);        long end = System.currentTimeMillis();        System.out.println("花费的时间为:" + (end - start));    }    // 使用缓冲流实现文件的复制的方法    public void copyFile(String src, String dest) {        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        try {            // 1.提供读入、写出的文件            File file1 = new File(src);            File file2 = new File(dest);            // 2.想创建相应的节点流:FileInputStream、FileOutputStream            FileInputStream fis = new FileInputStream(file1);            FileOutputStream fos = new FileOutputStream(file2);            // 3.将创建的节点流的对象作为形参传递给缓冲流的构造器中            bis = new BufferedInputStream(fis);            bos = new BufferedOutputStream(fos);            // 4.具体的实现文件复制的操作            byte[] b = new byte[1024];            int len;            while ((len = bis.read(b)) != -1) {                bos.write(b, 0, len);                bos.flush();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (bos != null) {                try {                    bos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (bis != null) {                try {                    bis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    // 使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制    @Test    public void testBufferedInputOutputStream() {        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        try {            // 1.提供读入、写出的文件            File file1 = new File("G:\\wyf.jpg");            File file2 = new File("G:\\2.jpg");            // 2.想创建相应的节点流:FileInputStream、FileOutputStream            FileInputStream fis = new FileInputStream(file1);            FileOutputStream fos = new FileOutputStream(file2);            // 3.将创建的节点流的对象作为形参传递给缓冲流的构造器中            bis = new BufferedInputStream(fis);            bos = new BufferedOutputStream(fos);            // 4.具体的实现文件复制的操作            byte[] b = new byte[1024];            int len;            while ((len = bis.read(b)) != -1) {                bos.write(b, 0, len);                bos.flush();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (bos != null) {                try {                    bos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (bis != null) {                try {                    bis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

转换流、标准输入输出流

/*
* 如何实现字节流与字符流之间的转换:
* 转换流:InputStreamReader OutputStreamWriter
* 编码:字符串 —>字节数组
* 解码:字节数组—>字符串
*/

package atguigu;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import org.junit.Test;public class TestOtherStream {    @Test    public void test2() {        BufferedReader br = null;        try {            InputStream is = System.in;            InputStreamReader isr = new InputStreamReader(is);            br = new BufferedReader(isr);            String str;            while (true) {                System.out.println("请输入字符串:");                str = br.readLine();                if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) {                    break;                }                String str1 = str.toUpperCase();                System.out.println(str1);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (br != null) {                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /*     * 如何实现字节流与字符流之间的转换: 转换流:InputStreamReader OutputStreamWriter 编码:字符串     * --->字节数组 解码:字节数组--->字符串     */    @Test    public void test1() {        BufferedReader br = null;        BufferedWriter bw = null;        try {            // 解码            File file = new File("hello.txt");            FileInputStream fis = new FileInputStream(file);            InputStreamReader isr = new InputStreamReader(fis, "GBK");            br = new BufferedReader(isr);            // 编码            File file1 = new File("hello1.txt");            FileOutputStream fos = new FileOutputStream(file1);            OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");            bw = new BufferedWriter(osw);            String str;            while ((str = br.readLine()) != null) {                bw.write(str);                bw.newLine();                bw.flush();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (bw != null) {                try {                    bw.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (br != null) {                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}
原创粉丝点击