Simple File creating + Overwriting

Is there something wrong with this code:

using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class Files : MonoBehaviour {
	public string filename="";
	public string[] args;
	
	void Update() {
		if(Input.GetKeyDown("t")) {
			Read(args);
		}
	}
	
	void Read(string[] strs) {
		StreamWriter w = File.AppendText(Application.persistentDataPath+"/"+filename+".txt");
		foreach(string s in strs) {
			Log(s, w);
		}
		w.Close();
		
		StreamReader r = File.OpenText(Application.persistentDataPath+"/"+filename+".txt");
		DumpLog(r);
		r.Close();
	}
	
	void Log(string logMessage, TextWriter w) {
		w.Write("\r\n"+logMessage);
		w.Flush();
	}
	
	void DumpLog(StreamReader r) {
		string line = r.ReadLine();
		while(line != null) {
			Debug.Log(line);
		}
		r.Close();
	}
}

because for some reason, everytime I hit “t”, Unity freezes, but the file is created and all of the info is in the file… is there something I’m missing? a Close(); somewhere?

Yes, DumpLog() is not reading a new line within the While loop, and is permanently looping reading the initial line. ReadLine() should occur within the while loop.

Wow. ok, thanks! works perfectly.

I want to learn how to make .txt files on my computer with unity application :frowning: I’m doing everything with Playerprefs