我使用此测试将 txt 转换为 pdf :
package convert.pdf;
//getResourceAsStream(String name) : Returns an input stream for reading the specified resource.
//toByteArray : Get the contents of an InputStream as a byte[].
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import convert.pdf.txt.TextConversion;
public class TestConversion {
private static byte[] readFilesInBytes(String file) throws IOException {
return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
}
private static void writeFilesInBytes(byte[] file, String name) throws IOException {
IOUtils.write(file, new FileOutputStream(name));
}
//just change the extensions and test conversions
public static void main(String args[]) throws IOException {
ConversionToPDF algorithm = new TextConversion();
byte[] file = readFilesInBytes("/convert/pdf/text.txt");
byte[] pdf = algorithm.convertDocument(file);
writeFilesInBytes(pdf, "text.pdf");
}
}
问题:
线程“主”java.lang.NullPointerException 中的异常 在 org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1025) 在 org.apache.commons.io.IOUtils.copy(IOUtils.java:999) 在 org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:218) 在 convert.pdf.TestConversion.readFilesInBytes(TestConversion.java:17) 在 convert.pdf.TestConversion.main(TestConversion.java:28)
我使用调试器,问题似乎出在此处:
private static byte[] readFilesInBytes(String file) throws IOException {
return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
}
我的问题是什么?