Hi all,
Unfortunately, Javascript isn't my first (programming) language and I'm struggling to get to grips with creating a simple plain text file from a script. I've spent hours staring at the JS Object Model Reference (http://synchro.net/docs/jsobjs.html) and I just cannot work it out.
This is what I've got so far:
var f = new File('test.txt');
file.open('w');
file.writeln('Line 1');
file.writeln('Line 2');
file.close();
I've tried the file.open(file_exists(file.name) ? 'r+':'w+'); line
mentioned in the documentation and I just cannot get this to work.
Any code suggestions would be welcome!
var f = new File('test.txt');
file.open('w');
file.writeln('Line 1');
file.writeln('Line 2');
file.close();
"file" is not defined in that example. I think you meant to write f.open(), f.writeln(), and f.close().
There are many examples in the exec/*.js files which open/modify files. You could look at those for some tips or inspiration as well.
this actually looks fine to me... you can try checking whether or not the file.open() was successful ala var didItOpen = file.open('w');
As DM said, I needed to use the variable that I had declared as a file, not just the word 'file'. All working now though, thanks for the reply!
"file" is not defined in that example. I think you meant to write f.open(), f.writeln(), and f.close().
Ah, that makes a lot of sense! See, Java isn't my best programming
language!
var f = new File('test.txt');
file.open('w');
file.writeln('Line 1');
file.writeln('Line 2');
file.close();
I've tried the file.open(file_exists(file.name) ? 'r+':'w+'); line
mentioned in the documentation and I just cannot get this to work.
Since you're not specifying a path, I think it goes in the ctrl directory.
Sysop: | Ree |
---|---|
Location: | Toronto, ON |
Users: | 2 |
Nodes: | 10 (0 / 10) |
Uptime: | 109:12:50 |
Calls: | 374 |
Calls today: | 1 |
Files: | 2 |
Messages: | 38,878 |