【问题标题】:Checking if a File is Blank in Android在Android中检查文件是否为空白
【发布时间】:2013-07-26 13:50:21
【问题描述】:

这是我的代码示例。代码很长,只是为了测试文件是否为空白,如果不是,则写入该文件。无论哪种方式,if (!(data.equals("")) && !(data.equals(null))) 行都不起作用,即使文件为空白,它仍然会通过警报。

FileInputStream fIn = null;String data = null;InputStreamReader isr = null;
try{
    char[] inputBuffer = new char[1024];
    fIn = openFileInput("test.txt");
    isr = new InputStreamReader(fIn);
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fIn.close();
}catch(IOException e){}

// this is the check for if the data inputted from the file is NOT blank
if (!(data.equals("")) && !(data.equals(null)))
{
    AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
    builder.setMessage("Clear your file?" + '\n' + "This cannot be undone.")
    .setCancelable(false)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            EditText we = (EditText)findViewById(R.id.txtWrite);
            FileOutputStream fOut = null;

            OutputStreamWriter osw = null;
            try{
                fOut = openFileOutput("test.txt", Context.MODE_PRIVATE);
                osw = new OutputStreamWriter(fOut);
                osw.write("");
                osw.close();
                fOut.close();
                we.setText("");
            }catch(Exception e){}
        }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

另外,如果有人有办法缩短这段代码,我会很感激的!

【问题讨论】:

标签: java android file


【解决方案1】:

如果一个文件是空白的(没有内容),它的长度是0。如果它不存在,长度也返回0;如果这是必要的区别,您可以使用exists 方法检查文件是否存在。

File f = getFileStreamPath("test.txt");
if (f.length() == 0) {
    // empty or doesn't exist
} else {
    // exists and is not empty
}

当前的方法不起作用,因为inputBuffer 是一个由 1024 个字符组成的数组,从它创建的字符串也将有 1024 个字符,与从文件中成功读取的字符数无关。

【讨论】:

  • 你确定文件真的是空的吗?它的长度是多少?
  • 啊openFileOutput 是一个Android API 函数,我还以为是你自己写的。它在您可以从getFilesDir 获取的特定目录中创建文件,并且有一个不同的API 函数来获取具有完整路径的文件对象。检查更新。
  • File f = new File(getFilesDir(), "test.txt"); 有效! File f = getFileStreamPath("test.txt"); 也可以使用
【解决方案2】:

试试这个,祝你好运!

File sdcard = Environment.getExternalStorageDirectory();
        File f = new File(sdcard, "/yourfile");

if(!f.exsist()){
f.createNewFile();
//Use outwriter here, outputstream search how to write into a tet file in java code 
}

【讨论】:

  • 好吧,我不是想看看它是否存在,而是想看看它是否为空白。还有,如果你创建了File f = new File(sdcard, "/yourfile");这个文件,那它不是一直存在的吗,因为你刚刚创建了它?
【解决方案3】:

由于您使用的是返回 FileInputStream 的 openFileInput("test.txt"),因此请尝试

FileInputStream fIn = openFileInput("test.txt");
FileChannel channel = fIn.getChannel();

if(channel.size() == 0) {
  // This is empty
}
else {
  // Not empty
}

我没有 Java NIO 经验。

【讨论】:

    猜你喜欢
    • 2015-04-30
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多