C# 由类将文件读入列表不起作用
我用c#中的xamarin编写了一个android应用程序,并创建了一个应用程序类来管理包含要加载到列表中的数据的文件 应用程序类:C# 由类将文件读入列表不起作用,c#,android,xamarin,C#,Android,Xamarin,我用c#中的xamarin编写了一个android应用程序,并创建了一个应用程序类来管理包含要加载到列表中的数据的文件 应用程序类: namespace soroksar_sc_stat { [Application] public class GetDataClass : Android.App.Application { public GetDataClass (){} private string filename = Path.Combine(System.Envir
namespace soroksar_sc_stat
{
[Application]
public class GetDataClass : Android.App.Application
{
public GetDataClass (){}
private string filename = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),"playerdata.txt");
private DirectoryInfo di = Directory.CreateDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData));
private FileStream myFile = new FileStream(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),"playerdata.txt"), FileMode.Create);
public List<string> GetList()
{
StreamReader myReader = new StreamReader(this.myFile);
string line;
List<string> list = new List<string>();
while((line = myReader.ReadLine()) != null)
{
list.Add(line);
}
myReader.Close ();
list.Add ("Blabla");
return list;
}
public void SetNewData (string playerName, DateTime bornDate)
{
string newLine = playerName + " " + bornDate.ToString () + " 0 0 0 0";
StreamWriter myWriter = new StreamWriter(this.myFile);
myWriter.WriteLine(newLine);
myWriter.Close();
}
}
}
名称空间soroksar\u sc\u stat
{
[申请]
公共类GetDataClass:Android.App.Application
{
公共GetDataClass(){}
私有字符串filename=Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),“playerdata.txt”);
private DirectoryInfo di=Directory.CreateDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData));
private FileStream myFile=new FileStream(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),“playerdata.txt”)、FileMode.Create);
公共列表GetList()
{
StreamReader myReader=新的StreamReader(此.myFile);
弦线;
列表=新列表();
而((line=myReader.ReadLine())!=null)
{
列表。添加(行);
}
myReader.Close();
列表。添加(“Blabla”);
退货清单;
}
public void SetNewData(字符串playerName、DateTime bornDate)
{
字符串换行符=playerName+“”+bornDate.ToString()+“0”;
StreamWriter myWriter=新的StreamWriter(此.myFile);
myWriter.WriteLine(新线);
myWriter.Close();
}
}
}
应显示列表的活动
namespace soroksar_sc_stat
{
[Activity (Label = "DataActivity")]
public class DataActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.DataLayout);
Button addbutton = FindViewById<Button> (Resource.Id.dataButton);
ListView lista = FindViewById<ListView> (Resource.Id.listView1);
GetDataClass dataList = new GetDataClass();
List<string> list = dataList.GetList();
addbutton.Click += delegate
{
Intent intent = new Intent(this, typeof(AddDataActivity));
this.StartActivity(intent);
};
}
}
}
名称空间soroksar\u sc\u stat
{
[活动(Label=“DataActivity”)]
公共类数据活动:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.DataLayout);
Button addbutton=FindViewById(Resource.Id.dataButton);
ListView lista=FindViewById(Resource.Id.listView1);
GetDataClass dataList=新建GetDataClass();
List=dataList.GetList();
添加按钮。单击+=委派
{
意向意向=新意向(此,类型为(AddDataActivity));
这一点。触觉(意图);
};
}
}
}
第一节课是阅读课。现在它是一个空文件,但是为了进行故障排除,我在读取的末尾添加了一个字符串。但是,如果我在第二个活动(第二个代码)中启动emulator,则列表中没有任何项目。我真的不知道问题出在哪里,如果有人能帮助我,我将非常感激。正如我之前在评论中提到的,我认为因为您没有正确关闭streamreader,它可能在写入文件之前就关闭了。解决方案如下:
public void SetNewData (string playerName, DateTime bornDate)
{
string newLine = playerName + " " + bornDate.ToString () + " 0 0 0 0";
using(StreamWriter myWriter = new StreamWriter(this.myFile))
myWriter.WriteLine(newLine);
}
请注意:第一种方法是我的首选方法,因为它确保所有清理都正确完成。此外,还应使用using块进行读取
using(StreamReader myReader = new StreamReader(this.myFile))
{
string line;
List<string> list = new List<string>();
while((line = myReader.ReadLine()) != null)
{
list.Add(line);
}
}
使用(StreamReader myReader=newstreamreader(this.myFile))
{
弦线;
列表=新列表();
而((line=myReader.ReadLine())!=null)
{
列表。添加(行);
}
}
我猜您没有正确关闭streamreader/writer-要么使用using block
将声明包装在中,要么使用flush
谢谢,我尝试了一个using block,但它不起作用。你能帮我个忙吗,我应该怎样使用flush?我附上了一个答案,显示了两种方式,如果不是这样,那么从给出的代码中很难判断,你可能需要验证你传递的行包含数据。无论如何,我的建议对您来说仍然是一个改进。我没有测试文件写入部分,但我使用streamreader执行了完全相同的解决方案,在返回列表之前,我添加了一个字符串,因为现在文件为空。我的问题是,在活动中,当我调用该类时,它返回一个空列表。(我也覆盖了streamwriter部分,还是一样)我也要感谢您的帮助,我以前从未使用过,但现在我知道它什么时候能派上用场:)@gepont-老实说,我没有费心看活动,因为如果文件中没有任何数据,我怀疑这是问题所在。再看一看,这可能是因为您没有以与上面示例中所示类似的方式关闭FileStream
using(StreamReader myReader = new StreamReader(this.myFile))
{
string line;
List<string> list = new List<string>();
while((line = myReader.ReadLine()) != null)
{
list.Add(line);
}
}