编写一个文件,添加随机字符到每行的开头(Writing a file adding random characters to start of each line)
我在Windows Phone 7中使用C#覆盖文件。当我这样做时,每行的开头都会添加一个看似随机的字符。
这是为什么发生?
码:
public static bool overwriteFile(string filename, string[] inputArray) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); FileStream stream = store.OpenFile(filename, FileMode.Create); BinaryWriter writer = new BinaryWriter(stream); foreach (string input in inputArray) { writer.Write(input + "\n"); } writer.Close(); return true; } catch (IOException ex) { return false; } }洛代码:
public static Idea[] getFile(string filename) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); string fileContents = null; if (store.FileExists(filename)) // Check if file exists { IsolatedStorageFileStream save = new IsolatedStorageFileStream(filename, FileMode.Open, store); StreamReader streamReader = new StreamReader(save); fileContents = streamReader.ReadToEnd(); save.Close(); } string[] lines = null; if (fileContents != null) { lines = fileContents.Split('\n'); } Idea[] ideaList = null; if (lines != null) { ideaList = new Idea[lines.Length]; for (int i = 0; i < lines.Length; i++) { ideaList[i] = new Idea(lines[i].TrimEnd('\r')); } } return ideaList; } catch (IOException ex) { return null; } }I'm overwriting a file using C# in Windows Phone 7. When I do this a seemingly random character is added to the start of each line.
Why is this happening?
Code:
public static bool overwriteFile(string filename, string[] inputArray) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); FileStream stream = store.OpenFile(filename, FileMode.Create); BinaryWriter writer = new BinaryWriter(stream); foreach (string input in inputArray) { writer.Write(input + "\n"); } writer.Close(); return true; } catch (IOException ex) { return false; } }Lodaing Code:
public static Idea[] getFile(string filename) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); string fileContents = null; if (store.FileExists(filename)) // Check if file exists { IsolatedStorageFileStream save = new IsolatedStorageFileStream(filename, FileMode.Open, store); StreamReader streamReader = new StreamReader(save); fileContents = streamReader.ReadToEnd(); save.Close(); } string[] lines = null; if (fileContents != null) { lines = fileContents.Split('\n'); } Idea[] ideaList = null; if (lines != null) { ideaList = new Idea[lines.Length]; for (int i = 0; i < lines.Length; i++) { ideaList[i] = new Idea(lines[i].TrimEnd('\r')); } } return ideaList; } catch (IOException ex) { return null; } }最满意答案
随机字符是长度前缀; 请参阅http://msdn.microsoft.com/en-us/library/yzxa6408.aspx 。
您应该使用某种类型的TextWriter将字符串写入文件; 不是BinaryWriter。
StreamWriter可能是最好的,然后你可以使用WriteLine方法。
The random character is a length prefix; see http://msdn.microsoft.com/en-us/library/yzxa6408.aspx.
You should be using some type of TextWriter to write strings to the file; NOT a BinaryWriter.
A StreamWriter might be the best and then you could use the WriteLine method.
更多推荐
发布评论