为什么在重新加载页面时不会保存通过 StreamWriter 写入文件的文本? (赛马林)
Why does text written to a file via StreamWriter not get saved when page is reloaded? (Xamarin)
我正在尝试将多个字符串写入一个 txt 文件,以便在用户切换页面时保存该信息。但是,字符串似乎永远不会被保存。
条目。 (要写入文件的字符串来源)
<Entry
x:Name="Entry"
Placeholder="Username"
WidthRequest = "200"
VerticalOptions = "Start"
HorizontalOptions = "Center"
/>
将信息写入文件的代码。
private void Write()
{
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(txtStorage.Length);
//Writes length so that txtStorage can be correct length later
sw.WriteLine(Entry.Text);
//Writes username entered in this instance of the page
for (int i = 0; i < txtStorage.Length; i++)
{
sw.WriteLine(txtStorage[i]);
//Writes usernames stored from previous instances
};
sw.Close();
}
读取文件的代码。
{
StreamReader sr = new StreamReader(path);
txtStorage = new string[Convert.ToInt32(sr.ReadLine())];
//updates txtstorage to new length written in text file
for (int i = 0; i < txtStorage.Length; i++)
{
txtStorage[i] = sr.ReadLine();
//puts all usernames back into txtstorage
};
sr.Close();
}
不会保存以前实例中的所有用户名。我做错了什么?
当你写文件的时候,你就是在做这个
// 1. write the length of the array
sw.WriteLine(txtStorage.Length);
// 2. write the user name
sw.WriteLine(Entry.Text);
// 3. write the array
for (int i = 0; i < txtStorage.Length; i++)
这将生成类似这样的文件
3
myusername
user1
user2
user3
那么当你读取文件时,你就是这样做的
// 1. read the length as a string
txtStorage = new string[Convert.ToInt32(sr.ReadLine())];
// 2. you aren't doing anything to handle line #2
// 3. you are using the LENGTH of txtstorage, which is 1, so your loop is only executing once
// and since you skipped #2, you are also starting on the wrong line
for (int i = 0; i < txtStorage.Length; i++)
相反,这样做
// 1. read the length as an int
var ndx = int.Parse(sr.ReadLine());
// 2. read the user name
var user = sr.ReadLine();
// 3. use ndx as the loop counter, starting on the correct line
for (int i = 0; i < ndx; i++)
我正在尝试将多个字符串写入一个 txt 文件,以便在用户切换页面时保存该信息。但是,字符串似乎永远不会被保存。
条目。 (要写入文件的字符串来源)
<Entry
x:Name="Entry"
Placeholder="Username"
WidthRequest = "200"
VerticalOptions = "Start"
HorizontalOptions = "Center"
/>
将信息写入文件的代码。
private void Write()
{
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(txtStorage.Length);
//Writes length so that txtStorage can be correct length later
sw.WriteLine(Entry.Text);
//Writes username entered in this instance of the page
for (int i = 0; i < txtStorage.Length; i++)
{
sw.WriteLine(txtStorage[i]);
//Writes usernames stored from previous instances
};
sw.Close();
}
读取文件的代码。
{
StreamReader sr = new StreamReader(path);
txtStorage = new string[Convert.ToInt32(sr.ReadLine())];
//updates txtstorage to new length written in text file
for (int i = 0; i < txtStorage.Length; i++)
{
txtStorage[i] = sr.ReadLine();
//puts all usernames back into txtstorage
};
sr.Close();
}
不会保存以前实例中的所有用户名。我做错了什么?
当你写文件的时候,你就是在做这个
// 1. write the length of the array
sw.WriteLine(txtStorage.Length);
// 2. write the user name
sw.WriteLine(Entry.Text);
// 3. write the array
for (int i = 0; i < txtStorage.Length; i++)
这将生成类似这样的文件
3
myusername
user1
user2
user3
那么当你读取文件时,你就是这样做的
// 1. read the length as a string
txtStorage = new string[Convert.ToInt32(sr.ReadLine())];
// 2. you aren't doing anything to handle line #2
// 3. you are using the LENGTH of txtstorage, which is 1, so your loop is only executing once
// and since you skipped #2, you are also starting on the wrong line
for (int i = 0; i < txtStorage.Length; i++)
相反,这样做
// 1. read the length as an int
var ndx = int.Parse(sr.ReadLine());
// 2. read the user name
var user = sr.ReadLine();
// 3. use ndx as the loop counter, starting on the correct line
for (int i = 0; i < ndx; i++)