【发布时间】: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