android无法读写文件,Android SDK - 无法读/写文件

我对Android SDK非常陌生,无法读写文件。请注意,我对Java也很陌生。该程序应该简单地将一个字符串写入一个文件,读取相同的字符串,然后将TextView更改为该读取的字符串。程序运行但不改变TextView。有人可以告诉我我做错了什么吗?谢谢。Android SDK - 无法读/写文件

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中

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" >

android:id="@+id/getMe"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

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

+1

请确保您在AndroidManifest.xml中请求写入权限: –

+1

发布您的logcat。 –

+0

@Mike M.否,不要请求该权限,因为涉及的文件不在外部存储器上。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值