NIO读取文件

	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();
			}
		}
	}

 

你可能感兴趣的:(java)