有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文件中的文件已损坏。重命名()

这是这个问题的后续:here,涉及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 ‘startxref’ keyword or the xref position was not found.
The end-of-file marker was not found.

如果我省略了delete()和renameTo()的调用,那么剩下的两个文件都没有损坏。我还尝试过用字节[]复制文件内容,结果相同。我已经试过了。重命名为(新文件(infle.toString()),因为infle实际上是File的一个子类,具有相同的结果。我尝试了新的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;
}

共 (0) 个答案