Hi guys,

I'm writing an application to download torrent file and modify it's tracker link (just replace passkey). But probably I have big problem with encoding because when I save modified file it can't be open in my torrent client.

I wrote a code to download and modify file:
Java Code:
 
            @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;
    }
I'm sure that downloading works because I can open not modified file in torrent client (also when I modify downloaded file in KomodoEdit). But when I modify file and save replaced String my client can't open it and complains about invalid data.

Anyone have any idea? Maybe UTF8 is wrong or I have to change some part of my code?

BR