java字节流字符流接口实现

main函数(将D盘下的 “D:/123.txt” 读写到E盘下 “E:/123.txt” , 并将"E:/123.txt"的内容读成字符串)

注:(1)接口类的前三个是字节输入输出流,即可以读取非文本文件
(2)后两个方法是字符输入输出流,即只能读取文本文件(字符流是特殊的字节流)

public class TestMain {
	@Test
	public void test1() {	
		IOUilts ioUilts = new IOUiltsImpl();
		ioUilts.readeAndWrite("D:/123.txt", "E:/123.txt");
		
		System.out.println(ioUilts.read2String("E:/123.txt"));
	}
}

接口IOUilts

public interface IOUilts {
	/**
	 * @Title: readeAndWrite
	 * @Description:(完成文件的读写)
	 * @param fromPath 要读取的文件地址
	 * @param toPath   要写出的文件地址
	 */
	void readeAndWrite(String fromPath, String toPath);

	/**
	 * @Title: readeAndWrite
	 * @Description:(完成文件的读写)
	 * @param fromFile 要读取的文件
	 * @param toFile   要写出的文件
	 */
	void readeAndWrite(File fromFile, File toFile);

	/**
	 * @Title: readeAndWrite
	 * @Description:(完成文件的读写
	 * @param inputStream  要读取的输入流
	 * @param outputStream 要写出的输出流
	 */
	void readeAndWrite(InputStream inputStream, OutputStream outputStream);

	/**
	 * @Title: read2String
	 * @Description:(将文本文件读成字符串)
	 * @param readPath 要读取的文件地址
	 * @return 读取成的字符串
	 */
	String read2String(String readPath);
	
	/**
	 * @Title: read2String
	 * @Description:(将文本文件读成字符串)
	 * @param readFile 要读取的文件
	 * @return 读取成的字符串
	 */
	String read2String(File readFile);
}

接口实现类,重写readAndWriter()方法和read2String()方法

public class IOUtilImpl {
	/**
	 * 
	 * @param fromFile 读取的文件
	 * @param toFile   写入的文件
	 */
	public static void readAndWriter(File fromFile, File toFile) {
		// 此处需要处理一个写出文件的目录
		File parentFile = toFile.getParentFile();
		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}
		try {
			InputStream inputStream = new FileInputStream(fromFile);
			OutputStream outputStream = new FileOutputStream(toFile);
			readAndWriter(inputStream, outputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param fromPath 读取的文件路径
	 * @param toPath   写入的文件路径
	 */
	public static void readAndWriter(String fromPath, String toPath) {
		try {
			InputStream inputStream = new FileInputStream(fromPath);
			OutputStream outputStream = new FileOutputStream(toPath);
			readAndWriter(inputStream, outputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param inputStream  输入流
	 * @param outputStream 输出流
	 */
	public static void readAndWriter(InputStream inputStream, OutputStream outputStream) {
		// 将传入的输入流输出流,在包装成带缓冲区的输入流输出流。
		BufferedInputStream bis = new BufferedInputStream(inputStream);
		BufferedOutputStream bos = new BufferedOutputStream(outputStream);
		byte[] bytes = new byte[1024];
		int length = -1;
		try {
			while ((length = bis.read(bytes)) != -1) {
				bos.write(bytes, 0, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bos != null) {
					bos.close();
				}
				if (bis != null) {
					bis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}
	
	/**
	 * @Description:(将文本文件读成字符串)
	 * @param readPath 要读取的文件地址
	 * @return 读取成的字符串
	 */
	@Override
	public String read2String(String readPath) {
		File file = new File(readPath);
		String string = read2String(file);
		return string;
	}

	/**
	 * @Description:(将文本文件读成字符串)
	 * @param readFile 要读取的文件
	 * @return 读取成的字符串
	 */
	@Override
	public String read2String(File readFile) {
		StringBuffer buffer = new StringBuffer();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(readFile), "utf-8"));
			String line = null;
			while((line=reader.readLine())!=null) {
				buffer.append(line).append("\n");
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值