从Java 1.4开始添加了一种新的处理I/O操作的方法,称为NIO(新的I/O)。NIO API 提供了一种处理I/O操作的基于通道(Channel)的方法,可以用来查看并解决某种特定类型的I/O操作。NIO并没有取代java.io中的I/O类,相反,NIO类对标准的I/O系统做了补充,提供了另一种方法。
以下是NIO对文件操作的简单例子:
package
com.simon.test.javanio.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
public class FileIOTest ... {
private static Log log = LogFactory.getLog(FileIOTest.class);
@Test
public void testRead()
...{
FileInputStream fIn;
FileChannel fChan;
long fSize;
ByteBuffer bBuf;
try ...{
fIn = new FileInputStream("test.txt");
fChan = fIn.getChannel();
fSize = fChan.size();
bBuf = ByteBuffer.allocate((int)fSize);
// Read the file into Buffer.
fChan.read(bBuf);
bBuf.rewind();
for(int i=0; i<fSize; i++)
System.out.println((char)bBuf.get());
fChan.close();
fIn.close();
} catch (FileNotFoundException e) ...{
log.error(e.getMessage());
} catch (IOException ioExcep) ...{
log.error(ioExcep.getMessage());
}
}
@Test
public void testMapRead()
...{
FileInputStream fIn;
FileChannel fChan;
long fSize;
MappedByteBuffer bBuf;
try ...{
fIn = new FileInputStream("test.txt");
fChan = fIn.getChannel();
fSize = fChan.size();
bBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
for(int i=0; i<fSize; i++)
System.out.println((char)bBuf.get());
fChan.close();
fIn.close();
} catch (FileNotFoundException fileExcep) ...{
log.error(fileExcep.getMessage());
} catch (IOException ioExcep) ...{
log.error(ioExcep.getMessage());
}
}
@Test
public void testFileCopy()
...{
FileInputStream fIn;
FileOutputStream fOut;
FileChannel fIChan, fOChan;
long fSize;
MappedByteBuffer bBuf;
try ...{
fIn = new FileInputStream("test.txt");
fOut = new FileOutputStream("testOut.txt");
fIChan = fIn.getChannel();
fOChan = fOut.getChannel();
fSize = fIChan.size();
bBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
fOChan.write(bBuf);
fIChan.close();
fIn.close();
fOChan.close();
fOut.close();
} catch (FileNotFoundException fileExcep) ...{
log.error(fileExcep.getMessage());
} catch (IOException ioExcep) ...{
log.error(ioExcep.getMessage());
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
public class FileIOTest ... {
private static Log log = LogFactory.getLog(FileIOTest.class);
@Test
public void testRead()
...{
FileInputStream fIn;
FileChannel fChan;
long fSize;
ByteBuffer bBuf;
try ...{
fIn = new FileInputStream("test.txt");
fChan = fIn.getChannel();
fSize = fChan.size();
bBuf = ByteBuffer.allocate((int)fSize);
// Read the file into Buffer.
fChan.read(bBuf);
bBuf.rewind();
for(int i=0; i<fSize; i++)
System.out.println((char)bBuf.get());
fChan.close();
fIn.close();
} catch (FileNotFoundException e) ...{
log.error(e.getMessage());
} catch (IOException ioExcep) ...{
log.error(ioExcep.getMessage());
}
}
@Test
public void testMapRead()
...{
FileInputStream fIn;
FileChannel fChan;
long fSize;
MappedByteBuffer bBuf;
try ...{
fIn = new FileInputStream("test.txt");
fChan = fIn.getChannel();
fSize = fChan.size();
bBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
for(int i=0; i<fSize; i++)
System.out.println((char)bBuf.get());
fChan.close();
fIn.close();
} catch (FileNotFoundException fileExcep) ...{
log.error(fileExcep.getMessage());
} catch (IOException ioExcep) ...{
log.error(ioExcep.getMessage());
}
}
@Test
public void testFileCopy()
...{
FileInputStream fIn;
FileOutputStream fOut;
FileChannel fIChan, fOChan;
long fSize;
MappedByteBuffer bBuf;
try ...{
fIn = new FileInputStream("test.txt");
fOut = new FileOutputStream("testOut.txt");
fIChan = fIn.getChannel();
fOChan = fOut.getChannel();
fSize = fIChan.size();
bBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
fOChan.write(bBuf);
fIChan.close();
fIn.close();
fOChan.close();
fOut.close();
} catch (FileNotFoundException fileExcep) ...{
log.error(fileExcep.getMessage());
} catch (IOException ioExcep) ...{
log.error(ioExcep.getMessage());
}
}
}