- Home /
Help me about reading .TXT File
Hi there i have one TXT hile and i wanna use it as one word with one number. Text file...
Character / 0934
US / 0424
Seoul / 0426
etc...
and i wanna read one line such as "seoul, 0426"
FTR the easiest way by far to do this is just use Unity's http://docs.unity3d.com/$$anonymous$$anual/class-TextAsset.html text asset class, as FIreDude explains.
Answer by Eric5h5 · Mar 05, 2010 at 01:28 PM
Use the System.IO
classes, such as StreamReader:
import System.IO;
var fileName = "foo.txt";
function Start () {
var sr = new StreamReader(Application.dataPath + "/" + fileName);
var fileContents = sr.ReadToEnd();
sr.Close();
var lines = fileContents.Split("\n"[0]);
for (line in lines) {
print (line);
}
}
This reads in the contents of the named text file and splits into separate strings on the newline char.
The "var lines = fileContents.Split("\n"[0]);" line results in a string array, so "lines[134]" would be the 135th line. You can use Split again to split the line into separate elements.
"lines" is a string array...you can't Split a string array, only individual strings. So you'd use "var lineElements = lines[x].Split("/"[0]);", where x is the line number you want to split.
For iPhone and Android, you need to use the correct file path.
I just made a blog post explaining how to obtain a valid file path in a platform-independent way (working on both PC, $$anonymous$$ac, iOS and Android):
Normally in .Net you should use System.IO's Path.Combine() to combine folder and filenames versus directly working with '\' and '/' symbols.
ex: string fullPath = Path.Combine(Application.dataPath, filename);
Answer by fireDude67 · Dec 30, 2010 at 05:16 AM
You can use the Unity Text Asset class, and then use a regex to split it.
Simply do this...
public TextAsset dictionaryTextFile;
and then literally drop your text file in to the project. It would be called something.txt. In the example it is called yourTextFIle.txt...
To convert a TextAsset
to one long string, is completely trivial.
You just go .text
. That's all there is to it.
That's exactly the reason we all use Unity, rather than actually bothering programming.
You then just convert that one long string to a List<>
of strings.
So, in your code, simply do this:
public TextAsset dictionaryTextFile;
private string theWholeFileAsOneLongString;
private List<string> eachLine;
void Start()
{
theWholeFileAsOneLongString = dictionaryTextFile.text;
eachLine = new List<string>();
eachLine.AddRange(
theWholeFileAsOneLongString.Split("\n"[0]) );
// you're done.
Debug.Log(eachLine[4]);
Debug.Log(eachLine[10]);
Debug.Log(eachLine[101]);
Debug.Log(eachLine[0]);
int kWords = eachLine.Count;
Debug.Log(eachLine[kWords-1]);
}
AddRange
and .Split("\n"[0])
are just methods to easily convert a long string
which is separated by newlines, to, a List<>
of words.
Fortunately it's that easy.
@fireDude67 Thank you good sir! After 8 years, this still works great!
Answer by StephanK · Mar 05, 2010 at 01:09 PM
You can use the standard c# file api.
If you want to run it in the webplayer or you are using javascript you could use the WWW class to do this.
var pathToFile = "path/to/example.txt"; var url = "file://" + pathToFile; yiel download = new WWW(url);
text = download.data;
pathToFile needs to be a complete path. You could also use a path relative to your unity project. Then you'd have to do this to run in the editor:
var url = "file://" + Application.dataPath + "/../" + pathToFile;
There is no "C# file api". There is only the .NET file API, which works perfectly fine in Javascript or Boo. Also, "file://" doesn't work in the webplayer.
I meant .NET, sorry for being vague. As I don't use javascript or Visual Basic C# and .NET are pretty much the same to me. However you are right, C# is not .NET. "file://" won't work in the web player use "http://" ins$$anonymous$$d. Would StreamReader and Application.dataPath work in a WebPlayer context?
StreamReader doesn't work in the web player, and dataPath returns the absolute URL of the unity file in that case.