【问题标题】:android error : java.io.filenotfoundexception /proc/net/xt_qtaguid/stats + open failed :ENOENT(no such file or directory )android 错误:java.io.filenotfoundexception /proc/net/xt_qtaguid/stats + 打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2013-12-26 10:54:43
【问题描述】:

我正在尝试创建允许用户在输入文件名后保存文件的 android 应用程序,但问题是当用户尝试保存文件时会显示错误消息:

 Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)

然后它保存为 null.txt

的文件

如何解决这个错误???

SignSactivity.java

package com.devleb.idapp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignSoldgerActivity extends Activity {

    EditText edit_txt_note;
    final Context context = this;
    // attribute for the date picker

    public String fileName;
    String userinputResult;

    Button btndatePicker, btn_save_soldger;
    TextView txtatePicker;
    int year, monthofyear, dayofmonth;
    Calendar cal = Calendar.getInstance();
    DateFormat dt = DateFormat.getInstance();

    DatePickerDialog.OnDateSetListener dpd;

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

        edit_txt_note = (EditText) findViewById(R.id.editTxtNote);

        btndatePicker = (Button) findViewById(R.id.btnDateTimePicker);
        btndatePicker.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                new DatePickerDialog(SignSoldgerActivity.this, dpd, cal
                        .get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
                        .get(Calendar.DAY_OF_MONTH)).show();

            }
        });

        txtatePicker = (TextView) findViewById(R.id.txtDate);

        dpd = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, monthOfYear);
                cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                txtatePicker.setText(new StringBuilder().append(year + "/")
                        .append(monthOfYear + "/").append(dayOfMonth));
            }
        };

        btn_save_soldger = (Button) findViewById(R.id.btnSaveSoldger);
        btn_save_soldger.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // / for creating a dialog
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompts, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);

                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.editTextDialogUserInput);

                // set dialog message
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // get user input and set it to result
                                        // edit text
                                        userinputResult = userInput.getText()
                                                .toString();

                                        SimpleDateFormat formatter = new SimpleDateFormat(
                                                "yyyy/MM/dd\\HH:mm:ss");
                                        Date now = new Date();
                                        fileName = formatter.format(now) + "/"
                                                + userinputResult;

                                        saveFile(fileName);
                                        txtatePicker.setText(fileName);
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();
                                    }
                                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }

        });

    }

    // / for saving the file on the SD

    public void saveFile(String fileName) {
        try {
            String sdPath = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/" + fileName + ".txt";

            File myFile = new File(sdPath);
            myFile.createNewFile();

            Toast.makeText(getBaseContext(), "the second step in saving file",
                    Toast.LENGTH_SHORT).show();

            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

            // append or write
            myOutWriter.append(edit_txt_note.getText());
            myOutWriter.close();
            fOut.close();
            edit_txt_note.setText("");
            Toast.makeText(getBaseContext(), "Done Writing SD" + fileName,
                    Toast.LENGTH_SHORT).show();

        } catch (Exception e) {

            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
                    .show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sign_soldger, menu);
        return true;
    }

}

【问题讨论】:

    标签: android error-handling save fileoutputstream


    【解决方案1】:

    它会引发错误,因为您尝试将文件保存在不存在的目录中。我运行了您的代码并使用断点检查字符串fileName 中的值。

    从下面的屏幕截图中,您可以看到它是一个无效的目录,并且问题出在哪里。您可能想要更改目录。

    [编辑]

    更改设置文件名的方式。

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    Date now = new Date();
    fileName = formatter.format(now) + "-" + userinputResult;
    saveFile(fileName);
    

    现在,fileName 将具有当前日期 + 用户输入的文本。

    例如,文件名将是2013-12-26-18-51-26-hello.txt

    另外,请确保您已添加权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    【讨论】:

    • 所以我必须做什么我的意思是文件名的正确形式是什么是不允许的字符??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2016-07-05
    相关资源
    最近更新 更多