Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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)

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 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)

Plus de réponses (0)

Catégories

En savoir plus sur Call Java from MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by