Java 文件中的文件已损坏。重命名()
这是这个问题的后续问题:,涉及iText。我创建一个具有不同旋转角度的新Pdf,然后删除旧Pdf并将新Pdf重命名为旧Pdf的名称。我已经确定,调用Java 文件中的文件已损坏。重命名(),java,android,itext,Java,Android,Itext,这是这个问题的后续问题:,涉及iText。我创建一个具有不同旋转角度的新Pdf,然后删除旧Pdf并将新Pdf重命名为旧Pdf的名称。我已经确定,调用 outFile.renameTo(inFile) 奇怪的是,renameTo()返回true,但文件不等于原始文件,outFile将不再在Windows上的Adobe Reader中打开。我尝试在桌面Pdf修复程序中分析损坏的Pdf文件,结果如下: The end-of-file marker was not found. The ‘startx
outFile.renameTo(inFile)
奇怪的是,renameTo()返回true,但文件不等于原始文件,outFile将不再在Windows上的Adobe Reader中打开。我尝试在桌面Pdf修复程序中分析损坏的Pdf文件,结果如下:
The end-of-file marker was not found.
The ‘startxref’ keyword or the xref position was not found.
The end-of-file marker was not found.
如果我省略了delete()和renameTo()的调用,那么剩下的两个文件都没有损坏。我还尝试过用字节[]复制文件内容,结果相同。我尝试了outFile.renameTo(新文件(infle.toString()),因为infle实际上是具有相同结果的文件的子类。我尝试了具有相同结果的新FileDescriptor().sync()。我尝试了在具有相同结果的每个文件操作之间添加此广播:
PdfRotateService.appContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://")));
我已尝试使用相同的结果休眠线程。我已验证路径是否正确。未引发任何异常,delele()和renameTo()返回true。我还尝试保留对FileOutputStream的引用,并在finally块中手动关闭它
我开始觉得Android操作系统中有一个bug或其他什么(但也许我忽略了一些简单的东西),请帮助!我想要一个与原始文件名相同的旋转Pdf
static boolean rotatePdf(LocalFile inFile, int angle)
{
PdfReader reader = null;
PdfStamper stamper = null;
LocalFile outFile = getGoodFile(inFile, ROTATE_SUFFIX);
boolean worked = true;
try
{
reader = new PdfReader(inFile.toString());
stamper = new PdfStamper(reader, new FileOutputStream(outFile));
int i = FIRST_PAGE;
int l = reader.getNumberOfPages();
for (; i <= l; ++i)
{
int desiredRot = angle;
PdfDictionary pageDict = reader.getPageN(i);
PdfNumber rotation = pageDict.getAsNumber(PdfName.ROTATE);
if (rotation != null)
{
desiredRot += rotation.intValue();
desiredRot %= 360;
}
// else
// worked = false;
pageDict.put(PdfName.ROTATE, new PdfNumber(desiredRot));
}
} catch (IOException e)
{
worked = false;
Log.w("Rotate", "Caught IOException in rotate");
e.printStackTrace();
} catch (DocumentException e)
{
worked = false;
Log.w("Rotate", "Caught DocumentException in rotate");
e.printStackTrace();
} finally
{
boolean z = closeQuietly(stamper);
boolean y = closeQuietly(reader);
if (!(y && z))
worked = false;
}
if (worked)
{
if (!inFile.delete())
worked = false;
if (!outFile.renameTo(inFile))
worked = false;
}
else
{
outFile.delete();
}
return worked;
}
static boolean closeQuietly(Object resource)
{
try
{
if (resource != null)
{
if (resource instanceof PdfReader)
((PdfReader) resource).close();
else if (resource instanceof PdfStamper)
((PdfStamper) resource).close();
else
((Closeable) resource).close();
return true;
}
} catch (Exception ex)
{
Log.w("Exception during Resource.close()", ex);
}
return false;
}
public static LocalFile getGoodFile(LocalFile inFile, String suffix)
{
@SuppressWarnings("unused")
String outString = inFile.getParent() + DIRECTORY_SEPARATOR +
removeExtension(inFile.getName()) + suffix + getExtension(inFile.getName());
LocalFile outFile = new LocalFile(inFile.getParent() + DIRECTORY_SEPARATOR +
removeExtension(inFile.getName()) + suffix + getExtension(inFile.getName()));
int n = 1;
while (outFile.isFile())
{
outFile = new LocalFile(inFile.getParent() + DIRECTORY_SEPARATOR +
removeExtension(inFile.getName()) + suffix + n + getExtension(inFile.getName()));
++n;
}
return outFile;
}
static boolean rotatePdf(LocalFile infle,int angle)
{
PdfReader reader=null;
PdfStamper压模=null;
LocalFile outFile=getGoodFile(填充,旋转后缀);
布尔值=真;
尝试
{
reader=newpdfreader(infle.toString());
stamper=newpdfstamper(读取器,新文件输出流(outFile));
int i=第一页;
int l=reader.getNumberOfPages();
对于(;i,当您执行outFile.renameTo(infle)时
和infle
最终会被破坏。你可以很肯定outfile
已经被破坏。例如,你的FileOutputStream
没有被关闭。renameTo
使用系统级重命名,这是可以保证工作的。但是如果我用delete()和renameTo()注释掉if/else块方法,两者都没有损坏?还应该提到我曾尝试保留对FileOutputStream的引用,并在最终块中手动关闭它…相同的结果,没有异常thrownHum,new FileDescriptor().sync();
是无意义的,out.flush();out.getFD().sync();
是您想要的(out
是一个FileOutputStream
)然后关闭它。接下来可以检查损坏的文件与正确的文件有何不同。例如,使用hexeditor比较有/没有重命名和删除的文件。哦,没有找到文件结尾标记。
是否意味着在重命名之前没有正确关闭输出,并且结尾缺少一些字节。