Java 仅当文件不为空时才将文件从资产复制到资产
我正在使用以下代码:Java 仅当文件不为空时才将文件从资产复制到资产,java,android,Java,Android,我正在使用以下代码: // 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.getExternalStor
// 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卡。现在我需要的是,如果SD卡中的文件是空的,那么继续从资产复制到SD卡。否则,如果SD卡中的文件不是空的,请不要复制任何内容。我怎样才能做到这一点?谢谢如果文件存在且不是空的,请运行上述代码
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();
}
或
如果文件存在且不为空,请运行上述代码
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();
}
或
检查文件大小?
现在我需要的是,如果SD卡中的文件为空,请继续执行
。不。如果文件还不存在,通常会进行复制。但如果文件已经存在,您确实可以添加文件大小0的检查。get-getExternalStorageDirectory不是micro SD卡。//刚创建的空db的路径
???为什么要创建一个空文件?毫无意义。你在哪里这么做的?我看不到它的代码。请检查文件大小?现在我需要的是,如果SD卡中的文件是空的,请继续
。不。如果文件还不存在,通常会进行复制。但如果文件已经存在,您确实可以添加文件大小0的检查。get-getExternalStorageDirectory不是micro SD卡。//刚创建的空db的路径
???为什么要创建一个空文件?毫无意义。你在哪里这么做的?我没有看到它的代码。