- Home /
System.IO in javascript
Ive looked online and doesn't have much detail on using the StreamWriter or StreamReader in javascript language only in c# and c++. Does anyone have any examples I can look at so I can understand how to make it work. What I want to use it for is I have several variables that I want to write into a txt file. Its basically a data structure that I am looking to create from using StreamWriter. Example is I have multiple users that can have access to there records like username,firstname,lastname. Now when I want to search for a specific username how would I access there information in txt file. I was thinking designing it by this example (username,firstname,lastname). Any help of understanding IO would be greatly appreciated.
Answer by Graham-Dunnett · Apr 17, 2013 at 10:31 AM
When using Javascript pretty much all of the .Net classes work exactly the same, you just need to remember you are working in JS and not c#, so things like variable declarations need to change. Taking the MSDN StringWriter example you end up with:
#pragma strict
import System.IO;
function Start () {
var dirs : DirectoryInfo[] = new DirectoryInfo("/Users/graham").GetDirectories();
var sw : StreamWriter = new System.IO.StreamWriter("DriveDirs.txt");
sw.WriteLine("Found the following folder:");
for (var dir : DirectoryInfo in dirs){
sw.WriteLine(dir.Name);
}
sw.Close();
var sr : StreamReader = new System.IO.StreamReader("DriveDirs.txt");
Debug.Log(sr.CurrentEncoding);
var line : String;
while (!sr.EndOfStream) {
line = sr.ReadLine();
Debug.Log(line);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100456im_/https://answers.unity.com/themes/thub/images/avi.jpg)