Android Studio一直告诉我无法找到该文件

时间:2016-04-28 10:24:50

标签: java android ioexception

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    Button mBtn1 = (Button) findViewById(R.id.start_Quit_button);
    mBtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("clicks", "You Clicked B1");
            Intent i = new Intent(
                    StartActivity.this,
                    MainActivity.class);
            startActivity(i);
        }
    });
    Button mBtn2 = (Button) findViewById(R.id.start_Next_button);
    mBtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("clicks", "You Clicked B2");
            TextView username = (TextView) findViewById(R.id.login_text);

            File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog");
            if (!root.exists()) {
                root.mkdir();
            }

            Username = username.getText().toString();


            save(root, Username);

            Intent i = new Intent(
                    StartActivity.this,
                    Test1_0.class);
            startActivity(i);
        }
    });
}

public void save(File dir, String string) {
    try {
        File filepath = new File(dir, Username + ".txt");
        FileWriter writer = new FileWriter(filepath);
        writer.append(string);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

这是我的代码。正如您所看到的,我希望第二个按钮转到下一个活动并保存我从editText start_Next_button获得的文本。还有一个save方法将字符串写入创建的文件中。 但是,当我按下AVD上的按钮时,android studio会显示:

04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: java.io.FileNotFoundException: /sdcard/ExperimentLog/Name.txt: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at java.io.FileWriter.<init>(FileWriter.java:42)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at com.android.mick.encodingaidingexperiment.StartActivity.save(StartActivity.java:74)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at com.android.mick.encodingaidingexperiment.StartActivity$2.onClick(StartActivity.java:61)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at android.view.View.performClick(View.java:5198)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at android.os.Looper.loop(Looper.java:148)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at libcore.io.Posix.open(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err:    ... 14 more
BTW我使用AVD galaxy Nexus运行android 6.0

3 个答案:

答案 0 :(得分:1)

尝试在eb upgrade之后添加txtAtiv.Text = dataGridView1.Rows[0].Cells[1].Value.ToString(); string value = dataGridView1.Rows[0].Cells[2].Value.ToString(); lblLeft.Text = value.Split(' ')[1]; ,然后/ExperimentLog

答案 1 :(得分:0)

首先,异常是关于文件不存在,因此如果目录中不存在该文件夹和文件,则需要创建该文件夹和文件。 你需要一个try catch块来处理FileNotFoundException。 如果不存在,您也可以使用Files.createFile(thePath);创建文件。

答案 2 :(得分:0)

试试这个

File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog"+".txt");

而不是

File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog");