導航:首頁 > 編程語言 > 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相關的資料

熱點內容
郝萬山pdf 瀏覽:83
潛行者電影在線觀看 瀏覽:865
php判斷元素在數組中 瀏覽:745
androidphpmd5 瀏覽:691
vr能看什麼電影 瀏覽:163
成龍電影裡面有個龍復活了 瀏覽:106
成人影視免費 瀏覽:369
男的送快遞,女的在按摩的上班的叫什麼電影 瀏覽:753
海綿寶寶大電影免費中文版 瀏覽:276
愛國戰爭片免費觀看 瀏覽:888
三位數碼管單片機是如何工作的 瀏覽:728
免費看不下載老電影院 瀏覽:512
啄木鳥影業都有哪些作品 瀏覽:824
在電腦上怎麼把pdf保存成圖片 瀏覽:767
末段愛情廣播劇是哪個app可以聽 瀏覽:322
e片免費看 瀏覽:361
成龍教外國小孩功夫電影叫什麼 瀏覽:482
disk命令分區 瀏覽:912
丁巴度愛與激情 瀏覽:264
韓國野戰電影 瀏覽:462