标签: randomaccessfile

RandomAccessFile.setLength在Java 10(Centos)上慢得多

以下代码

public class Main {
    public static void main(String[] args) throws IOException {
        File tmp = File.createTempFile("deleteme", "dat");
        tmp.deleteOnExit();
        RandomAccessFile raf = new RandomAccessFile(tmp, "rw");
        for (int t = 0; t < 10; t++) {
            long start = System.nanoTime();
            int count = 5000;
            for (int i = 1; i < count; i++)
                raf.setLength((i + t * count) * 4096);
            long time = System.nanoTime() - start;
            System.out.println("Average call time " + time / count / 1000 + " us.");
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java randomaccessfile java-10

20
推荐指数
1
解决办法
562
查看次数

Java中RandomAccessFile的并发性

我正在创建一个RandomAccessFile对象,以便通过多个线程写入文件(在SSD上).每个线程都尝试在文件中的特定位置写一个直接字节缓冲区,并确保线程写入的位置不会与另一个线程重叠:

file_.getChannel().write(buffer, position);
Run Code Online (Sandbox Code Playgroud)

where file_的实例是RandomAccessFilebuffer直接字节缓冲区.

对于RandomAccessFile对象,由于我没有使用fallocate来分配文件,并且文件的长度在变化,这是否会利用底层媒体的并发性?

如果不是,在创建文件时如果没有调用fallocate,使用上述函数是否有任何意义?

java multithreading filechannel randomaccessfile

17
推荐指数
2
解决办法
1671
查看次数

RandomAccessFile支持超过Long?

我目前正在使用一个实例RandomAccessFile来管理一些内存中的数据,但是我的RandomAccessFile实例的大小超过了2 ^ 64字节,所以我不能使用这样的方法seek(),write()因为它们使用Long并且无法管理大于2 ^的地址空间64.那我该怎么办?还有什么我可以使用它支持超过2 ^ 64的地址空间吗?

编辑:提出这个问题的原因:

我有一个Tree数据结构,理论上可以有多达2 ^ 128个节点,我想将这个树存储到一个文件中.每个节点的数据大约为6个字节.所以我想知道如何将这个树存储到文件中.

java randomaccessfile

17
推荐指数
1
解决办法
540
查看次数

Java - 如何有效地编写带有偶然漏洞的顺序文件

我要求将记录写入文件,其中数据根据数字键的值写入文件位置(即搜索位置).例如,如果键为100,我可能会写入位置400.

记录由数字键和一段数据组成.记录不会很大(几个字节).但是,可能会有很多记录(数百万).

有两种可能的情况:

  1. 键是单调增加的.在这种情况下,最好的方法是使用DataOutputStream包装a 进行写入BufferedOutputStream,将缓冲区大小设置为某个数字(例如64k)以最大化I/O吞吐量.

  2. 密钥正在增加,但可能存在较大差距.在这种情况下,使用OutputStream将需要在文件的间隙中写入零.为了避免这种情况,RandomAccessFile它可以更好,因为它可以寻找空隙,如果可以寻找整个块,节省空间.缺点是,据我所知,RandomAccessFile不缓冲,因此这种方法对于顺序密钥来说会很慢.

但是,可能的情况是文件是两者兼而有之.有一系列单调增加的密钥.有一些键间隙很小,其他键间隙很大.

我正在寻找的是一个提供两全其美的解决方案.如果检测到键之间的间隙,则可能在两个I/O模式之间切换.但是,如果有一个标准的Java类可以完成这两件事,那就更好了.我见过FileImageOutputStream,但我不确定这是怎么回事.

请注意,我不是在寻找代码示例(虽然这对于演示复杂的解决方案很有帮助),但这只是一个通用策略.最好知道顺序数据的最佳大小缓冲区大小以及从顺序策略切换到随机访问策略需要的时间点(间隙大小).

编辑:

为了接受答案,我希望保证所提出的解决方案能够处理两者,而不仅仅是它可能.这需要:

  • 确认顺序模式是缓冲的.
  • 确认随机访问模式在文件中留下漏洞.

此外,解决方案需要具有内存效率,因为可能会同时打开许多这些文件.

编辑2

这些文件可以在NAS上.这不是设计,而是简单地认识到在企业环境中,这种架构被大量使用,解决方案可能应该处理它(可能不是最佳的)而不是阻止它的使用.AFAIK,这不应该影响基于write()和的解决方案lseek(),但可能会使一些更深奥的解决方案失效. 

java file-io fileoutputstream randomaccessfile

16
推荐指数
1
解决办法
1287
查看次数

java中的RandomAccessFile是否读取内存中的整个文件?

我需要从大文件中读取最后n行(比如2GB).该文件是UTF-8编码的.

想知道最有效的方法.在java中读取RandomAccessFile,但是seek()方法读取内存中的整个文件.它使用本机实现,因此我无法引用源代码.

java randomaccessfile

11
推荐指数
1
解决办法
9588
查看次数

如何通过为Lollipop提供的API随机访问SD卡上的文件?

我的应用程序使用Java类RandomAccessFile通过SeekableByteChannel接口的实现随机读取/写入SD卡上的文件.现在我需要使用新的Lollipop API为Android 5.0重写它.

我找到了唯一的阅读方式:

InputStream inputStream = getContentResolver().openInputStream(uri);
Run Code Online (Sandbox Code Playgroud)

和写:

ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
Run Code Online (Sandbox Code Playgroud)

从/到新API中的文件.

我希望能够在某个随机位置设置通道,并将字节读/写到该位置.是否可以在新的SDK 21中执行此操作?新的SDK是否意味着获取频道:

FieInputChannel fieInputChannel = fileInputStream.getChannel();
FieOutputChannel fieOutputChannel = fileOutputStream.getChannel();
Run Code Online (Sandbox Code Playgroud)

或其他一些方法?

filechannel android-sdcard randomaccessfile android-5.0-lollipop documentfile

10
推荐指数
1
解决办法
1678
查看次数

如何在Java中的txt文件中写入特定的行号

我正在写我的学校项目,其中要求我读取和写入txt文件.我可以正确读取它们但我只能在附加的FileWriter中写入它们.我希望能够通过首先删除行上的数据然后写入新数据来覆盖行号上的txt文件中的内容.我试图使用这种方法......

public void overWriteFile(String dataType, String newData) throws IOException
{
    ReadFile file = new ReadFile(path);
    RandomAccessFile ra = new RandomAccessFile(path, "rw");
    int line = file.lineNumber(path, dataType);
    ra.seek(line);
    ra.writeUTF(dataType.toUpperCase() + ":" + newData);
}
Run Code Online (Sandbox Code Playgroud)

但我相信搜索方法以字节而不是行号移动.谁能帮忙.提前致谢 :)

PS file.lineNumber方法返回旧数据所在的确切行,因此我已经有了需要写入的行号.

编辑:Soloution发现!谢谢大家:)如果有人有兴趣,我会在下面发布soloution

public void overWriteFile(String dataType, String newData, Team team, int dataOrder) throws IOException
{
    try
    {
        ReadFile fileRead = new ReadFile(path);
        String data = "";
        if(path == "res/metadata.txt")
        {
            data = fileRead.getMetaData(dataType);
        }
        else if(path == "res/squads.txt")
        {
            data = fileRead.getSquadData(dataType, dataOrder);
        } …
Run Code Online (Sandbox Code Playgroud)

java file-io text file randomaccessfile

8
推荐指数
1
解决办法
2万
查看次数

从RandomAccessFile读取数据产生不正确的结果 - Java

我有一个42行的文本文件.每行包含超过22,000个以逗号分隔的数字.

我想从每一行中提取某些数字,并且我有一个长度为1000的int数组,其中包含从这42行中的每一行中需要的1,000个数字.

例如,如果数组包含43,1,3244,则意味着我需要从第一行到第42行开始的第43个数字,第1个数字和第3244个数字.

我的for循环似乎不起作用,它只读取文本文件中第一行有42行220000个数字,我不知道它出错了.

for(int i=0;i<42;i++){ //irretates through the 42 lines of 
    counter=1; // to keep track about on which line the code is working
    System.out.println("Starting line"+i);

    st2=new StringTokenizer(raf1.readLine(),",");
    //raf3 is a RandomAccessFile object containing the 42 lines

    a:while(st2.hasMoreTokens()){
        b=is_there(array2,counter);
        // is_there is a method that compares the rank of the taken being read with 
       //the whole array that has the 1000 numbers that i want. 
        if(b==false){ 
            // if the rank or order of token [e.g. 1st, 432th] we are …
Run Code Online (Sandbox Code Playgroud)

java for-loop stringtokenizer randomaccessfile

7
推荐指数
2
解决办法
886
查看次数

IndexOutofBounds使用Java的读取字节

我在Java 6中使用RandomAccessFile但在读取字节时有一些奇怪的行为.

使用以下代码,在哪里offsetdata适当初始化:

int offset;
byte data[];
randFile.readFully(data, offset, data.length);
Run Code Online (Sandbox Code Playgroud)

我得到以下堆栈跟踪:

null
java.lang.IndexOutOfBoundsException
    at java.io.RandomAccessFile.readBytes(Native Method)
    at java.io.RandomAccessFile.read(RandomAccessFile.java:355)
    at java.io.RandomAccessFile.readFully(RandomAccessFile.java:414)
Run Code Online (Sandbox Code Playgroud)

但与相同的价值观offsetdata,下面的(貌似相同)的代码工作正常!

randFile.seek(offset);

for (int i = 0; i < (data.length); i += 1) {
    data[i] = randFile.readByte();
}
Run Code Online (Sandbox Code Playgroud)

有没有人能够了解为什么会这样?

java indexoutofboundsexception randomaccessfile

5
推荐指数
1
解决办法
2473
查看次数

DocumentFile RandomAccessFile

有没有办法得到RandomAccessFile一个给定的DocumentFile

我知道可以通过输入一个InputStream getUri

InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri());
Run Code Online (Sandbox Code Playgroud)

但我需要一个 RandomAccessFile

谢谢你的帮助,

java android randomaccessfile documentfile

5
推荐指数
1
解决办法
1306
查看次数