1
System.out.print("1");
System.out.println(ts.getComponent());
System.out.println(ts.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
String dir = ts.getTransferable().getTransferData(DataFlavor.javaFileListFlavor).toString();

File file = new File(dir);
System.out.println(file.getName());
System.out.println(file.getPath());
System.out.println(file.getTotalSpace());

long num = file.length();
System.out.println(num);

Output

1javax.swing.JTable[,0,0,556x64,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@1baabbd6,flags=251658568,maximumSize=,minimumSize=,preferredSize=,autoCreateColumnsFromModel=true,autoResizeMode=AUTO_RESIZE_SUBSEQUENT_COLUMNS,cellSelectionEnabled=false,editingColumn=-1,editingRow=-1,gridColor=javax.swing.plaf.ColorUIResource[r=35,g=35,b=36],preferredViewportSize=java.awt.Dimension[width=450,height=400],rowHeight=16,rowMargin=0,rowSelectionAllowed=true,selectionBackground=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,selectionForeground=DerivedColor(color=255,255,255 parent=nimbusLightBackground offsets=0.0,0.0,0.0,0 pColor=255,255,255,showHorizontalLines=false,showVerticalLines=false]
[C:\Users\Ye\Desktop\Spotify.lnk]
Spotify.lnk]
[C:\Users\Ye\Desktop\Spotify.lnk]
0

I dont get how file size is 0 despite testing it with few files

2
  • Have you tried checking different file types? i.e., not Windows shortcuts. Commented Nov 24, 2018 at 4:52
  • yea i tried with some other exe and files Commented Nov 24, 2018 at 5:11

2 Answers 2

3

Chances are that you're either checking the length of a directory OR that the specified File doesn't exist.

From the documentation of the File.length, it returns:

The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.

Sign up to request clarification or add additional context in comments.

Comments

2

The problem seems to be with the path String you are providing to the File constructor. The value [C:\Users\Ye\Desktop\Spotify.lnk] is not a valid pathname for the File constructor for the file your are trying to access. This is visible in your print statement's output for file.getName() which is Spotify.lnk] and not Spotify.lnk. The file object you are trying to get the size of is a new file with the name Spotify.lnk**]** that has the size 0.
I don't know whether you have any other way to get the file name other than getTransferData(DataFlavor.javaFileListFlavor).toString() but you can try to sanitise the file name using the following code :

    String dirTemp = ts.getTransferable().getTransferData(DataFlavor.javaFileListFlavor).toString();
    dir = dirTemp.substring(1, dirTemp.length() - 1);
    // Then create the file using this dir String.
    File file = new File(dir);

This would work for the current output of getTransferData()#toString() that you are getting, but might not work if, say, someone changes the toString() implementation.

Edit :

As correctly pointed by @MichaelA.Schaffrath, the object returned by ts.getTransferable().getTransferData(DataFlavor.javaFileListFlavor) is actually a List<File>. So, there isn't even a need to construct a new File object again. Just cast the returned value as a List and use the first value for the File object.

2 Comments

No need for string parsing. He is using DataFlavor.javaFileListFlavor, which means getTransferData will return a java.util.List. He just needs to cast his result from getTransferData to List and get the first element.
@MichaelA.Schaffrath , Thanks I wasn't aware of that... Will update my answer with the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.