「Java基础」八、文件与IO流

2021/7/3 1:21:37

本文主要是介绍「Java基础」八、文件与IO流,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

八、文件与IO流

I:input 写入

O:output 写出

1 - 1 文件

  • 对java而言

    • 读取电脑上的文件----输入

    • 写出到电脑上进行文件内容存储----输出

      即java运行时,将java中的数据写出到硬盘(从内存的临时存储到磁盘的持久化存储)--------持久化存储

  • java中文件存储流程:

    1. 创建一个文件容器 File 保存文件的容器 ----只涉及对文件本身的操作,不涉及文件内容
public class Test {
    public static void main(String[] args) {
        // 要创建文件的路径
        File file = new File("C:\\Users\\Genius\\Desktop\\Test\\test1.txt");
        // 要创建文件夹的路径
//        File makeDirectory = new File("C:\\Users\\Genius\\Desktop\\Test\\MyDir");
        
        System.out.println("file ::> " + file);
        // 创建文件
        try {
            if (!file.exists()){ // 文件不存在
                file.createNewFile();//创建文件
            }else{
                System.out.println("文件已经存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建文件夹
        file.mkdir();
//        file.mkdirs(); // 逐层创建

        // 设置文件权限
        file.setWritable(false);//只读
        // 判断文件是否能读
        System.out.println("file.canRead() ::> " + file.canRead());

        // 获取文件大小
        System.out.println("file.length() ::> " + file.length());
        // 获取文件路径
        System.out.println("file.getName() ::> " + file.getName());
        // 删除文件
        System.out.println("file.delete() ::> "+file.delete());

    }
}

1 - 2 IO流--字节流的读和写

用于读取字节文件

1 - 2 - 1 读取文件内容

InputStream

FileInputStream

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

        File file = new File("C:\\Users\\Genius\\Desktop\\Test\\test.txt");
//         读取文件内容
        InputStream is = null;	//InputStream 输入的字节流
        try {
            is = new FileInputStream(file); //创建通道

            byte[] bytes = new byte[(int)file.length()];
            is.read(bytes); // 将内容存入数组

            System.out.println(new String(bytes));// 输出文件内容

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close(); // 关闭通道
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

1 - 2 - 2 写入内容到文件

OutputStream

FileOutputStream

public class Test {


    public static void main(String[] args) {

        File file = new File("C:\\Users\\Genius\\Desktop\\Test\\test.txt");
        OutputStream os = null;
        InputStream is = null;
        try {
            // 写入内容
//            os = new FileOutputStream(file);   // 向文件中写入内容,不加true会覆盖原文件中的内容
            os = new FileOutputStream(file,true);   // 向文件中写入内容,且不覆盖原内容,会在原内容的基础上进行拼接
            os.write("hahahahah".getBytes());

            // 读取文件内容
            is = new FileInputStream(file);
            byte[] bytes = new byte[(int)file.length()];
            is.read(bytes);
            System.out.println(new String(bytes));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {	// 关闭通道
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

}

1 - 2 - 3 文件复制

public class Test {

    public static void main(String[] args) {

        File file = new File("C:\\Users\\Genius\\Desktop\\Test\\test.txt");	// 原文件位置
        File fileColon = new File("C:\\Users\\Genius\\Desktop\\Test\\tes\\test.txt");	// 复制文件位置

        OutputStream os = null;
        InputStream is = null;

        try {
            // 输入
            is = new FileInputStream(file);
            byte[] bytes = new byte[(int)file.length()];
            is.read(bytes);
            
            // 输出
            os = new FileOutputStream(fileColon);
            os.write(bytes);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

}

1 - 2 - 4 优化

byte[] 读取文件,会出现超出int范围的情况,故可以使用循环读取

// byte[] bytes = new byte[(int)file.length()];	// 当文件过大时,可能会超出int的范围,导致文件读取不完整
// 文件边读边写,有效解决了这一问题
byte[] bytes = new byte[1024];
int i = is.read(bytes);
while (i!=-1){	
	os.write(bytes,0,i);
	i = is.read(bytes);
}
  • demo-图片的复制
public class Test {

    public static void main(String[] args) {

        File file = new File("C:\\Users\\Genius\\Desktop\\Test\\课表.jpg");
        File fileColon = new File("C:\\Users\\Genius\\Desktop\\Test\\tes\\课表.jpg");

        OutputStream os = null;
        InputStream is = null;

        try {
            is = new FileInputStream(file);
            os = new FileOutputStream(fileColon,true);

            byte[] bytes = new byte[1024];
            int i = is.read(bytes);
            while (i!=-1){	// 文件边读边写
                os.write(bytes,0,i);// 注意:从 0 位置开始写 ,写到 i
                i = is.read(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

}

1 - 2 IO流--字符流的读和写

用来读取中文信息

public class InputTest {

    public static void main(String[] args) {

        File file = new File("C:\\Users\\Genius\\Desktop\\Test\\test.txt");
        File fileColon = new File("C:\\Users\\Genius\\Desktop\\Test\\tes\\test.txt");

        Reader reader = null;
        Writer writer = null;

        try {
            reader = new FileReader(file);
            writer = new FileWriter(fileColon);
            char[] chars = new char[512];// 字符流的读取,1个字符占用2个字节
            int i = reader.read(chars);
            while (i!=-1){
                writer.write(chars,0,i);
                i = reader.read(chars);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

1 - 3 缓冲流

可以提高数据的读写速度

建立在IO流的基础上的

BufferedInputStream

BufferedOutputStream

BufferedReader

BufferedWriter

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

        File file = new File("C:\\Users\\Genius\\Desktop\\Test\\test.txt");
        File fileColon = new File("C:\\Users\\Genius\\Desktop\\Test\\tes\\test.txt");

        InputStream is = null;
        BufferedInputStream bis = null;

        try {
            is = new FileInputStream(file);
            bis = new BufferedInputStream(is);

            byte[] bytes = new byte[(int)file.length()];
            bis.read(bytes);
            System.out.println(new String(bytes));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}


这篇关于「Java基础」八、文件与IO流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程