appcelerator titanium: Creating a new file

2019-04-13 10:49发布

How to create a new file in appcelerator titanium.

  var Settings = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory,'Settings');
  Ti.API.info("Created Settings: " + Settings.createDirectory());
  Ti.API.info('Settings ' + Settings);
  var newFile = Titanium.Filesystem.getFile(Settings.nativePath,'Settings.txt');
  newFile.write('line 1\n');
  Ti.API.info('newfile: '+newFile.read());

The Above code is not working...

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-13 10:53

Using newFile.createFile(); will throw error. It seems depricated in 3.0 as I did not find it woking with me. I tried newfile.write('Some data'); and it worked.

查看更多
趁早两清
3楼-- · 2019-04-13 11:03

Try creating the file before writing to the file:

var Settings = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory,'Settings');
Ti.API.info("Created Settings: " + Settings.createDirectory());
Ti.API.info('Settings ' + Settings);
var newFile = Titanium.Filesystem.getFile(Settings.nativePath,'Settings.txt');

newFile.createFile();

if (newFile.exists()){
    newFile.write('line 1\n');
    Ti.API.info('newfile: '+newFile.read());
}
查看更多
登录 后发表回答