manipulate text files in js

Dec 9, 2008 at 5:58am

manipulate text files in js

I have settings that I’d like to write to a text file in JS, but the number of lines is variable.
It seems there’s no way to delete lines or clear a file that exists in Javascript in Max, is there? So if I write 10 lines to a file, and then, later, need to change that file and there are only 5 lines, well, it seems like I’m out of luck, no?
Is this possible in Java, maybe?

#41255
Dec 9, 2008 at 6:10am

On Dec 8, 2008, at 9:58 PM, pnyboer wrote:

> I have settings that I’d like to write to a text file in JS, but the
> number of lines is variable.
> It seems there’s no way to delete lines or clear a file that exists
> in Javascript in Max, is there? So if I write 10 lines to a file,
> and then, later, need to change that file and there are only 5
> lines, well, it seems like I’m out of luck, no?

This is accomplished in JS the same way it is accomplished in C or
Java or whatever. For existing files, you need to set the EOF of the
file explicitly, otherwise you can write to some portions of the
already existing file without changing the EOF.

file.open(foo);
file.writeline(“one”);
file.writeline(“two”);
file.writeline(“three”);
file.eof = file.position;
file.close();

-Joshua

#146708
Dec 9, 2008 at 5:50pm

great, thanks for the tip.

#146709

You must be logged in to reply to this topic.