Java android studio FileNotFoundException:(无此类文件或目录)
有人能告诉我为什么会出现这个错误,我应该把Java android studio FileNotFoundException:(无此类文件或目录),java,android,bufferedreader,filenotfoundexception,bufferedwriter,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.<i
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)
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>
您可以从下面的资产文件夹中读取.txt文件。您可以将文件写入设备存储
P/s:无法将文件保存到资产文件夹中请使用完整路径,而不仅仅是文件名。并知道old.txt的位置。@greenapps我已将该文件放在MainActivity的同一文件夹中。java@greenapps路径的开始和结束应该是什么?如果它与MainActivity.java位于同一文件夹中,那么它仍然存在:在您的PC上。并且由于您的应用程序在您的Android设备上很远,它当然找不到该文件。因此,请将改为将文件保存在资产文件夹中。然后更改您的Android代码以从资产读取文件。只需通过谷歌从资产中读取文件。我这样做了,但在路径/data/user/0/packageName/files/中找不到新的.txt文件。您应该告诉我们您是如何找到该文件的。使用文件浏览器应用程序确实是不可能的@阿曼尼。
<?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>