appcelerator titanium: Creating a new file

2019-04-13 11:09发布

问题:

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...

回答1:

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());
}


回答2:

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.