admin管理员组

文章数量:1307608

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

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

Share Improve this question asked Apr 5, 2011 at 11:20 theJavatheJava 15k48 gold badges134 silver badges174 bronze badges 1
  • are you creating a temp file first? Titanium.Filesystem.createTempFile() – rivenate247 Commented Apr 5, 2011 at 17:26
Add a ment  | 

2 Answers 2

Reset to default 7

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

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.

本文标签: javascriptappcelerator titanium Creating a new fileStack Overflow