Hello,
I am trying to read an excel file from a network folder. I am using standard Java DataInputStream to read the file and store the value in a byte array. How can I declare a variable of type byte array and specify the size of this byte array?
function getBytesFromFile(inputPath, inputFile){ var file = new java.io.File(inputPath, inputFile); var bytes = new Array; var dis = new java.io.DataInputStream(new java.io.FileInputStream(file)); dis.read(bytes); dis.close(); return bytes; }
The above code does not return any value.
Standard way in java is to declare the variable like this:
byte bytes[] = new byte[(int)file.length()];
But that doesn't seem possible in ARIS script.
Thanks for any help.
Regards,
Allen
Hi Allen,
Hope this helps you
function getBytesFromFile(inputPath, inputFile) { var file = java.io.File(inputPath, inputFile); var fileInputStream = java.io.FileInputStream(file); var byteArrayOutputStream = java.io.ByteArrayOutputStream(); while (fileInputStream.available()) { byteArrayOutputStream.write(fileInputStream.read()); } var bytes = byteArrayOutputStream.toByteArray(); fileInputStream.close(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); return bytes; }
Regards,
Amol Patil
Thank You Amol! This works perfect!
I was able to come up with my own solution below, but yours seems cleaner. I ended up looping and reading one byte at a time. Problem with ARIS script is not being able to define data type and size.
function getBytesFromFile(inputPath, inputFile){ // Define file object to store file info var file = new java.io.File(inputPath, inputFile); // Define array to store byte array info for data var bytes = new Array(); // Define data input stream object that we will read var is = new java.io.DataInputStream(new java.io.FileInputStream(file)); // Loop to read each byte from file until the end of file for (var index = 0; index < file.length(); index++) { bytes[index] = is.readByte(); } return bytes; }
Regards,
Allen
Hello,
After I read the file, I wish to delete it from the network directory. I thought it would be easy enough to do:
file.delete();
But that does not work in ARIS script. I even tried just to rename it to some other filename, but that method doesn't seem to work either.
Is this possible to do from within a report script?
Regards,
Allen