我想阅读pdf文件,将其写入txt,然后阅读此txt并重新创建pdf文件。
PDF - 读取字节 - >我 - 写字符 - > TXT - 读取字符 - > ME - 写字节 - >的 PDF
我的目标是重新创建初始PDF。但在这个过程中,最终的PDF被破坏了。我无法打开它。
这是出于学习目的,因此无需工具重新编写。我正在努力做甚么可能吗?
谢谢你,我的代码如下。
public class ByteToChar {
private static final int BUFFER_LENGTH = 8;
private static final String BYTE_INPUT_FILE = "mypdf.pdf";
private static final String CHAR_OUTPUT_FILE = "bytesOut.txt";
private static final String BYTE_OUTPUT_FILE = "bytesPdf.pdf";
public static void main(String[] args) throws IOException {
// Reads pdf's bytes, and writes txt's chars
byteToChar();
// Reads previous txt's chars and writes pdf's bytes
charToByte();
}
public static void byteToChar() throws IOException {
byte[] buffer = new byte[BUFFER_LENGTH];
try (InputStream in = new BufferedInputStream(new FileInputStream(BYTE_INPUT_FILE));
FileWriter fileWriter = new FileWriter(CHAR_OUTPUT_FILE)) {
while (in.read(buffer) != -1) {
String decoded = new String(buffer, "UTF-8");
fileWriter.write(decoded.toCharArray());
}
}
}
public static void charToByte() throws IOException {
char[] buffer = new char[BUFFER_LENGTH];
try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(CHAR_OUTPUT_FILE), "UTF-8"));
OutputStream out = new FileOutputStream(BYTE_OUTPUT_FILE)) {
String line;
while ((line = in.readLine()) != null) {
out.write(line.getBytes());
}
}
}
}