我是Android SDK的新手,我无法让文件写入/阅读工作。请注意,我也是Java的新手。该程序应该只是简单地将一个字符串写入一个文件,读取相同的字符串,然后将TextView更改为该读取字符串。该程序运行但不会更改TextView。有人可以告诉我我做错了什么吗?感谢。
MainActivity.java
package com.example.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
public String readFileLine(String filepath) throws java.io.FileNotFoundException, java.io.IOException {
//opens file and returns first line
FileReader fr = new FileReader (new File(filepath));
BufferedReader inputStream = new BufferedReader (fr);
String temp = inputStream.readLine();
inputStream.close();
return temp;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FileOutputStream outputStream_local;
try {
outputStream_local = openFileOutput("local.txt", Context.MODE_PRIVATE);
outputStream_local.write("asdf".getBytes());
outputStream_local.close();
} catch (Exception e) {
e.printStackTrace();
}
TextView temp = (TextView) findViewById(R.id.getMe);
try {
temp.setText(readFileLine(getFilesDir().toString() + "local.txt"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/getMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Logcat输出
[2014-02-08 11:18:11 - Test] ------------------------------
[2014-02-08 11:18:11 - Test] Android Launch!
[2014-02-08 11:18:11 - Test] adb is running normally.
[2014-02-08 11:18:11 - Test] Performing com.example.test.MainActivity activity launch
[2014-02-08 11:18:11 - Test] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-02-08 11:18:14 - Test] Uploading Test.apk onto device '014FD8210301B01A'
[2014-02-08 11:18:14 - Test] Installing Test.apk...
[2014-02-08 11:18:18 - Test] Success!
[2014-02-08 11:18:18 - Test] Starting activity com.example.test.MainActivity on device 014FD8210301B01A
[2014-02-08 11:18:18 - Test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.test/.MainActivity }
[2014-02-08 11:18:38 - Test] ------------------------------
[2014-02-08 11:18:38 - Test] Android Launch!
[2014-02-08 11:18:38 - Test] adb is running normally.
[2014-02-08 11:18:38 - Test] Performing com.example.test.MainActivity activity launch
[2014-02-08 11:18:38 - Test] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-02-08 11:18:41 - Test] Application already deployed. No need to reinstall.
[2014-02-08 11:18:41 - Test] Starting activity com.example.test.MainActivity on device 014FD8210301B01A
[2014-02-08 11:18:41 - Test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.test/.MainActivity }
[2014-02-08 11:18:41 - Test] ActivityManager: Warning: Activity not started, its current task has been brought to the front
答案 0 :(得分:-1)
确保您在manifest.xml文件中为您的应用授予了以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
您需要授予这些权限以写入文件并从设备中读取文件。