简体   繁体   中英

File Read-Write Error in iOS

I have used following code for file reading and writing.

 private void StorePuzzleData ()
 {
     FileInfo fileInfo = new FileInfo (Application.persistentDataPath + "\\" + difficultyLevel + puzzleId + ".txt");

     if (fileInfo.Exists)
         fileInfo.Delete ();

     string fileData = string.Empty;

     foreach (CellInformation cellInfo in cellInfoList)
         fileData += cellInfo.RowIndex + "#" + cellInfo.ColIndex + "#" + cellInfo.number + "#" + cellInfo.CellColor + "#" + cellInfo.CellDisplayColor + "#" + (cellInfo.IsGroupComplete ? 1 : 0) + ",";

     StreamWriter streamWriter = fileInfo.CreateText ();
     streamWriter.WriteLine (fileData);
     streamWriter.Close ();

     DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);

 }

 private void ReadPuzzleData ()
 {
     // format:  rownumber, colnumber, number, cellcolor, celldisplaycolor, isgroupcomplete

     StreamReader streamReader = File.OpenText (Application.persistentDataPath + "\\" + difficultyLevel + puzzleId + ".txt");
     string fileData = streamReader.ReadLine ();
 }

But I am getting following error in actual iOS device running. This code working correct in iMac as well in android device.

在此处输入图片说明

在此处输入图片说明

Please give me some suggestion what changes I need to do to make this correct.

It seems you're using Windows-style paths in a Unix-like (Apple Mac OS) environment. Notice that on windows you have paths with a backslash like

   C:\Users\Maxi\Desktop

On Unix-like system however something like

  /var/mobile/Containers

You notice that in your faulty path you have mixed forward and backward slashes, which makes the path invalid.

/var/mobile/Containers/Data/Application/2.....\debutan1.txt

The correct way to always generate the correct path is to use the Path.Combine(string, string) function. This will combine two paths using the correct directory path seperator, which can also be seperatly accessed through Path.DirectorySeparatorChar .

So, in order to make your code correct, you would do

 using System.IO; /* must be imported */

 private void StorePuzzleData ()
 {
     FileInfo fileInfo = new FileInfo (Path.Combine(Application.persistentDataPath, difficultyLevel + puzzleId + ".txt"));

     if (fileInfo.Exists)
         fileInfo.Delete ();

     string fileData = string.Empty;

     foreach (CellInformation cellInfo in cellInfoList)
         fileData += cellInfo.RowIndex + "#" + cellInfo.ColIndex + "#" + cellInfo.number + "#" + cellInfo.CellColor + "#" + cellInfo.CellDisplayColor + "#" + (cellInfo.IsGroupComplete ? 1 : 0) + ",";

     StreamWriter streamWriter = fileInfo.CreateText ();
     streamWriter.WriteLine (fileData);
     streamWriter.Close ();

     DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);

 }

 private void ReadPuzzleData ()
 {
     // format:  rownumber, colnumber, number, cellcolor, celldisplaycolor, isgroupcomplete

     StreamReader streamReader = File.OpenText (Path.Combine(Application.persistentDataPath, difficultyLevel + puzzleId + ".txt"));
     string fileData = streamReader.ReadLine ();
 }

If this still gives an "Access denied" error it must be because of filepermissions. Post the output of ls -la <thatpath> then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM