Need help creating .txt file

Hi all, I am attempting to export strings to a text file but it isn’t working and this has been a major road block in my app development. I’ve tried everything possible but it just isn’t working. What I am trying to do is to get the path file, create a new directory/folder called “textFiles” and then export the .txts into the textFiles folder.

I have a textmesh that tells me what the Application.persistentDataPath is, and it comes out to /data/data/com.app.app/files. With the create directory code, the new path should be /data/data/com.app.app/files/textFiles, but this clearly isn’t the case…

Further more, when I plug my phone into the computer and go to this directory, it comes out to \Android\data\com.app.app\files. Why is there no second data directory?? (data/data)

After doing some research, some people say that this second data directory is hidden, which makes no sense.

Also, I am strictly limiting this to internal storage. No SD card or external write accesses.

I believe the issue might be that the directory and text file are being created but I am not able to view it. I really can’t hardcore paths because each phone is different, I need to use the persistent data path.

The reason I need a text file is because I am making an app that takes in reports, and I would like to export these reports as text files so people may copy and paste the information into any appropriate program.

Any help would be appreciated, thanks!!!

#pragma strict

import System;
import System.IO;

private var createDirectory : String;

function Awake() {

createDirectory = Application.persistentDataPath + "/textFiles";

}

function OnMouseDown() {

    var clickedTag:String = this.gameObject.tag; //variable for object tag
    if(clickedTag == "reportTXT"){

                 if (!Directory.Exists (createDirectory)) {
             Directory.CreateDirectory(createDirectory);
         }
        else {

                var sr = new File.CreateText(createDirectory + "/MyFile.txt");
                sr.Write("KUSH");
        sr.WriteLine ("This is my file.");
        sr.WriteLine ("I can write ints {0} or floats {1}, and so on.",
            1, 4.2);
        sr.Close();
    
          }
    
        }
    
       }

Anyone??

TextWriter tw = new StreamWriter(Application.persistentDataPath+@"/file.sav");
tw.Write(strToSave);
tw.Close();
2 Likes

Thanks mbowen, I’m using JavaScript and have been able to switch the syntax correctly, however the @ symbol before the “/file.sav” is giving me issues. I can compile the code without the @, but with the @ I get like 5 errors. Thoughts?

Figured it out! When I had write access set to internal only, it was not writing and saving the file, however after changing it to external it is finally working. This also fixed a similar issue I had with taking screenshots using Unity. Now the screenshots save as well. Here is the JavaScript version of the awesome code mbown provided earlier.

Basically, this script just saves a hard-coded textfile when a plane I tagged with “reportTXT” gets tapped on in my app.

function OnMouseDown() {

    var clickedTag:String = this.gameObject.tag; //initialize a string variable that will be equal to whatever tag this gameobject has.

    if(clickedTag == "reportTXT"){ //if this objects tag equals clickedTag variable, which it always should.
 
    var tw : TextWriter; //initialize textwriter variable

    tw = new StreamWriter(Application.persistentDataPath + "/myFile.txt"); //create new textfile in this program's /files directory.

tw.Write("PLEASE WORK"); //string I want entered, in this case it is hard-coded in.

tw.Close(); //close and save this text file

     }

}
1 Like

It was a while ago when I made that code, and the example I used or something told me to use the @ sign. I tried finding a reason again and I found this: