【问题标题】:My App doesn't work (BufferedWriter)我的应用程序不起作用(BufferedWriter)
【发布时间】:2018-08-19 07:48:03
【问题描述】:

我为 Android 4.1 或更高版本编写了一个小型演示应用程序,它应该在单击按钮时使用 BufferedWriter 将字符串写入文件,并且应该读取此字符串并将按钮的文本设置为此字符串。我使它变得更加简单以避免出现问题。但它不起作用!

源代码:

`package com.goldenskies.jonathan.readwritedemo;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    String filepath = "/saves/clicker";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 public void write(View view) {
        try {
            File f = new File(filepath);
            BufferedWriter out = new BufferedWriter(new FileWriter(f));
            out.write("Test erfolgreich!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    } public void read(View view) {
        try {
            File f = new File(filepath);
            BufferedReader in = new BufferedReader(new FileReader(f));
            String S = in.readLine();
            Button b = findViewById(R.id.button);
            b.setText(S);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

还有我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/status"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:onClick="write"
android:text="Write"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="32dp"
android:onClick="read"
android:text="Read"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

非常感谢任何帮助。

【问题讨论】:

  • 究竟你将View 传递到方法中,应该读取和写入? “它不起作用”根本不是正确的错误描述(这就是为什么你对你的问题有密切的投票),最好一次进入你的代码:developer.android.com/studio/debug ...并且至少提供相关的logcat输出.
  • 很可能,因为人们只能根据该示例假设,再加上“它不起作用”的抱怨,您的Manifest.xml 可能缺少android.permission.READ_EXTERNAL_STORAGEandroid.permission.WRITE_EXTERNAL_STORAGE。跨度>

标签: java android bufferedreader bufferedwriter


【解决方案1】:

您的文件路径是/saves/clicker。这有两个问题:

  1. 您正在尝试直接访问根文件夹 (/)。这需要root设备和su命令才能访问。

  2. 即使您正在尝试这样做,您首先需要创建一个名为 saves 的新目录。

这里有两种解决方案:

  1. 您使用应用程序的默认存储目录。这可以使用以下代码完成:

    String filepath = "saves";
    ....
    File f = new File(getFilesDir(), filepath);
    f.mkdir();
    f = new File(f, "clicker");
    filepath = f.getPath();
    ....
    
  2. 您使用默认的外部存储目录。这需要AndroidManifest.xml 中定义的权限READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE 才能访问外部存储。代码是:

    String filepath = "saves";
    ....
    File f = new File(Environment.getExternalStorageDirectory(), filepath);
    f.mkdir();
    f = new File(f, "clicker");
    filepath = f.getPath();
    ....
    

您还可以在调用mkdir() 之前检查目录是否已经存在。代码类似于:

if(!f.exists() || !f.isDirectory())
    f.mkdir();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多