【问题标题】:access android randomaccessfile访问android随机访问文件
【发布时间】:2015-08-06 03:53:35
【问题描述】:

我正在使用以下代码来读取/写入随机访问文件。此代码是从教程站点之一复制的。我总是遇到 IOExeption。我正在使用的测试文件填充在每个位置(源、资产、根目录)。我搜索了互联网站点,并且都同意访问随机访问文件的想法。我不知道文件命名或放置文件的位置是否有问题,或者可能是我不知道的问题。任何帮助表示赞赏。提前致谢。

package com.example.xper;

import java.io.IOException;
import java.io.RandomAccessFile;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main();
    }

    public static void main() {
        try {
            // create a new RandomAccessFile with filename test
            RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
            // write something in the file
            raf.writeUTF("Hello World");
            // set the file pointer at 0 position
            raf.seek(0);
            // print the string
            System.out.println("" + raf.readUTF());
            // set the file pointer at 5 position
            raf.seek(5);
            // write something in the file
            raf.writeUTF("This is an example");
            // set the file pointer at 0 position
            raf.seek(0);
            // print the string
            System.out.println("" + raf.readUTF());
            raf.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

【问题讨论】:

  • 我点击了下面的链接并且能够让它工作。 link

标签: android randomaccessfile


【解决方案1】:

您的问题似乎是文件位置。您可以阅读有关可以在 android here 上保存文件的位置。

简而言之,这应该可以解决您的问题:

File f = new File(getFilesDir(), "test.txt");
RandomAccessFile raf = new RandomAccessFile(f, "rw");

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多