フィルターのクリア

java.io.File - strange behaviour

15 ビュー (過去 30 日間)
bbb_bbb
bbb_bbb 2018 年 12 月 29 日
コメント済み: John 2019 年 5 月 17 日
filename='F:\__1\2\test files\file.bin';
success=java.io.File(filename).delete;
invoked from the Matlab Command Window - works,
running as a script - gives error:
"Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with any additional indexing or dot references.
MATLAB:staticMethodIndex"
Why is this so?

採用された回答

Yair Altman
Yair Altman 2018 年 12 月 30 日
With most Java methods, you can follow the method invocation parentheses with a dot, thereby chaining methods. For example: myJPanel.getComponent(0).getComponent(1).setVisible(false). However, such method chaining cannot be done with a constructor - it's valid syntax in Java, but Matlab's interpreter is not smart enough. In this case, simply separate the call into two parts:
filename='F:\__1\2\test files\file.bin';
jFile = java.io.File(filename);
success = jFile.delete;
  3 件のコメント
Yair Altman
Yair Altman 2019 年 5 月 17 日
as far as I am aware, the behavior is the same in this regards in both the command window and scripts - in both cases you cannot chain methods to a Java constructor call, only to other methods.
John
John 2019 年 5 月 17 日
Nope, they are different. I just tested:
>> ls('D:\tmp')
Figure1.JPG
Figure2.JPG
>> filename = 'D:\temp\Figure1.JPG';
>> success=java.io.File(filename).delete
success =
logical
0
Put those exact lines into a script? and get:
>> Untitled7
Error: Functions cannot be indexed using {} or . indexing.
Error in Untitled7 (line 2)
success=java.io.File(filename).delete;
Conclusion: The parser in the command line, is different from the parser, that parses scripts... That's a bit of a surprise to me, but I'm not into the details of Matlab like you are.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJava Package Integration についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by