Java 为什么在尝试读取文件时会出现NullPointerException?
我使用此测试将txt转换为pdf:Java 为什么在尝试读取文件时会出现NullPointerException?,java,file-io,itext,Java,File Io,Itext,我使用此测试将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.
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");
}
}
问题:
Exception in thread "main" java.lang.NullPointerException
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1025)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:999)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:218)
at convert.pdf.TestConversion.readFilesInBytes(TestConversion.java:17)
at convert.pdf.TestConversion.main(TestConversion.java:28)
我的问题是什么?在将文件传递到
readFilesInBytes()之前,是否检查文件是否存在?请注意,如果找不到文件,则Class.getResourceAsStream()
将返回null
。您可能想做:
private static byte[] readFilesInBytes(String file) throws IOException {
File testFile = new File(file);
if (!testFile.exists()) {
throw new FileNotFoundException("File " + file + " does not exist");
}
return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
}
或者更好:
private static byte[] readFilesInBytes(String file) throws IOException {
InputStream stream = TestConversion.class.getResourceAsStream(file);
if (stream == null) {
throw new FileNotFoundException("readFilesInBytes: File " + file
+ " does not exist");
}
return IOUtils.toByteArray(stream);
}
听起来资源可能不存在该名称
您是否知道Class.getResourceAsStream()
查找与该类的包相关的资源,而ClassLoader.getResourceAsStream()
没有?您可以在Class.getResourceAsStream()
中使用前导正斜杠来模拟这一点,所以
Foo.class.getResourceAsStream("/bar.png")
大致相当于
Foo.class.getClassLoader().getResourceAsStream("bar.png")
这实际上是您试图加载的文件(即普通文件系统上的特定文件)吗?如果是这样,使用FileInputStream
将是一个更好的选择。如果是绑定在jar文件中或以其他方式绑定在类路径中的资源,请使用Class.getResourceAsStream()
;如果是任意文件,可以在文件系统中的任何位置使用FileInputStream
编辑:另一件需要注意的事情,这在以前给我带来了问题-如果这在您的开发框(碰巧是Windows)上起作用,并且现在在生产服务器(碰巧是Unix)上出现故障,请检查文件名的大小写。不同的文件系统以不同的方式处理大小写敏感度这一事实可能是一个难题…此类读取类路径中的TXT文件,并使用TextConversion将其转换为PDF,然后将PDF保存在文件系统中
这里是文本转换代码:
package convert.pdf.txt;
//Conversion to PDF from text using iText.
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import convert.pdf.ConversionToPDF;
import convert.pdf.ConvertDocumentException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class TextConversion implements ConversionToPDF {
public byte[] convertDocument(byte[] documents) throws ConvertDocumentException {
try {
return this.convertInternal(documents);
} catch (DocumentException e) {
throw new ConvertDocumentException(e);
} catch (IOException e) {
throw new ConvertDocumentException(e);
}
}
private byte[] convertInternal(byte[] documents) throws DocumentException, IOException {
Document document = new Document();
ByteArrayOutputStream pdfResultBytes = new ByteArrayOutputStream();
PdfWriter.getInstance(document, pdfResultBytes);
document.open();
BufferedReader reader = new BufferedReader( new InputStreamReader( new ByteArrayInputStream(documents) ) );
String line = "";
while ((line = reader.readLine()) != null) {
if ("".equals(line.trim())) {
line = "\n"; //white line
}
Font fonteDefault = new Font(Font.COURIER, 10);
Paragraph paragraph = new Paragraph(line, fonteDefault);
document.add(paragraph);
}
reader.close();
document.close();
return pdfResultBytes.toByteArray();
}
}
下面是转换为PDF的代码:
package convert.pdf;
// Interface implemented by the conversion algorithms.
public interface ConversionToPDF {
public byte[] convertDocument(byte[] documentToConvert) throws ConvertDocumentException;
}
我认为问题来自我的文件系统(windows上的devbox和服务器是Unix)。
我将尝试修改我的类路径。此问题可能是由于调用test.txt
上的方法引起的,该方法可以是文件夹快捷方式。换句话说,您正在对一个不存在的文件调用一个方法,从而导致NullPointerException
package convert.pdf;
// Interface implemented by the conversion algorithms.
public interface ConversionToPDF {
public byte[] convertDocument(byte[] documentToConvert) throws ConvertDocumentException;
}