The code below will read a text file successfully and is probably good for smaller files. I, however, need to read a 1.5 mB file which takes 8-9 minutes because of the String.fromCharCode(array[i]) statement. Does anyone know of a faster read method with example?
Thanks in advance.
function readTextFile(){
//Browse for and select master matrix data text file
var selFiles = Dialogs.getFilePath("","*.txt","C:\Users","Select File",0);
Context.writeStatus("Reading file");
var binData=selFiles[0].getData();
Context.writeStatus("UniCode to Text conversion");
var sString=toText(binData);
return sString;
}
function toText(array){
var result="";
for(var i=0; i<array.length; i++){
Context.writeStatus("Converting UniCode to Text",(i*100)/array.length);
result+=String.fromCharCode(array[i]);
}
return result;
}
Hello Dave,
To read a text file, I use java function :
//Read a file located in the script category var fl = Context.getFile("area.txt", Constants.LOCATION_SCRIPT); var fstream = new java.io.ByteArrayInputStream(fl); var inputstream = java.io.DataInputStream(fstream); var bufferReader = java.io.BufferedReader(new java.io.InputStreamReader(inputstream)); var strLine = ""; var totalLines = ""; while((strLine = bufferReader.readLine()) != null){ totalLines += strLine; }
I hope it can help.
Best Rgards
Romain Tricarico
Hello, the argument must be a buffer array of byte.
Do not pass in parameter the selected file you get with the :
Dialogs.getFilePath("","*.txt","C:\Users","Select File",0);
I think you should pass a byte Array :
var byteArray = selFiles[0].getData();
var fstream = java.io.ByteArrayInputStream(byteArray);
Let me know if it works.
Best Regards.
Thank you for your help. I was able to find an example in the scripts supplied with the distribution disk that helped me. The code below is working for me and is very similar to the code you have.
function readTextFile(){
var selFiles = Dialogs.getFilePath("","*.txt","C:\Users","Select the Master Matrix Data File",0);
var inStream = new java.io.ByteArrayInputStream(selFiles[0].getData());
var fileReader = new java.io.InputStreamReader(inStream);
var bufReader = new java.io.BufferedReader(fileReader);
var strLine="";
var totalLines="";
while((strLine=bufReader.readLine())!=null){
totalLines+=strLine;
}
return totalLines;
}