Java openAssetFileDescriptor不会截断OneDrive文件
使用以下代码,生活在Google Drive上的文本文件会按照预期首先擦除,只在文件中留下新写入的内容。如果文件位于OneDrive上,则会覆盖前x个字节,其余原始字节保持不变。有人知道OneDrive文件的解决方案吗。我需要删除旧内容,只保留新内容 根据这些文件 及 这就是应该发生的事情 我已经用Java/Android Studio和C#/Xamarin,Android phone 9 api 28尝试过了Java openAssetFileDescriptor不会截断OneDrive文件,java,android,file-io,storage,onedrive,Java,Android,File Io,Storage,Onedrive,使用以下代码,生活在Google Drive上的文本文件会按照预期首先擦除,只在文件中留下新写入的内容。如果文件位于OneDrive上,则会覆盖前x个字节,其余原始字节保持不变。有人知道OneDrive文件的解决方案吗。我需要删除旧内容,只保留新内容 根据这些文件 及 这就是应该发生的事情 我已经用Java/Android Studio和C#/Xamarin,Android phone 9 api 28尝试过了 public void saveFile(View view) { try
public void saveFile(View view)
{
try
{
AssetFileDescriptor pfd = getContentResolver().openAssetFileDescriptor(fileUri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
fileOutputStream.write(("Overwritten again " + System.currentTimeMillis() + "\n").getBytes());
fileOutputStream.close();
pfd.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
无法找到一个为什么它不能像文档中描述的那样工作,但我已经找到了一个模拟预期行为的解决方案。如果有人看到更好的方法,我不介意评论
public void saveFile(View view)
{
try
{
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(fileUri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
//Added if-block to simulate automatic truncate when file located on onedrive.
if( (fileUri.toString()).contains("skydrive"))
{
FileChannel fileChannel = fileOutputStream.getChannel();
fileChannel.truncate(0);
}
fileOutputStream.write(("Overwritten again " + System.currentTimeMillis() + "\n").getBytes());
fileOutputStream.close();
pfd.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
尝试使用以下命令,只需将“w”替换为“rwt”即可截断原始文件
getContentResolver().openAssetFileDescriptor(fileUri, "rwt")
您是否尝试过
getContentResolver().openFileDescriptor()
?结果相同。已更改--AssetFileDescriptor pfd=getContentResolver()。openAssetFileDescriptor(fileUri,“w”);--to--ParcelFileDescriptor pfd=getContentResolver().openFileDescriptor(fileUri,“w”);还尝试了(相同的结果)-OutputStream OutputStream=getContentResolver().openOutputStream(fileUri,“w”);outputStream.write((“再次覆盖”+System.currentTimeMillis()+“\n”).getBytes());