NIO读取文件

博客围绕Java的NIO和流展开,虽未给出具体内容,但可知聚焦于Java技术领域中NIO和流的相关知识,这些在后端开发里较为重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	private static void NIORW2() {
		FileInputStream accessFile = null;
		FileOutputStream accessFile2 = null;
		try {
			accessFile = new FileInputStream("E:\\tool\\wls1036_win32.exe");
			accessFile2 = new FileOutputStream("E:\\tool\\wls1036_win32(6).exe");
			FileChannel fileChannel = accessFile.getChannel();
			FileChannel fileChannel2 = accessFile2.getChannel();
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			while ((fileChannel.read(buffer)) > 0) {
				buffer.flip();
				// byte[] bytes = new byte[buffer.remaining()];
				// buffer.get(bytes);
				// System.out.println( new String(bytes));

				// fileChannel2.write(ByteBuffer.wrap(bytes));
				fileChannel2.write(buffer);
				buffer.clear();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				accessFile.close();
				accessFile2.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 

	private static void NIORW() {
		RandomAccessFile accessFile = null;
		RandomAccessFile accessFile2 = null;
		try {
//			accessFile = new RandomAccessFile("./src/file/hello.txt", "rw");
//			accessFile2 = new RandomAccessFile("./src/file/hello2.txt", "rw");
			
			accessFile = new RandomAccessFile("E:\\tool\\wls1036_win32.exe", "rw");
			accessFile2 = new RandomAccessFile("E:\\tool\\wls1036_win32(3).exe", "rw");

			FileChannel fileChannel = accessFile.getChannel();
			FileChannel fileChannel2 = accessFile2.getChannel();

			ByteBuffer buffer = ByteBuffer.allocate(1024);
			while ((fileChannel.read(buffer)) > 0) {
				buffer.flip();
				// byte[] bytes = new byte[buffer.remaining()];
				// buffer.get(bytes);
				// System.out.println( new String(bytes));

				// fileChannel2.write(ByteBuffer.wrap(bytes));
				fileChannel2.write(buffer);
				buffer.clear();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				accessFile.close();
				accessFile2.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值