我正在使用此代码:
// Open your local db as the input stream
InputStream myInput = null;
try {
myInput = this.getAssets().open("Student info.txt");
// Path to the just created empty db
String outFileName =Environment.getExternalStorageDirectory().getAbsolutePath() + "/Student info.txt";
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
Toast.makeText(getBaseContext(), "Done", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
将资产文件夹中的文件复制到我的SD卡。现在我需要的是,如果sdcard中的文件是空的,那么继续并从资产复制到SD卡。否则,如果SD卡中的文件不为空,请不要复制任何内容。我怎么能做到这一点?谢谢
答案 0 :(得分:0)
如果文件存在且不为空,则运行上面的代码
String outFileName =
`Environment.getExternalStorageDirectory().getAbsolutePath() + "/Student info.txt"`;
File f = new File(outFileName);
if (f.length() == 0) {
Toast.makeText(this, "File does not Exists or empty", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Exists and is not empty", Toast.LENGTH_SHORT).show();
}
OR
File f = new File(outFileName);
if(f.exists()){
//File exist
if (f.length() == 0) {
Toast.makeText(this, "File empty", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "File is not empty", Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(this, "File does not exist", Toast.LENGTH_SHORT).show();
}