我正在使用下面的代码来读/写一个随机访问文件。这段代码是从一个教程站点复制的。我总是打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();
}
}
}发布于 2015-11-12 16:40:15
您的问题似乎是文件位置的问题。你可以在android here上阅读你可以保存文件的位置。
简而言之,这应该可以解决您的问题:
File f = new File(getFilesDir(), "test.txt");
RandomAccessFile raf = new RandomAccessFile(f, "rw");https://stackoverflow.com/questions/31846542
复制相似问题