導航:首頁 > 編程語言 > 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