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

4 Ansichten (letzte 30 Tage)
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)

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 17 Mai 2010
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)

Weitere Antworten (0)

Kategorien

Mehr zu Call Java from MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by