带有USB pendrive的Android主板具有延迟写入和创建0字节长文件的功能

带有USB pendrive的Android主板具有延迟写入和创建0字节长文件的功能,android,copy,usb,fwrite,Android,Copy,Usb,Fwrite,我有一个安卓专用主板(A100)这个主板有一些串行端口和USB端口。USB端口可以像主机或客户端一样工作 所以我插上一个USB pendrive,这个工作正常。 我可以写信和读这本书 我的问题: 我创建了一个应用程序,这个应用程序到达pendrive。当我将文件写入pendrive时,系统会指示写入成功。但如果我拔出pendrive并插入电脑,那么我可以识别文件,但长度都为0。 当我在android中停留更长的时间并退出我的应用程序时,文件将是正确的(并且大小也正确) 我认为有一种缓存机制。我能

我有一个安卓专用主板(A100)这个主板有一些串行端口和USB端口。USB端口可以像主机或客户端一样工作

所以我插上一个USB pendrive,这个工作正常。 我可以写信和读这本书

我的问题: 我创建了一个应用程序,这个应用程序到达pendrive。当我将文件写入pendrive时,系统会指示写入成功。但如果我拔出pendrive并插入电脑,那么我可以识别文件,但长度都为0。 当我在android中停留更长的时间并退出我的应用程序时,文件将是正确的(并且大小也正确)

我认为有一种缓存机制。我能做什么?我怎样才能感觉到真正的文件写入? 我认为COPYFILE函数是错误的

public static final String USBDISK = "/mnt/udisk1/disk-1/";
public void call_upload(View v){
    File directory = getDir("data", Context.MODE_PRIVATE);
    File sdcard = new File(consts.USBDISK);
    if ( !copyToExternal(directory, sdcard)) {
            Log.d(consts.logID, "No USB");
    }
}

public boolean copyToExternal(File directory, File sdcard){
    boolean res = true;
    try {
        File newFolder = new File(sdcard, "newfolder");

        if (!newFolder.exists()) {
            newFolder.mkdir();
        }
        try {
            Log.d("Files", "FileName:" + directory.getName());
            File f = directory;
            File file[] = f.listFiles();
            Log.d("Files", "Size: "+ file.length);
            String s;
            for (int i=0; i < file.length; i++)
            {
                s = file[i].getName();
                if (s.substring(0,4).compareTo(consts.xlsNapi.substring(1))==0){
                    consts.copyFile(new File(directory, s),new File(newFolder, s));
                    Log.d("Files", "FileName:" + s);
                } else {
                    Log.d("Files", "Not copied:" + s + "("+s.substring(0,4)+","+consts.xlsNapi.substring(1)+")");
                }
            }

            //consts.copyFile(new File(newFolder, consts.xlsSetup), new File(directory, consts.xlsSetup));
            consts.longHint(this,"Files copied!");
        } catch (Exception ex) {
            res = false;
            System.out.println("ex: " + ex); //ex: java.io.FileNotFoundException: /storage/sdcard0/cashregister_v1/setup.xls: open failed: ENOENT (No such file or directory)
        }
    } catch (Exception e) {
        System.out.println("e: " + e);
        res = false;
    }
    return res;
}
public static boolean copyFile(File src,File dst)throws IOException{
    if(src.getAbsolutePath().toString().equals(dst.getAbsolutePath().toString())){

        return true;

    }else{
        InputStream is=new FileInputStream(src);
        OutputStream os=new FileOutputStream(dst);
        byte[] buff=new byte[1024];
        int len;
        while((len=is.read(buff))>0){
            os.write(buff,0,len);
        }
        is.close();
        os.flush();
        os.close();
    }
    return true;
}
公共静态最终字符串USBDISK=“/mnt/udisk1/disk-1/”;
公共无效呼叫上传(视图五){
File directory=getDir(“数据”,Context.MODE_PRIVATE);
文件sdcard=新文件(consts.USBDISK);
如果(!copyToExternal(目录,SD卡)){
Log.d(consts.logID,“无USB”);
}
}
公共布尔copyToExternal(文件目录、文件SD卡){
布尔res=真;
试一试{
File newFolder=新文件(SD卡,“newFolder”);
如果(!newFolder.exists()){
mkdir();
}
试一试{
Log.d(“文件”,“文件名:”+目录.getName());
文件f=目录;
File File[]=f.listFiles();
Log.d(“文件”,“大小:”+文件.length);
字符串s;
for(int i=0;i0){
写操作系统(buff,0,len);
}
is.close();
os.flush();
os.close();
}
返回true;
}