繁体   English   中英

Android SDK-无法写入/读取文件

[英]Android SDK - Cannot write/read files

我是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

确保您对manifest.xml文件中的应用程序具有以下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

您需要授予这些权限来写入文件和从设备读取文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM