首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中修改.torrent文件

在Java中修改.torrent文件
EN

Stack Overflow用户
提问于 2013-10-12 04:36:09
回答 1查看 482关注 0票数 2

我正在写一个应用程序来下载torrent文件并修改它的跟踪器链接(只需替换密钥)。但可能我在编码方面有很大的问题,因为当我保存修改过的文件时,它不能在我的torrent客户端中打开。

我写了一个代码来下载和修改文件:

代码语言:javascript
复制
@Override
public void exeucte(String link) throws IOException {
    FileOutputStream fos = null;
    try {
        URL website = new URL(link);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        String fileName = getFileName(link);
        fos = new FileOutputStream(fileName);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        replacePassKey(fileName);
    } finally {
        if (fos != null)
            fos.close();
    }

}

private void replacePassKey(String fileName) throws IOException {
    File originalFile = new File(fileName);
    String lines = readLines(originalFile);
    String replacedLines = lines.replaceAll("(.*passkey=)(.*)(:comment27.*)", "$1" + PASS_KEY + "$3");
    originalFile.delete();
    writeReplacedLines(replacedLines, originalFile);
}

private void writeReplacedLines(String replacedLines, File file) throws IOException {
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(file));
        bw.write(replacedLines);
    } finally {
        if (bw != null)
            bw.close();
    }

}

private String readLines(File originalFile) throws IOException {
    RandomAccessFile raf = null;
    String lines = null;
    try {
        raf = new RandomAccessFile(originalFile, "r");
        byte[] bytes = new byte[(int) raf.length()];
        raf.read(bytes);
        lines = new String(bytes, Charset.forName("UTF8"));
    } finally {
        if (raf != null)
            raf.close();
    }
    return lines;
}

我确信下载可以工作,因为我可以在torrent客户端中打开未修改的文件(当我在KomodoEdit中修改下载的文件时也是如此)。但是当我修改文件并保存替换的字符串时,我的客户无法打开它,并抱怨无效数据。

有谁知道吗?也许UTF8是错的,或者我必须更改代码的某一部分?

EN

回答 1

Stack Overflow用户

发布于 2013-12-14 17:44:16

尝试使用各种设置打开它,然后检查the wiki page

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19326810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档