javaSE (三十)IO流异常处理、图片加密处理、输入文件路径并拷贝文件内容、在文件中键入内容

1、IO流异常处理:

IO流异常处理一般可以写得如下这么复杂(面试备用)

  • alt + shift + z 直接try-catch ,不过没有加finally
  • 因为作用域的问题,需要在外面创建BufferedInputStream对象并且初始化为null(要是不初始化,就不需要关闭了)
  • 最后关闭的时候,需要来个嵌套try-catch,去日报关闭一个

代码中try-finally,没加catch(最好还是加吧,反正可以快捷键):
java中异常处理时为什么可以只要try-finally,而可以不要catch
捕获异常,不处理,直接finally扫尾

package cn.njupt;

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 file {
	public static void main(String[] args) throws IOException {

		BufferedInputStream bin = null;
		BufferedOutputStream bout = null;
		try {
			bin = new BufferedInputStream(new FileInputStream("xxx.txt"));
			bout = new BufferedOutputStream(new FileOutputStream("yyy.txt"));

			int b = 0;
			while ((b = bin.read()) != -1) {
				bout.write(b);

			}
		} finally { // alt + shift + z 直接try-catch ,不过没有加finally

			try {
				if (bin != null)
					bin.close();
			}  finally {
				if (bout != null)
					bout.close();
			}
		}
	}

}

1.7新版本的IO异常处理:

因为IO流实现了 AutoCloseable的接口,所以可以自动关闭,要是想在try()的括号里写方法,需要实现实现接口(感觉这么写虽然简洁但是没什么章法)

package cn.njupt;

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 file {
	public static void main(String[] args) throws IOException {

		try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream("xxx.txt"));
			BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("yyy.txt"));)
		{		
			int b = 0;
			while ((b = bin.read()) != -1) {
				bout.write(b);
			}
	}
		
	}

}

2、图片加密处理:

可以在write()的时候直接异或一个数,然后再次拷贝的时候需要再次异或同一个数(a异或b两次等于a本身)

package cn.njupt;

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 file {
	public static void main(String[] args) throws IOException {

		BufferedInputStream bin = new BufferedInputStream(new FileInputStream("yyy.txt"));
		BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("zzz.txt"));

		int b = 0;
		while ((b = bin.read()) != -1) {
			bout.write(b ^ 223);
		}

		bin.close();
		bout.close();
	}

}

3、输入文件路径并拷贝:

  1. 键盘输入 (加一个while循环键入和判断)
  2. 判断是否为文件 File.isFile()
  3. IO流拷贝
package cn.njupt;

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


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

		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.println("请输入正确的文件名:");
			String line = sc.nextLine();
			File file = new File(line);

			if (file.isFile()) {
				FileInputStream in = new FileInputStream(file);
				FileOutputStream out = new FileOutputStream(file.getName());

				byte[] arr = new byte[1024 * 8];
				int len;
				while ((len = in.read(arr)) != -1) {
					out.write(arr, 0, len);
				}

				in.close();
				out.close();
				break;

			} else if(file.isDirectory()){
				System.out.println("输入的是文件夹,请重新输入");
			}else if(!file.exists()){
				System.out.println("不存在此路径,请重新输入");
			}

		}

	}

}

4、在文件中键入内容(遇到quit结束):

package cn.njupt;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

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

		Scanner sc = new Scanner(System.in);
		FileOutputStream out = new FileOutputStream("yyy.txt");
		while (true) {
			System.out.println("请输入想要存储的数据:");
			String line = sc.nextLine();
			if (line.equals("quit")) {
				break;
			} else {
				byte[] arr = line.getBytes();
				out.write(arr);
				out.write("\r\n".getBytes());//键入换行
				
			}

		}
			out.close();
	}

}

输出:
我
是
吴彦祖

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值