繁体   English   中英

信息未写入文本文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM