Reputation:
I am attempting to read in files into a java application running via netbeans. I have been successful in previewing the files, but I can only preview .txt files. How can I alter my code to read in any file(s)? (eg. .doc, .docx, .pdf, .jpg, .png).
JFileChooser share = new JFileChooser();
share.showOpenDialog(null);
File f = share.getSelectedFile();
String fileName = f.getAbsolutePath();
try {
FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
jTextArea1.read(br, null);
br.close();
jTextArea1.requestFocus();
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "File not found", "Error", JOptionPane.ERROR_MESSAGE);
}
Upvotes: 1
Views: 61
Reputation: 8008
you should use something like apache tika
this will allow you to read almost any kind of file
also have a look at java.io.File.list() to find out the types of files you have at a location
Upvotes: 1
Reputation: 24802
You can read them alright ; it's just that they are huge blobs of binary data you can't make any sense of without the appropriate tools. Open one of them with a notepad and you'll get what I'm saying.
Their associated software (Word, Reader, etc...) usually do the decoding, but you may find java libraries that can do as much.
Upvotes: 0