Copy link to clipboard
Copied
Hi,
I'm searching a way to read (and subsequently edit) the contents of a text file on my disk. I thought it works with the File.read() function but when I run the following I get nothing from the file. How does it work? How do I get the string with the file content? I tried different functions like readln() etc but with no luck
var testtextfile = File("/c/users/bernhard-büro/desktop/text.txt"); | |
testtextfile.open("r", undefined, undefined); //also tried with .open("r"); and .open('r'); | |
var fileContentsString = testtextfile.read(); |
alert("Full path: "+testtextfile.fsName+"\r\n"+ | |
"File content:"+fileContentsString+"\r\n"+ | |
"fileContentsString length:"+fileContentsString.length); | |
testtextfile.close(); |
this is the result: (and what is the proper way to format code in this forum?)
Thank you very much!
Cheers
Often the cause is encoding related.
Try
var testtextfile = File("/c/users/bernhard-büro/desktop/text.txt");
testtextfile.encoding = 'UTF8'; // set to 'UTF8' or 'UTF-8'
testtextfile.open("r");
var fileContentsString = testtextfile.read();
testtextfile.close();
alert("Full path: " + testtextfile.fsName + "\r\n" +
"File content:" + fileContentsString + "\r\n" +
"fileContentsString length:" + fileContentsString.length);
To format the code you go to advanced editor
and then
HTH
Trevor
Copy link to clipboard
Copied
Please try this..
var testtextfile = new File("/c/users/bernhard-büro/desktop/text.txt");
testtextfile.open('r');
fileContentsString = testtextfile.readln();
alert("File content:"+fileContentsString);
Copy link to clipboard
Copied
Often the cause is encoding related.
Try
var testtextfile = File("/c/users/bernhard-büro/desktop/text.txt");
testtextfile.encoding = 'UTF8'; // set to 'UTF8' or 'UTF-8'
testtextfile.open("r");
var fileContentsString = testtextfile.read();
testtextfile.close();
alert("Full path: " + testtextfile.fsName + "\r\n" +
"File content:" + fileContentsString + "\r\n" +
"fileContentsString length:" + fileContentsString.length);
To format the code you go to advanced editor
and then
HTH
Trevor
Copy link to clipboard
Copied
Hi Trevor, thanks a lot for that. I still have to find out what exactly is going on in detail about the character encoding on my system (changing it doesn't seem to be necessary in most cases) but now I can parse the text out of my file, great! Kind regards, Stefan
Copy link to clipboard
Copied
Hi Stefan
The need for the encoding is dependent on the contents of the file.
If the files contains just asci then you shouldn't need to set it. ü and most of the diacritics are asci extended and also shouldn't need setting but if the text contains Unicode characters you are generally going to need to set it.
Trevor
Copy link to clipboard
Copied
So it's safer always to set the encoding to UTF-8.
Copy link to clipboard
Copied
Apart from the encoding, another problem might be the u+umlaut in your filename. If Trevor's suggestion doesn't help, rename the file so that its name doesn't have any accented characters, then try again.
Copy link to clipboard
Copied
Hi Peter,
about the u umlaut problem:
I've seen this on Mac OS X where a ü in a file name can be composed out of:
u\u0308
Could that happen on Windows as well?
Regards,
Uwe
Copy link to clipboard
Copied
> Could that happen on Windows as well?
No, it doesn't. One of the few areas where Windows behaves better than the Mac OSs
Copy link to clipboard
Copied
Hi
I second that. I discovered that recently while resolving image files instance on Mac Os. I got a textual reference with a diacritic such as "marché.jpg" and the file couldn't be found to my surprise. I coudl see the file but using File ("marché.jpg").exists would return false.
Fact is Mac os was using "marche[diacritic].jpg"
Painful indeed.
Copy link to clipboard
Copied
Unless you need to use UTF-16 like I did yesterday 😉
Copy link to clipboard
Copied
Fair point!
Copy link to clipboard
Copied
If that was indeed the case: A very basic check to see if the file is opened succesfully would have shown that.
Copy link to clipboard
Copied
Hi Peter, thanks for your answer. Taking the umlaut out of my filepath did not help. However, I still think it might be part of the problem because why else did changing the encoding setting help when usually it works well without? But this is a wild guess, I will still look into it and post it here if I can find out something relevant. regards
Find more inspiration, events, and resources on the new Adobe Community
Explore Now