0

Good morning!

I have been working on a client side browser based app using JavaScript that (all of a sudden) needs the capability to save and load files locally.

The saved files are plain text (.txt) files.

I have managed to get JavaScript to read existing text files. However, I am unable to find reliable information on how to create and edit the contents of these files.

Based on what I see online, I am under the impression that you can't do this with JavaScript alone.

I found out from another source that the best way to do this is outsource the file writing/editing to a Java file and let Java do the work.

I found a code snippet and tweaked it around a bit, but it is not working and I seem to be at a loss:

JAVASCRIPT

<!Doctype html>
<html>

<OBJECT ID="Test" height=0 width=0
CLASSID="CLSID:18F79884-E141-49E4-AB97-99FF47F71C9E" CODEBASE="JavaApplication2/src/TestJava.java" VIEWASTEXT>
</OBJECT>

<script language="Javascript">
var Installed;
Installed = false;
try
{ 
  if (Test==null)
    Installed = false;
  else
    Installed = true;
}
catch (e)
{
  Installed = false;
}

alert ("Installed :- " + Installed);
TestStr = Test.SendStr("Basil");
alert (TestStr);

</script>

</html>

JAVA

    import javax.swing.*;

    public class TestJava {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
        }

        public String SendStr(String lStr)
        {
            return lStr + "!!!";
        }
    }

If someone could point me in the right direction or even just explain why this isn't working, I would appreciate it.

John
  • 2,820
  • 3
  • 30
  • 50
SandyBites
  • 173
  • 2
  • 19
  • Javascript cant do File handling. It can only be done by any server side language. How can you open & read text file with javascript ? – Harshit Aug 06 '15 at 04:03
  • Calling a java function from JavaScript http://stackoverflow.com/questions/6649125/calling-java-methods-in-javascript-code – rest_day Aug 06 '15 at 04:05
  • Absolutely, I guess what im asking here is how do I call a Java method from a Java file that is in the same directory as my JavaScript file. Thanks. – SandyBites Aug 06 '15 at 04:05
  • Javascript cannot directly contact with a server side programming language like Java. You could use Ajax to get data through Java. – Harshit Aug 06 '15 at 04:13
  • One technique for this is to run a web-service locally. Then make ajax calls. – matt Aug 06 '15 at 05:45

1 Answers1

0

I believe the sandbox issue prevents all browsers from performing any and all local file writing, without an enormous amount of working around the access restrictions. It is easier to write files remotely on the server than to write them locally to the client. This is true across all browsers.

So while it may be possible to perform the load function, you cannot perform the 'save' function on the local machine.