Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trying to read a text file

I have a text file with a list in the format of:

name 0 0 

 

I am trying to read it into a searchable format.  So far, I can't even get the file to be found.  I tried

let info = readFileSync("MyData.txt", "ascii");

let info = readFileSync("../common/MyData.txt", "ascii");

 

The studio tells me it cannot find the file.  Any thoughts on where it might be?  Also, I am hoping to read/search the info.  If this won't work...let me know now.

 

Thanks.

 

Thanks.

 

 

 

 

Best Answer
0 Votes
6 REPLIES 6

It probably depends how you created the file. If it was transferred from the companion, you could try looking in /private/data/

Peter McLennan
Gondwana Software
Best Answer
0 Votes

I created a file under my common folder (where my utils.js is) and copied the text info into it. I will try the private/data tomorrow. 

 

Thank you for the suggestion. 

Best Answer
0 Votes

FlyFrosty, I don't think it works like that. I don't think that text files that you create in Fitbit Studio get uploaded to your device. Did you find this mechanism in the docs? If not, you can't rely on it.

 

Maybe you should write the file from your app when it is first run or something.

 

Best Answer
0 Votes

I am hoping to read the text file with my app.  I have found a text list with 400+ lines of data in it (it is the city/time zone list).  I would love to allow the user to search through them and choose their desired time zone based on that city.  If I have to type them in, they may get 10 or 15 to choose from.

Best Answer
0 Votes

I read a binary file in my resources folder like this:-

 var stats = fs.statSync("/mnt/assets/resources/level_data.bin");
allLevels = new Uint8Array(stats.size);

let file = fs.openSync("/mnt/assets/resources/level_data.bin", "r");
fs.readSync(file, allLevels, 0, stats.size, 0);
fs.closeSync(file);

You should be able to read your text file in the same way.

Best Answer
0 Votes

The paths when reading file are relative from where the .js file is stored.

So when reading a file the path would be like "./resources/files/somefile.txt"

The above example:

   "./"  - Go up one folder from the JS file.

  "/resources" - Folder to put resources in ( create with the project)

  "/files" - Sub folder to keep our stuff organized.

  "/somefile.txt" - Name of file.

 

Something else to be aware of when reading files that are packaged with the app, they are read only on the device.

Best Answer
0 Votes