jjzjj

FileChannel

全部标签

java - 如何在 Java 中内存映射(mmap)一个 linux block 设备(例如/dev/sdb)?

我可以使用java.nio使用Java读/写linuxblock设备。以下代码有效:Pathfp=FileSystems.getDefault().getPath("/dev","sdb");FileChannelfc=null;try{fc=FileChannel.open(fp,EnumSet.of(StandardOpenOption.READ,StandardOpenOption.WRITE));}catch(Exceptione){System.out.println("Erroropeningfile:"+e.getMessage());}ByteBufferbuf=Byt

java - 如何使用 FileChannel 将一个文件的内容附加到另一个文件的末尾?

文件a.txt看起来像:ABC文件d.txt看起来像:DEF我正在尝试获取“DEF”并将其附加到“ABC”,因此a.txt看起来像ABCDEF我尝试过的方法总是完全覆盖第一个条目,所以我总是以:DEF以下是我试过的两种方法:FileChannelsrc=newFileInputStream(dFilePath).getChannel();FileChanneldest=newFileOutputStream(aFilePath).getChannel();src.transferTo(dest.size(),src.size(),dest);...我试过了FileChannelsrc=

java - 从 FileChannel (Java NIO) 读取 GZIP 文件

我需要读取/解压给定FileChannel的.gz文件。我尝试过使用GZIPInputStream提取GZIP文件,但这不会占用FileChannel。我无权访问从中获取FileChannel的原始FileInputStream。如果有人能告诉我从FileChannel读取GZIP的好方法(或至少任何方法),我将不胜感激。改编自SunOracleforums上的一个问题. 最佳答案 您可以获得一个环绕FileChannel的InputStream:FileChannelfc=...GZIPInputStreamgis=newGZIP

java - 使用 Java NIO 直接访问 Windows 磁盘

我正在使用一个使用JavaNIO的库来直接将文件映射到内存,但我无法直接读取磁盘。我可以直接使用带有UNC的FileInputStream读取磁盘,比如Filedisk=newFile("\\\\.\\PhysicalDrive0\\");try(FileInputStreamfis=newFileInputStream(disk);BufferedInputStreambis=newBufferedInputStream(fis)){byte[]somebytes=newbyte[10];bis.read(somebytes);}catch(Exceptionex){System.o

java - FileChannel.transferTo 用于 Windows 中的大文件

使用JavaNIO使用可以更快地复制文件。我主要通过互联网找到了两种方法来完成这项工作。publicstaticvoidcopyFile(FilesourceFile,FiledestinationFile)throwsIOException{if(!destinationFile.exists()){destinationFile.createNewFile();}FileChannelsource=null;FileChanneldestination=null;try{source=newFileInputStream(sourceFile).getChannel();desti

java - 在 Java 中连接两个大文件(超过 1.5GB)的最有效(最快)的方法是什么?

我使用了此处的技术并在70秒内连接了两个1.5GB的文件。http://nadeausoftware.com/articles/2008/02/java_tip_how_read_files_quickly我的代码涉及使用具有内存映射的FileChannels和具有8KB缓冲区大小的ByteBuffers。我怎样才能提高这个速度?Filefile=newFile(binDirectory+"/donjon.avi");FileoFile=newFile(binDirectory+"/donjon2.avi");FileInputStreamis=newFileInputStream(f

java - 从 FileChannel 读取所有行到字符串流

对于我的特定任务,我需要从FileChannel中读取数据到Stream(或Collection)属于String的。在常规NIO对于Path我们可以使用一个方便的方法Files.lines(...)返回Stream.我需要得到相同的结果,但是来自FileChannel而不是Path:publicstaticStreamlines(finalFileChannelchannel){//...}有什么办法吗? 最佳答案 我假设您希望在返回的Stream关闭时关闭channel,所以最简单的方法是publicstaticStreamli

java - 没有通过 FileInputStream 获取文件?

我正在尝试检查文件位置,以免它被覆盖。为此,我必须使用FileInputStream,因为它有一个可以与FileChannel一起使用的方法position()。BufferedReader不保持位置。我的代码是:FileChannelfc=null;FileInputStreamfis=null;inti=0;longpos;charc;fis=newFileInputStream("File.txt");while((i=fis.read())!=-1){fc=fis.getChannel();pos=fc.position();c=(char)i;System.out.print

android - 复制图像,丢失 Exif 数据

我正在将图像复制到私有(private)目录,如下所示:FileChannelsource=null;FileChanneldestination=null;source=newFileInputStream(sourceFile).getChannel();destination=newFileOutputStream(destFile).getChannel();destination.transferFrom(source,0,source.size());source.close();destination.close();..但是当我稍后将它原封不动地插入画廊时:privat

java - DocumentFile 随机访问文件

有没有办法从给定的DocumentFile中获取RandomAccessFile?我知道可以通过getUri获取InputStreamInputStreaminputStream=getContentResolver().openInputStream(DocumentFile.getUri());但我需要一个RandomAccessFile谢谢你的帮助,詹斯 最佳答案 对于SDK21(Lollipop)来说,获得对SD卡上文件的随机读/写访问权限的唯一方法似乎如下:importandroid.content.Context;imp