NIO - FileChannel

Java文件操作与通道实现
本文详细介绍了使用Java NIO的FileChannel进行文件读写、复制及编码转换的操作方法,包括文件读取、多行字符串写入、文件复制以及通道间的高效数据传输。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class FileChannelMain {
	
	private static final Charset charset = Charset.forName("GBK");
	
	private static final int BUFFER_CAPACITY  = 1024;
	
	public static void main(String[] args) throws IOException, InterruptedException {
		final String srcfilePath = "D:/tomcat-6.0.26/logs/catalina.2012-04-30.log";
		readFile(srcfilePath);
		
		final String writeFilePath = "D:/test.txt";
		final String[] lines = new String[]{"line1xxssss", "中文测试", "!@#$%^&*()"}; 
		writeFile(writeFilePath, lines, Boolean.TRUE);
		readFile(writeFilePath);
		
		final String targetFilePath = "D:/test-copy.txt";
		copyFile1(srcfilePath, targetFilePath);
		copyFile2(srcfilePath, targetFilePath);
	}

	/**
	 *
	 * <br>------------------------------<br>
	 * @param srcfilePath
	 * @param targetPath
	 * @throws IOException 
	 */
	private static void copyFile2(String srcfilePath, String targetPath) throws IOException {
		File file = new File(targetPath);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		FileInputStream fileInputStream = new FileInputStream(srcfilePath);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		FileChannel inChannel = fileInputStream.getChannel();
		FileChannel outChannel = fileOutputStream.getChannel();
		//两者等价
//		inChannel.transferTo(0, inChannel.size(), outChannel);
		outChannel.transferFrom(inChannel, 0, inChannel.size());
		
		close(fileOutputStream);
		close(fileInputStream);
		close(inChannel);
		close(outChannel);
	}

	/**
	 *
	 * <br>------------------------------<br>
	 * @param srcfilePath
	 * @param targetPath
	 * @throws IOException 
	 */
	private static void copyFile1(String srcfilePath, String targetPath) throws IOException {
		File file = new File(targetPath);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		FileInputStream fileInputStream = new FileInputStream(srcfilePath);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		FileChannel inChannel = fileInputStream.getChannel();
		FileChannel outChannel = fileOutputStream.getChannel();
		ByteBuffer inBuffer = ByteBuffer.allocate(BUFFER_CAPACITY);
		while (inChannel.read(inBuffer) != -1) {
			inBuffer.flip();
			outChannel.write(inBuffer);
			inBuffer.clear();
		}
		close(fileOutputStream);
		close(fileInputStream);
		close(inChannel);
		close(outChannel);
	}

	/**
	 * <br>------------------------------<br>
	 * @param writeFilePath
	 * @param lines
	 * @param append
	 * @throws IOException
	 */
	private static void writeFile(String writeFilePath, String[] lines, boolean append) throws IOException {
		File file = new File(writeFilePath);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		FileOutputStream fileOutputStream = new FileOutputStream(file, append);
		FileChannel fileChannel = fileOutputStream.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY);
		for (String line : lines) {
			buffer.put(line.getBytes());
			buffer.put("\r\n".getBytes());
			buffer.flip();
			fileChannel.write(buffer);
			buffer.clear();
		}
		close(fileOutputStream);
		close(fileChannel);
	}
	
	/**
	 * <br>------------------------------<br>
	 * @param path
	 * @throws IOException
	 */
	private static void readFile(String path) throws IOException {
		if (isFileNotExists(path)) {
			throw new FileNotFoundException();
		}
		FileInputStream fileInputStream = new FileInputStream(path);
	 	FileChannel fileChanne = fileInputStream.getChannel();
	 	ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY);
	 	while (fileChanne.read(buffer) != -1) {
	 		buffer.flip();
	 		System.out.println(charset.decode(buffer));
	 		buffer.clear();
		}
	 	close(fileInputStream);
	 	close(fileChanne);
	}
	
	private static boolean isFileNotExists(String path) {
		File file = new File(path);
		return !file.exists();
	}
	
	/**
	 * 
	 * <br>------------------------------<br>
	 * @param outputStream
	 */
	private static void close(OutputStream outputStream) {
		if (outputStream == null) return;
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * <br>------------------------------<br>
	 * @param channel
	 */
	private static void close(Channel channel) {
		if (channel == null ) return;
		try {
			channel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * <br>------------------------------<br>
	 * @param inputStream
	 */
	private static void close(InputStream inputStream) {
		if (inputStream == null) return;
		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
### 回答1: FileChannel是Java NIO中的一个类,它用于从文件中读取数据或向文件写入数据。FileChannel是非阻塞的,这意味着当调用FileChannel的读写方法时,线程不会被阻塞。相反,它会立即返回一个Future对象,你可以在后面调用这个对象的get()方法来获取实际读写操作的结果。 例如,你可以使用FileChannel来读取文件的内容,代码如下: ``` Path path = Paths.get("file.txt"); FileChannel channel = FileChannel.open(path, StandardOpenOption.READ); ByteBuffer buffer = ByteBuffer.allocate(1024); Future<Integer> result = channel.read(buffer, 0); int bytesRead = result.get(); ``` 你也可以使用FileChannel向文件写入数据,代码如下: ``` Path path = Paths.get("file.txt"); FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE); ByteBuffer buffer = ByteBuffer.wrap("Hello, World!".getBytes()); Future<Integer> result = channel.write(buffer, 0); int bytesWritten = result.get(); ``` 需要注意的是,FileChannel只能用于文件,如果你想要从网络或其他非文件源读取数据,你需要使用Java NIO中的其他类,例如SocketChannel或ServerSocketChannel。 ### 回答2: FileChannel是Java NIO中表示文件的通道。它是对文件进行读取和写入的通道,类似于传统IO中的InputStream和OutputStream。 通过FileChannel,我们可以使用缓冲区(Buffer)来读取和写入文件的数据。我们可以创建一个FileChannel对象来操作文件,这可以通过FileInputStreamFileOutputStream或RandomAccessFile等类的getChannel()方法来获取。 FileChannel支持阻塞式和非阻塞式的IO操作。我们可以使用read()方法从通道中读取数据,使用write()方法将数据写入通道。还可以使用transferTo()和transferFrom()方法在通道之间直接传输数据,避免了传统IO中需要使用缓冲区来进行数据复制的过程。 FileChannel还提供了其他的一些有用的方法,比如position()和position(long newPosition)用于获取和设置文件指针的位置;size()方法用于获取文件的大小;truncate(long size)用于截断文件等。 在操作文件时,通常需要首先打开一个FileChannel,然后创建一个缓冲区来进行数据的读取和写入。一般的操作流程是,首先从通道中读取数据到缓冲区中,然后进行相应的处理,最后将数据写入到文件中。 总之,FileChannel是Java NIO中用于操作文件的通道,它提供了更加灵活和高效的文件操作方式。通过FileChannel,我们可以使用缓冲区来读取和写入文件的数据,还可以直接在通道之间传输数据。同时,FileChannel还提供了一些方便的方法来操作文件的指针和大小等属性。使用FileChannel可以提高文件IO操作的效率和灵活性。 ### 回答3: NIO(New Input/Output)中的FileChannel是用于文件的通道类,它提供了一种在文件中读取、写入和操作数据的方式。 FileChannel可以通过Java程序对文件进行各种操作,包括读取文件内容、写入数据到文件,以及对文件指定位置进行读写操作等。 FileChannel的一些主要功能包括: 1. 读取文件内容:可以使用FileChannel的read()方法来读取文件中的数据,可以一次读取一定数量的字节,也可以读取整个文件的内容。 2. 写入文件内容:使用FileChannel的write()方法,可以将数据写入文件,可以一次写入一定数量的字节,也可以覆盖原有内容或者追加到文件末尾。 3. 文件位置操作:FileChannel的position()方法可以获取当前位置,也可以通过position(long newPosition)方法设置新的位置。可以根据实际需要在文件中进行定位,进行读写操作。 4. 通道之间的传输:FileChannel提供了transferFrom()和transferTo()方法用于直接在通道之间传输数据,可以高效地传输大量数据。 5. 文件的锁定:可以使用FileChannel的lock()和tryLock()方法对文件进行锁定,确保多个线程或进程之间对同一个文件的读写操作的互斥性。 总之,FileChannelNIO中用于处理文件的通道,它提供了一系列的API方法,可以方便地读取、写入和操作文件中的数据,同时还提供了一些文件的操作特性,如文件位置操作、通道之间的数据传输以及文件的锁定等。使用FileChannel可以更灵活地对文件进行操作,并且相比于传统的IO操作,它在性能上可以得到一定的提升。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值