토런트 파일을 다운로드하고 트래커 링크를 수정하는 응용 프로그램을 작성 중입니다. 하지만 수정 된 파일을 저장할 때 내 토렌트 클라이언트에서 열 수 없기 때문에 인코딩에 큰 문제가있을 것입니다.자바에서. 토런트 파일 수정
@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;
}
내가 작품을 다운로드하는 것은 내가 토런트 클라이언트 (또한 내가 KomodoEdit에서 다운로드 한 파일을 수정할 때)에없는 수정 된 파일을 열 수 있기 때문에 확신 :
나는 다운로드하고 파일을 수정하는 코드를 썼다. 하지만 파일을 수정하고 대체 된 문자열을 저장하면 내 클라이언트가 파일을 열 수 없으며 잘못된 데이터에 대해 불만을 토로합니다.누구든지 아이디어가 있으십니까? 어쩌면 UTF8이 잘못되었거나 코드의 일부를 변경해야합니까?
파일이 ASCII 또는 변형으로 생각됩니다. 따라서 UTF-8의 직접 출력은 적합하지 않습니다. –
다운로드 한 파일의 인코딩을 확인할 방법이 있습니까? 위키에서는 UTF-8에 대한 정보가 토런트 파일에 있습니다. –
내 컴퓨터의 토런트 파일이 ANSI ...로 표시됩니다. 소스 - Windows 메모장에서 열기로 저장하십시오. 아래쪽에는 드롭 다운 상자에 감지 된 인코딩이 표시됩니다. –