-2

任何人都可以帮助我为什么出现这个错误,我应该把old.txt文件在代码中看到?有没有解决方法?android studio FileNotFoundException:(没有这样的文件或目录)

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> 
+0

使用完整路径而不是仅使用文件名。并知道old.txt的位置。 – greenapps

+0

@greenapps我已经把这个文件放在MainActivity.java文件的相同文件夹里。 – Amani

+0

@greenapps应该是什么路径的开始和结束? – Amani

回答

0

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

+0

我这样做了,但我找不到这个路径中的new.txt文件/ data/user/0/packageName/files/ – Amani

+0

你应该告诉我们你是如何试图找到该文件的。使用File Explorer应用程序确实是不可能的。 @Amani。 – greenapps

相关问题