Forums > Javascript

manipulate text files in js

December 9, 2008 | 5:58 am

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?


December 9, 2008 | 6:10 am

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


December 9, 2008 | 5:50 pm

great, thanks for the tip.


Viewing 3 posts - 1 through 3 (of 3 total)