如何在覆盖时增加文件大小



是否有办法使文本文件的大小增加,以适应其内容时,覆盖它?当我覆盖一个文件的大小总是保持不变的原始大小。当我需要用比原来包含的字节更多的字节来覆盖它时,这是一个问题:

String actual_name = "/storage/sdcard0/my_file_name.xml";  
FileOutputStream fileos  = new FileOutputStream(actual_name);
/**do a bunch of stuff to create XML string**/
fileos.write(myXMLstring.getBytes());
fileos.close();

在我的例子中,原始文件大小为1016字节。即使我有4k字节的新数据,前1016字节是写入文件的全部内容。顺便说一句,我已经尝试删除文件,如果它存在,然后写入它,但没有帮助:

String fname = "my_file_name.xml";         
File ROOT = Environment.getExternalStorageDirectory();
File file1 = new File(ROOT, fname);
if(!file1.exists()){
    file1.createNewFile();
}
else{
    //returns true but doesn`t change the file size or even the created date
    boolean did_delete = file1.delete();
    if (did_delete){
        file1.createNewFile();
    }
}
String x = file1.getAbsolutePath();
FileOutputStream fileos  = new FileOutputStream(x);

有什么建议吗?

好的,下面是我的发现:

删除:

myFile= new File(Environment.getExternalStorageDirectory(), filename);
myFile.delete();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myFile)));

myFile指向你的FILE。

要实际看到文件名的大小发生了变化,您必须断开USB电缆,然后重新连接。我不确定这是android问题还是Windows问题(在内存中缓存文件)

最新更新