Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: FileReader help

  1. #1
    emp
    emp is offline
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FileReader help

    I'm trying the following without a lot of luck. Its being called as a ECMAScript function from an application and I keep getting an error that readFile isnt known.

    I'm not sure how to actually call the FileReader to read the file in, does anyone have any tidbits based on the code below?

    importPackage(Packages.java.io);


    function userLookup(strFileName,username){

    var nNumLines = 0; //number of lines in file
    var stringToConfirm = username; //will hold text from file
    var document = new java.io.FileReader(strFileName);

    document.FileReader.readFile(strFileName);
    nNumLines = document.FileReader.getNumLines();

    for (var i=0; i<nNumLines; i++){

    stringToConfirm += document.FileReader.getLine(i) + "\n";

    }

    alert(stringToConfirm);

    }

    function getLines(strFileName){

    var stringDataFromFile = ""; //will hold string data in file

    document.FileReader.readFile(strFileName);
    stringDataFromFile += document.FileReader.getLines();
    alert(stringDataFromFile);


    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...that code is a *MIX* of JavaScript and Java.

    There's no such thing as java.io.FileReader or Packages.java.io in standard JavaScript/ECMAScript.

    But I *THINK* there is a HUGE mistake there: You are creating a variable named document and that *HIDES* the normal global object of that name. Normally, when accessing Java from JavaScript, you use something like
    Code:
        document.AppletName.PublicMethodName( )
    BUT that *must* be the standard global document object, not one you create.

    It's been 8+ years since I used Java applets and "talked" to them from JavaScript (Java applets were to be the be-all, end-all that would save the Web...sure didn't turn out that way), so I don't trust my memory, too much. But the part about needing to use the global document object I'm pretty clear on.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •