首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问android随机访问文件

访问android随机访问文件
EN

Stack Overflow用户
提问于 2015-08-06 11:53:35
回答 1查看 4K关注 0票数 0

我正在使用下面的代码来读/写一个随机访问文件。这段代码是从一个教程站点复制的。我总是打IOExeption。我正在使用的测试文件被填充到每个地方(源、资产、根目录)。我搜索了互联网网站,所有人都同意访问随机访问文件的想法。我不知道文件的命名是否有问题,或者文件放在哪里,或者是我不知道的东西。任何帮助都是非常感谢的。提前谢谢。

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-11-12 16:40:15

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

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

代码语言:javascript
复制
File f = new File(getFilesDir(), "test.txt");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31846542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档