java实现文件的读写以及图片读写

package com.company;

import java.io.*;

public class Main {

    public static void main(String[] args) {
//        写文件
//        writefile2();
//        读文件
//        getfile2();
//        读写图片
        readAndWritephoto();
    }

    static void readAndWritephoto(){
        File originalFile = new File("C:\\Users\\lenovo\\Desktop\\1606457361.jpg");//指定要读取的图片
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            File result = new File("D:\\test\\dabai.jpg");//要写入的图片
            if (result.exists()) {//校验该文件是否已存在
                result.delete();//删除对应的文件,从磁盘中删除
                result = new File("D:\\test0\\dabai.jpg");//只是创建了一个File对象,并没有在磁盘下创建文件
            }
            if (!result.exists()) {//如果文件不存在
                result.createNewFile();//会在磁盘下创建文件,但此时大小为0K
            }
            in = new FileInputStream(originalFile);
            out = new FileOutputStream(result);// 指定要写入的图片
            int n = 0;// 每次读取的字节长度
            byte[] bb = new byte[1024];// 存储每次读取的内容
            while ((n = in.read(bb)) != -1) {
                out.write(bb, 0, n);// 将读取的内容,写入到输出流当中
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //执行完以上后,磁盘下的该文件才完整,大小是实际大小
            if(in!=null||out!=null){
                try {
                    in.close();
                    out.close();// 关闭输入输出流
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    static void writefile() {
        String path = "D:/aaaaa/a1.txt";
        String context = "你是大番薯吗  真的是?  菜狗"+"\n"+"你是大番薯吗  真的是?  菜狗"+"\n"+"你是大番薯吗  真的是?  菜狗"+"\n"+"你是大番薯吗  真的是?  菜狗";
        FileWriter writer = null;
        try {
            writer = new FileWriter(path);
            writer.write(context);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //    利用BufferedReader缓存写入,效率不高但是安全
    static void writefile2() {
        String path = "D:/aaaaa/a1.txt";
        String context = "你是大番薯吗  真的是?  菜狗"+"\n"+"你是大番薯吗  真的是?  菜狗"+"\n"+"你是大番薯吗  真的是?  菜狗"+"\n"+"你是大番薯吗  真的是?  菜狗";
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(path));

            writer.write(context);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

//    利用BufferedReader缓存读取,效率不高但是安全
    static void getfile(){
        String path = "D:/aaaaa/a1.txt";
        String context = "";
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(path));
            String line;
            while ((line = reader.readLine())!=null){
                context += line + "/n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader !=null){
                try {
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        System.out.println(context);
    }
//区别 区别:1 都是用字符读入,但是FileReader一次只能读入一个字符,而BufferedReader提供了读入一行的方法 2 BufferedReader课余利用缓存写入,但是效率没有前者快
    static void getfile2(){
        //给定文件路径
        String path = "D:/test.txt";
        String context = "";
        //初始化文件写入流
        FileReader reader = null;

        try {
            //创建路径为path的输入流
            reader = new FileReader(path);
            int ch = 0;
//            调用reader.read方法,每调用一次便会读入一个字符,例如 a b c,则第一次调用会读入a的编码,第二次会调用读入空格的编码,第三次会调用读入b的编码
//            一次读取一个字符,返回的该字符的码值,如果想要返回字符,直接进行强转char
            while (reader.read() != -1) {
                System.out.print((char) ch);
                ch = reader.read();
                context += ch;
            }

            } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader !=null){
                try {
//                    读完后要记得关闭
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值