[英]Info not writing to text file
由於某種原因,此方法返回的文件為零字節。 它應該創建一個文件並返回一個列表。 它會返回到控制台,但不會寫入文件。
這是電話
data.GetStationIDs ("BoulderStations.txt", lat, lon);
這是方法
public List<string> GetStationIDs (string filename, string lat, string lon) {
//specify file name, instructions, and priveleges
FileStream CurrentDataFile = new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write);
//create a new stream to write to the file
StreamWriter CurrentData = new StreamWriter (CurrentDataFile);
List<string> StationIDList = new List<string>();
string url = @"http://api.wunderground.com/api/" + wundergroundkey + "/geolookup/q/" + lat + "," + lon + ".json";
Uri uri = new Uri(url);
WebRequest webRequest = WebRequest.Create(uri);
WebResponse response = webRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
String responseData = streamReader.ReadToEnd();
var container = JsonConvert.DeserializeObject<HistoryResponseContainer>(responseData);
foreach (var pws in container.location.nearby_weather_stations.pws.station) {
CurrentData.Write (pws.id + " " + pws.lat + " " + pws.lon);
}
foreach (var pws in container.location.nearby_weather_stations.pws.station) {
StationIDList.Add(pws.id);
}
return (StationIDList);
我有另一種方法,其中相同的方法工作正常。
public void GetFiveMinuteData(List<String> StationIDList, String Date, String Filename) {
//specify file name, instructions, and priveleges
FileStream CurrentDataFile = new FileStream (Filename, FileMode.OpenOrCreate, FileAccess.Write);
//create a new stream to write to the file
StreamWriter CurrentData = new StreamWriter (CurrentDataFile);
foreach (String StationID in StationIDList) {
string url = @"http://api.wunderground.com/api/" + wundergroundkey + "/history_" + Date + "/q/pws:" + StationID + ".json";
Uri uri = new Uri (url);
WebRequest webRequest = WebRequest.Create (uri);
WebResponse response = webRequest.GetResponse ();
StreamReader streamReader = new StreamReader (response.GetResponseStream ());
String responseData = streamReader.ReadToEnd ();
var container = JsonConvert.DeserializeObject<HistoryResponseContainer> (responseData);
foreach (var observation in container.history.observations) {
CurrentData.Write (StationID + " " + Date + " ");
// This makes easier access to the date. not perfect, but better.
DateTime date = observation.date.Value;
DateTime utc = observation.utcdate.Value;
// whatever you want to do with each observation
if (date.Minute == 0 || date.Minute % 5 == 0) {
CurrentData.Write (date.Hour + ":" + date.Minute + " " + observation.wdird + " " + observation.wspdi);
}//end if
CurrentData.Write ("\n");
} //End foreach observation
} //end foreach station
Console.WriteLine ("CurrentDataFile complete!");
這些行;
//specify file name, instructions, and priveleges
FileStream CurrentDataFile = new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write);
//create a new stream to write to the file
StreamWriter CurrentData = new StreamWriter (CurrentDataFile);
沒有按照您的期望去做。 您永遠不會將任何內容寫入文件,也永遠不會刷新流。 除此之外,您的代碼缺少適當的using
語句。 如果您想繼續使用流閱讀器/寫入類,請查看它們在msdn上的文檔,它們提供了很好的示例,您根本無需關注。
另一種選擇是簡化您的問題並使用以下內容;
File.WriteAllText(aStringWithDataThatIwantInTheFile);
string fileContents = File.ReadAllText(@"C:\thefilepath.txt");
string[] fileLines = File.ReadAllLines(AStringWithMyFilePath);
File.WriteAllLines(AStringArrayGoesHere);
抱歉,我將使用您當前的代碼,但是沒有太多意義,因為我將不得不完全重寫該方法,並且我不知道您實際上希望將什么數據寫入文件,因為您從未真正告訴StreamWriter
您分配寫入的數據任何事情,您只需分配它,然后繼續做其他事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.