检查Android中的文件是否为空 [英] Checking if a File is Blank in Android
问题描述
这是我的代码示例.该代码很长,仅用于测试文件是否为空,如果不是,则将其写入.无论哪种方式,行 if(!(data.equals("))&&!(data.equals(null)))
都不起作用,即使文件为空,它仍然会通过警报.
FileInputStream fIn = null;字符串数据= null; InputStreamReader isr = null;尝试{char [] inputBuffer = new char [1024];fIn = openFileInput("test.txt");isr =新的InputStreamReader(fIn);isr.read(inputBuffer);数据=新的String(inputBuffer);isr.close();fIn.close();} catch(IOException e){}//这是检查从文件输入的数据是否为空if(!(data.equals("))&&!(data.equals(null))){AlertDialog.Builder builder =新的AlertDialog.Builder(Main.this);builder.setMessage(清除文件吗?" +'\ n'+此操作无法撤消.").setCancelable(false).setPositiveButton(是",新的DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int id){我们的EditText =(EditText)findViewById(R.id.txtWrite);FileOutputStream fOut = null;OutputStreamWriter osw = null;尝试{fOut = openFileOutput("test.txt",Context.MODE_PRIVATE);osw =新的OutputStreamWriter(fOut);osw.write(");osw.close();fOut.close();we.setText(");} catch(Exception e){}}}).setNegativeButton("No",新的DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int id){dialog.cancel();}});AlertDialog alert = builder.create();alert.show();}
此外,如果有人可以简化这段代码,那就太好了!
如果文件为空(无内容),则其长度为0.如果文件不存在,则长度也返回0;否则,该长度返回0.如果这是必要的区别,则可以使用 exists
方法检查文件是否存在.
文件f = getFileStreamPath("test.txt");如果(f.length()== 0){//为空或不存在} 别的 {//存在且不为空}
当前方法无法工作,因为 inputBuffer
是一个1024个字符的数组,并且从该缓冲区创建的字符串也将具有1024个字符,而与从文件中成功读取了多少个字符无关./p>
This is my code sample. The code is pretty long just to test if a file is blank and then if it isn't, write onto it. Either way, the line if (!(data.equals("")) && !(data.equals(null)))
doesn't work and even when the file is blank, it still goes through the Alert.
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();
}
Also, if anyone has a way of shorting up this code, I would be greatful!
If a file is blank (has no contents) its length is 0. The length returns also 0 if it doesn't exist; if this is a necessary distinction you can check if the file exists with the exists
method.
File f = getFileStreamPath("test.txt");
if (f.length() == 0) {
// empty or doesn't exist
} else {
// exists and is not empty
}
The current approach fails to work because inputBuffer
is an array of 1024 chars, and a strings created from it will also have 1024 chars, independently of how many chars were successfully read from the file.
这篇关于检查Android中的文件是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!