返回 0 的 Java 文件对象大小

标签 java file

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);

输出

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

尽管用几个文件进行了测试,但我还是不知道文件大小是 0

最佳答案

有可能您正在检查目录的长度或者指定的文件不存在。

来自 File.length 的文档,它返回:

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.

关于返回 0 的 Java 文件对象大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53455220/

相关文章:

java - 如何在 java 中迭代 bson 文档

java - 执行 JUnit 测试用例时出现堆栈溢出错误

PHP scandir 递归

JavaScript - 如何将音频文件加载到 readAsArrayBuffer 中?

java - 当我们指定路径时,在 ant 文件中使用 **/*

java - 如何在确定的表格单元格中更改背景颜色

c++ - 测试文件是否具有所需权限的好方法是什么?

php - 如何在 php 事务中执行文件系统操作?

c++ - 在 C++ 中覆盖文本文件

java - 使用 Spring boot 更新 MongoDB 对象