信息不写入文本文件

时间:2014-01-18 03:23:10

标签: c# file-io

由于某种原因,此方法返回一个零字节的文件。它应该创建一个文件并返回一个列表。它将返回到控制台,但不会写入文件。

这是电话

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!");

1 个答案:

答案 0 :(得分:1)

这些行;

//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);

很抱歉,我会使用您当前的代码,但没有太大意义,因为我必须完全重写该方法,我不知道您实际期望写入该文件的数据,因为您实际上从未告诉{{1你分配给任何东西,你只需要分配它,然后继续做其他事情。