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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章