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

10 views (last 30 days)
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)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 May 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)

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!