Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅当文件不为空时才将文件从资产复制到资产_Java_Android - Fatal编程技术网

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的路径
???为什么要创建一个空文件?毫无意义。你在哪里这么做的?我没有看到它的代码。