android studio FileNotFoundException :(没有这样的文件或目录)

时间:2017-05-06 22:17:42

标签: java android bufferedreader filenotfoundexception bufferedwriter

任何人都可以帮助我出现此错误的原因以及我应该在哪里放置代码可以看到的old.txt文件?有什么解决方案吗?

 W/System.err: java.io.FileNotFoundException: old.txt (No such file or directory)
W/System.err:     at java.io.FileInputStream.open(Native Method)
  W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:146)
 W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:99)
  W/System.err:     at java.io.FileReader.<init>(FileReader.java:58)
   W/System.err:     at FileUtils.readFileAsString(FileUtils.java:23)
   W/System.err:     at MainActivity.onclick(MainActivity.java:33)
    W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
   W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
   W/System.err:     at android.view.View.performClick(View.java:5637)
    W/System.err:     at android.view.View$PerformClick.run(View.java:22429)
   W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
   W/System.err:     at android.os.Looper.loop(Looper.java:154)
    W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6121)
   W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

代码文件如下:

MainActivity .java

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.io.IOException;
import java.io.FileNotFoundException;


public class MainActivity extends AppCompatActivity {

private Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    setContentView(R.layout.activity_main);

}

public void onclick(View view) {

    FileUtils fu = new FileUtils();

    try {
        fu.writeFile("new.txt", fu.readFileAsString("old.txt"));

    } catch (FileNotFoundException f) {
    f.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    TextView displayTextView = (TextView) findViewById(R.id.display);
    displayTextView.setText("Done!");
}

}

FileUtils .java

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 FileUtils {

    /**
     * Opens and reads a file, and returns the contents as one String.
     */
    public static String readFileAsString(String filename)
            throws IOException
    {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        reader.close();
        return sb.toString();
    }


    /**
     * Save the given text to the given filename.
     * @param canonicalFilename Like /Users/al/foo/bar.txt
     * @param text All the text you want to save to the file as one String.
     * @throws IOException
     */
    public static void writeFile(String canonicalFilename, String text)
            throws IOException
    {
        File file = new File (canonicalFilename);
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(text);
        out.close();
    }

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">

<Button
    android:id="@+id/display_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_vertical_margin"
    android:text="Display"
    android:onClick="onclick"

    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Result: "
    android:layout_marginTop="32dp"
    android:layout_marginLeft="@dimen/activity_vertical_margin"/>

<TextView
    android:id="@+id/display"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:text=""
    android:background="#e3e3e3"
    style="@style/text_size" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以按照this post从资源文件夹中读取.txt文件。您可以按照this post将文件写入设备存储 P / s:您无法将文件保存到资源文件夹