For some reason, this method is returning a file with zero bytes. It should create a file and return a list. It will return to the console, but won't write to a file.
Here's the call
data.GetStationIDs ("BoulderStations.txt", lat, lon);
and here's the method
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);
I have another method where the same approach is working fine.
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!");
These lines;
//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);
aren't doing what you expect them to. You never write anything to the file and you never flush the stream. In addition to that, you're code is lacking the proper using
statements. If you want to continue using the stream reader/write classes look at their docs on msdn, they have good examples which you're not following at all.
Another option would be to simplify your problem and use the following;
File.WriteAllText(aStringWithDataThatIwantInTheFile);
string fileContents = File.ReadAllText(@"C:\thefilepath.txt");
string[] fileLines = File.ReadAllLines(AStringWithMyFilePath);
File.WriteAllLines(AStringArrayGoesHere);
Sorry I would work with your current code but there is not much point because I would have to completely rewrite the method and I have no idea what data you actually expect to write to the file because you never actually tell the StreamWriter
that you allocate to write anything, you just allocate it and then move on to doing other things.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.