gpt4 book ai didi

Java : reading all files

转载 作者:行者123 更新时间:2023-11-30 07:47:02 26 4
gpt4 key购买 nike

我正在尝试将文件读入通过 netbeans 运行的 java 应用程序。我已成功预览文件,但只能预览 .txt 文件。如何更改我的代码以读取任何文件? (例如.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);
}

最佳答案

你应该使用像 apache tika 这样的东西

http://tika.apache.org/

这将允许您读取几乎任何类型的文件

还可以查看 java.io.File.list() 以找出某个位置的文件类型

关于Java : reading all files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33782907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com