这里我有一个名为sourceTx的文本区域,我在其中拖放文件,然后用BufferedReader读取该文件的内容。从下面的代码中我可以看到,我设置了文件,我正在使用absolutepath读取内容。
所以,当我拖动一个.txt文件时它会工作,它会读取内容并将其放在文本区域中,但是当我也拖放一些文件夹时,例如它也会读取一些内容并将其放在文本区域中。
所以我想把这个拖放设置为只读.txt文件?我怎么能得到它?
以下是该方法的代码:
public void dragDrop(){
sourceTx.setOnDragOver(new EventHandler <DragEvent>() {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if(db.hasFiles()){
event.acceptTransferModes(TransferMode.ANY);
for(File file:db.getFiles()){
String absolutePath = file.getAbsolutePath();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath)));
String line = null;
String text = "";
String nl = System.getProperty("line.separator", "\n");
while((line = br.readLine()) != null)
text += line + nl;
sourceTx.setText( text.trim() );
} catch (Exception e) {
MessageBox.show(MessageBoxType.ERROR, I18n.localize("File Error"), I18n.localize("Error while reading content from selected file"));
} finally{
if(br != null)
try {
br.close();
} catch (Exception e) {}
}
}
}else{
event.setDropCompleted(false);
}
event.consume();
}
});
}
答案 0 :(得分:1)
您好试着用递归
读取您的文件...
for (File file : db.getFiles()) {
sourceTx.setText(handleFile(file));
}
...
private String handleFile(File file) {
String ret = "";
if (file.isDirectory()) {
for (File f : file.listFiles()) {
ret.concat(handleFile(f));
}
} else {
/*this is your filereader*/
String absolutePath = file.getAbsolutePath();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath)));
String line = null;
String text = "";
String nl = System.getProperty("line.separator", "\n");
while ((line = br.readLine()) != null)
text += line + nl;
ret.concat(text.trim());
} catch (Exception e) {
MessageBox.show(MessageBoxType.ERROR, I18n.localize("File Error"), I18n.localize("Error while reading content from selected file"));
} finally {
if (br != null)
try {
br.close();
} catch (Exception e) {
}
}
}
return ret;
}
答案 1 :(得分:0)
我在网上找到了一个使用拖放的好资源。 以下是您可能想要调查的一些类/事项:
java.awt.dnd.*
我几乎从在线教程中复制了这个,但这里有一些代码(不是我的,但经过测试并且有效):
public class MyFrame extends JFrame
{
// insert other code here
JLabel myLabel = new JLabel("My stuff here");
// Create the drag and drop listener
MyDragDropListener myDragDropListener = new MyDragDropListener(this);
// Connect the label with a drag and drop listener
new DropTarget(myLabel, myDragDropListener);
// then just add the label
// also have a method something like "get" which will be used so that the listener can send
// the list of files dropped here, and you can process it here
}
现在是MyDragDropListener。
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.File;
import java.util.List;
public class MyDragDropListener implements DropTargetListener
{
MyFrame frame; // initialize in a constructor that takes in the frame
@Override
public void dragEnter(DropTargetDragEvent event) {
}
@Override
public void dragExit(DropTargetEvent event) {
}
@Override
public void dragOver(DropTargetDragEvent event) {
}
@Override
public void dropActionChanged(DropTargetDragEvent event) {
}
@Override
public void drop(DropTargetDropEvent event)
{
// This is the main chunk of the drag and drop.
event.acceptDrop(DnDConstants.ACTION_COPY);
Transferable transferable = event.getTransferable();
DataFlavor[] flavors = transferable.getTransferDataFlavors();
for(DataFlavor flavor : flavors)
{
if(flavor.isFlavorJavaFileListType())
{
List myFiles = (List) transferable.getTransferData(flavor);
frame.get(myFiles);
}
}
}
}
您可以使用它来创建一个JFrame来拖放文件,然后检查文件名是否包含&#34; .txt&#34; (我不确定Java是否有确定文件类型的方法,即使它没有扩展名。)如果它包含&#34; .txt&#34;然后你可以在TextArea中打开它。
如果有人可以帮我找到原始教程/网站,我会非常感激。也很抱歉答案的格式。