「javanio文档」Java jni

博主:adminadmin 2023-01-06 22:57:05 679

今天给各位分享javanio文档的知识,其中也会对Java jni进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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

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

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

实现方法:

        FileChannel fc=raf.getChannel();

       //一次读取文件,读取的字节缓存数

       ByteBuffer fbb=ByteBuffer.allocate(1024*5);

        fc.read(fbb);

        fbb.flip();

     //每行缓存的字节   根据你的实际需求     

      ByteBuffer bb=ByteBuffer.allocate(500);

   

              //判断是否读完文件

public boolean hasNext() throws IOException {

        if(EOF)return false;

        if(fbb.position()==fbb.limit()){//判断当前位置是否到了缓冲区的限制

            if(readByte()==0)  return false;

        }

        while(true){

            if(fbb.position()==fbb.limit()){

                if(readByte()==0)  break;

            }

            byte a=fbb.get();

            if(a==13){

                if(fbb.position()==fbb.limit()){

                    if(readByte()==0)  break;

                }

                return true;

            }else{

                if (bb.position()  bb.limit()) {

                    bb.put(a);

                }else {

                    if(readByte()==0)  break;

                }

            }

        }

        return true;

    }

我用JAVA NIO 来复制文件,但没有发现和传统的流的方式来操作有明显的优势,为什么呢?

好好读读Thinking in java文档,从1.5开始,Java对InputStream/OutputStream 进行了重新改写,用的就是NIO,因此,就算你不显示声明要用NIO,只要你的类继承了InputStream/OutputStream就已经在用NIO了,不信的话这样做

FileChannel channel=new FileInputStream.getChannel();

如果XXStream不用NIO构造,如何返回一个Channel的对象?

介绍一下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();

}

}

关于javanio文档和Java jni的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。