Why do I receive an error when I call the "read" method of the FileInputStream in MATLAB?

4 次查看(过去 30 天)
When I call the "read" method of the FileInputStream in MATLAB while running the following commands:
myFile = java.io.FileInputStream('dummyfile.txt');
b = javaArray ('java.lang.Byte',5)
myFile.read(b)
I receive the error:
??? No method read with matching signature found for class java.io.FileInputStream
Based on the java doc for the java.io.FileInputStream, the read method can take a byte[] input:
int read(byte[] b)

采纳的回答

MathWorks Support Team
This error is due to the lack of support for primitive Java data types in MATLAB. The "read" method of the FileInputStream class in the java.io package actually expects an array of primitive bytes (byte[]) and not an array of the Byte wrapper object (java.lang.Byte[]).
As a work around, you can use the overloaded read method that does not take any input arguments within a loop:
myFile = java.io.FileInputStream('dummyfile.txt');
b = zeros(1,myFile.available);
for n = 1:length(b)
b(n) = myFile.read;
end
In order to use a function such as this in this manner, you would need to declare the function as follows:
int read(java.lang.Byte[] b)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Java from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by