-1

Can anyone help me why this error appear and where should i put the old.txt file to be seen by the code ? is there any solution ?

 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)

The code files are bellow:

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>
Amani
  • 37
  • 1
  • 6
  • Use full path instead of only a file name. And know where old.txt is placed. – greenapps May 06 '17 at 22:21
  • @greenapps i have put the file in the same folder that of MainActivity .java – Amani May 06 '17 at 22:24
  • @greenapps what should be the start and end of path ? – Amani May 06 '17 at 22:25
  • If it is in the same folder as MainActivity.java then it is still there: on your PC. And as your app is far away on your Android devive it will not find that file of course. – greenapps May 06 '17 at 22:31
  • So put the file in the assets folder instead. Then change your Android code to read the file from assets. Just google for reading file from assets. – greenapps May 06 '17 at 22:33
  • @greenapps put there are two assets files ! app\build\intermediates\assets or app\build\generated\assets ?? – Amani May 06 '17 at 22:59
  • and then where can i find the generated new.txt file ? – Amani May 06 '17 at 23:34
  • There is only one assets folder to put files in. I have no Android Studio at hand at the moment. So google. Further you should supply a full path for new.txt. So you would know at forehand where the new file would be created. – greenapps May 07 '17 at 11:50

1 Answers1

1

You can read your .txt file from assets folder follow this post. And you can write your file into device storage follow this post P/s: you can not save the file into assets folder

Community
  • 1
  • 1
TuyenNTA
  • 1,194
  • 1
  • 11
  • 19
  • I did that but i cannot find new.txt file in this path /data/user/0/packageName/files/ – Amani May 07 '17 at 10:16
  • You should tell us how you tried to find that file. With a File Explorer app would be impossible indeed. @Amani. – greenapps May 07 '17 at 11:52