JAVA IO 总结

关于 java io 主要是 对 文件 的输入和 输出 方面的东西,因此,我进行系统的学习

主要有这几个 关键的类 File   FileInputStream   FileOutputStream   InputStreamReader   OutputStreamWriter  BufferedReader BufferedWriter  BufferedInputStream  BufferedOutputStream

 以下我坐下详细的介绍:

 File 类是最基本的 文件类

可以获取文件的基本属性.

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

public class Test01 {

	public static void main(String[] args) throws IOException {

		File file = new File("d:"+File.separator+"io"+File.separator+"写给新手程序员的一封信.txt");

		if (!file.exists()) {
			file.createNewFile();
			System.out.println("文件已创建");
			System.out.println("名称:" + file.getName());
			System.out.println("相对路径:" + file.getCanonicalPath());
			System.out.println("绝对路径:" + file.getAbsolutePath());
			System.out.println("文件大小:" + file.length());
			if (file.delete()) {
				System.out.println("文件已删除!");
			}

		}

	}

}

 FileInputStream   和 FileOutputStream 用这两个可以输出相应的文本文件内容,使用 InputStreamReader 和 OutputStreamWriter 来输入和输出文件流,也可以用来转换编码方式,这样可以使输出的文件不乱码.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Test02 {
    public static void main(String[] args) {

        String src = "D:" + File.separator + "io" + File.separator
                + "hello.txt";
        String dest = "D:" + File.separator + "io" + File.separator + "out"
                + File.separator + "hello.txt";

        FileInputStream in = null;
        FileOutputStream out = null;

        File path = new File(dest);
        try {
            if (!path.exists()) {
                path.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dest);
            InputStreamReader is = new InputStreamReader(in, "GBK");
            OutputStreamWriter os = new OutputStreamWriter(out, "GBK");
            // 读取文件内容
            // byte[] buf = new byte[1024];
            // int len =0;
            // while((len = in.read(buf))!=-1){
            // System.out.println(new String(buf,0,len));
            // }

            char[] buf = new char[1024];
            int len = 0;
            while ((len = is.read(buf)) != -1) {
                // System.out.println(new String(buf,0,len));
                os.write(new String(buf, 0, len));
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

使用 BufferedReader 和 BufferedWriter 来使用缓冲区读取 或者写入内容

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 java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Test03 {
    public static void main(String[] args) {
        String src = "D:" + File.separator + "io" + File.separator + "pet.txt";
        String dest = "D:" + File.separator + "io" + File.separator
                + "pets.txt";

        //FileReader fr = null;
        //FileWriter fw = null;

        File path = new File(dest);
        if (!path.exists()) {
            try {
                path.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            //fr = new FileReader(src);
           // fw = new FileWriter(dest);

            // 进行转码
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    src), "GBK"));
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(path), "GBK"));

            String str = null;

            System.out.print("替换前:");
            char[] ch = new char[1024];
            int temp = 0;
            while ((temp = br.read(ch)) != -1) {
                str = new String(ch, 0, temp);
                System.out.println(str);
            }

            System.out.print("替换后:");
            String name = "欧欧", type = "狗狗", master = "范范";

            String one = str.replace("{name}", name);
            String two = one.replace("{type}", type);
            String there = two.replace("{master}", master);

            System.out.println(there);

            bw.write(there);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

使用 FileInputStream 和 FileOutputStream 用于二进制文件的读取和输入 ,也可以使用缓冲区进行 BufferedInputStream 和 BufferedOutputStream 二进制文件的读取和输出

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test04 {

    public static void main(String[] args) {

        // windows 下添加 \ linux 下添加 
        String src = "d:" + File.separator + "io" + File.separator + "1.jpg";
        String dest = "d:" + File.separator + "io" + File.separator + "out"
                + File.separator + "1.jpg";

        FileInputStream i = null;
        FileOutputStream o = null;

        BufferedInputStream bi = null;
        BufferedOutputStream bo = null;

        try {

            i = new FileInputStream(src);
            o = new FileOutputStream(dest);
            bi = new BufferedInputStream(i);
            bo = new BufferedOutputStream(o);

            byte[] buf = new byte[1024];
            int temp = 0;
            while ((temp = bi.read(buf)) != -1) {
                bo.write(buf, 0, temp);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bi != null) {
                try {
                    bi.close();
                    i.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bo != null) {
                try {
                    bo.close();
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

另外在进行 char[] 和 byte[] 数组进行读取数据的时候,char[] 大部分时候用于文本内容的读取 而byte[] 用于 二进制文件的读取

//这部分代码在 Test03 中
char[] ch = new char[1024];
    int temp = 0;
    while ((temp = br.read(ch)) != -1) {
    str = new String(ch, 0, temp);
    System.out.println(str);  
}

//这部分代码在 Test04 中
byte[] buf = new byte[1024];
    int temp = 0;
        while ((temp = bi.read(buf)) != -1) {
        bo.write(buf, 0, temp);
}

总结的大概就这么多,另外还有好几种其他的 读取文件内容的方式,详情请看这篇文章:

http://www.cnblogs.com/nerxious/archive/2012/12/15/2818848.html

原文地址:https://www.cnblogs.com/fengyeyang/p/4886642.html