导航:首页 > 编程语言 > javaniofile

javaniofile

发布时间:2023-12-07 10:06:18

1. 介绍一下java NIO,NIO读取文件都有哪些方法

NIO也就是New I/O,是一组扩展Java IO操作的API集, 于Java 1.4起被引入,Java 7中NIO又提供了一些新的文件系统API,叫NIO2.
NIO2提供两种主要的文件读取方法:
使用buffer和channel类
使胡举吵用Path 和 File 类
NIO读取文件有以下三种方式:
1. 旧的NIO方式,使用BufferedReader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class WithoutNIOExample
{
public static void main(String[] args)
{
BufferedReader br = null;
String sCurrentLine = null;
try
{
br = new BufferedReader(
new FileReader("test.txt"));
while ((sCurrentLine = br.readLine()) != null)
{
System.out.println(sCurrentLine);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
br.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
2. 使用buffer读取小文件
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadFileWithFileSizeBuffer
{
public static void main(String args[])
{
try
{
RandomAccessFile aFile = new RandomAccessFile(
"test.txt","r");
FileChannel inChannel = aFile.getChannel();
long fileSize = inChannel.size();
ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
inChannel.read(buffer);
buffer.rewind();
buffer.flip();
for (int i = 0; i < fileSize; i++)
{
System.out.print((char) buffer.get());
}
inChannel.close();
aFile.close();
}
catch (IOException exc)
{
System.out.println(exc);
System.exit(1);
}
}
}
3. 分块答伍读取大文件
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadFileWithFixedSizeBuffer
{
public static void main(String[] args) throws IOException
{
RandomAccessFile aFile = new RandomAccessFile
("test.txt", "r");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(inChannel.read(buffer) > 0)
{
buffer.flip();
for (int i = 0; i <裤侍 buffer.limit(); i++)
{
System.out.print((char) buffer.get());
}
buffer.clear(); // do something with the data and clear/compact it.
}
inChannel.close();
aFile.close();
}
}
4. 使用MappedByteBuffer读取文件
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class ReadFileWithMappedByteBuffer
{
public static void main(String[] args) throws IOException
{
RandomAccessFile aFile = new RandomAccessFile
("test.txt", "r");
FileChannel inChannel = aFile.getChannel();
MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
buffer.load();?
for (int i = 0; i < buffer.limit(); i++)
{
System.out.print((char) buffer.get());
}
buffer.clear(); // do something with the data and clear/compact it.
inChannel.close();
aFile.close();
}
}

2. 利用java.nio的FileChannel能够实现按行读取文件吗(解决了)

利用java.nio的FileChannel能够实现按行读取文件:

具体思路是:设置两个缓冲区,一大一小,大的缓冲区为每次读取的量,小的缓冲区存放每行的数据(确保大小可存放文本中最长的那行)。读取的时候判断是不是换行符13,是的话则返回一行数据,不是的话继续读取,直到读完文件。

实现方法:

FileChannelfc=raf.getChannel();
//一次读取文件,读取的字节缓存数
ByteBufferfbb=ByteBuffer.allocate(1024*5);
fc.read(fbb);
fbb.flip();
//每行缓存的字节根据你的实际需求
ByteBufferbb=ByteBuffer.allocate(500);

//判断是否读完文件
publicbooleanhasNext()throwsIOException{

if(EOF)returnfalse;
if(fbb.position()==fbb.limit()){//判断当前位置是否到了缓冲区的限制
if(readByte()==0)returnfalse;
}
while(true){
if(fbb.position()==fbb.limit()){
if(readByte()==0)break;
}
bytea=fbb.get();
if(a==13){
if(fbb.position()==fbb.limit()){
if(readByte()==0)break;
}
returntrue;
}else{
if(bb.position()<bb.limit()){
bb.put(a);
}else{
if(readByte()==0)break;
}
}
}
returntrue;
}

3. java如何拷贝文件到另一个目录下

下面列举出4种方式:

1、使用FileStreams复制

这是最经典的方式将一个文件的内容复制到另一个文件中。 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。 这是第一个方法的代码:

阅读全文

与javaniofile相关的资料

热点内容
幼儿编程教育培训多少钱 浏览:404
经常生气有什么东西能解压 浏览:901
代理服务器地址和端口可以怎么填 浏览:63
unity5手游编译模型 浏览:265
安卓无人机app源码 浏览:808
pl1编程语言 浏览:801
台达plc编程换算指令大全 浏览:174
手机上的编程游戏 浏览:108
服务器密码机有什么用 浏览:477
dos磁盘命令 浏览:955
单片机cpu52的功能 浏览:691
opc服务器怎么开发 浏览:373
觅喜是个什么app 浏览:402
加密cd机 浏览:946
社保用什么app缴纳 浏览:313
nodevlinux 浏览:582
腾讯tt服务器怎么登录密码 浏览:898
windows命令提示符 浏览:352
win7管理员权限命令 浏览:729
地图app哪个适合老年人用 浏览:74