【问题标题】:android studio FileNotFoundException: (No such file or directory)android studio FileNotFoundException:(没有这样的文件或目录)
【发布时间】:2017-05-06 22:17:41
【问题描述】:

谁能帮助我为什么会出现这个错误,我应该把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>

【问题讨论】:

  • 使用完整路径而不是仅使用文件名。并且知道 old.txt 放在哪里。
  • @greenapps 我已将文件放在 MainActivity .java 的同一文件夹中
  • @greenapps 路径的开始和结束应该是什么?
  • 如果它与 MainActivity.java 位于同一文件夹中,那么它仍然存在:在您的 PC 上。由于您的应用在您的 Android 设备上很远,因此它当然不会找到该文件。
  • 所以把文件放在 assets 文件夹中。然后更改您的 Android 代码以从 assets 中读取文件。只是谷歌从资产中读取文件。

标签: java android bufferedreader filenotfoundexception bufferedwriter


【解决方案1】:

您可以从资产文件夹中读取您的 .txt 文件,关注 this post。您可以将文件写入设备存储关注this post P/s:不能将文件保存到 assets 文件夹中

【讨论】:

  • 我这样做了,但在此路径 /data/user/0/packageName/files/ 中找不到 new.txt 文件
  • 您应该告诉我们您是如何尝试找到该文件的。使用文件资源管理器应用程序确实是不可能的。 @Amani。
猜你喜欢
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
  • 2020-04-28
  • 1970-01-01
  • 2014-10-25
相关资源
最近更新 更多