streamreader

Load a simple text file in Android Studio

老子叫甜甜 提交于 2019-11-26 17:38:47
问题 Got a brand new project using Google's new Android Studio IDE. I'm trying to load a simple text file using an InputStreamReader . I'm getting a file not found exception. Now there isn't any assets/ folder. I tried to create one and add my file at many different spots (at the root of the project, at the root of the .java file, etc...) I've tried to move the file around but still get the file not found. Now that never was a problem using Eclipse as there is an assets folder created by any

How do I open an already opened file with a .net StreamReader?

懵懂的女人 提交于 2019-11-26 16:05:30
I have some .csv files which I'm using as part of a test bench. I can open them and read them without any problems unless I've already got the file open in Excel in which case I get an IOException : System.IO.IOException : The process cannot access the file 'TestData.csv' because it is being used by another process. This is a snippet from the test bench: using (CsvReader csv = new CsvReader(new StreamReader(new FileStream(fullFilePath, FileMode.Open, FileAccess.Read)), false)) { // Process the file } Is this a limitation of StreamReader? I can open the file in other applications (Notepad++ for

How to Find And Replace Text In A File With C#

≯℡__Kan透↙ 提交于 2019-11-26 15:45:41
My code so far StreamReader reading = File.OpenText("test.txt"); string str; while ((str = reading.ReadLine())!=null) { if (str.Contains("some text")) { StreamWriter write = new StreamWriter("test.txt"); } } I know how to find the text, but I have no idea on how to replace the text in the file with my own. Read all file content. Make a replacement with String.Replace . Write content back to file. string text = File.ReadAllText("test.txt"); text = text.Replace("some text", "new value"); File.WriteAllText("test.txt", text); You're going to have a hard time writing to the same file you're reading

StreamReader vs BinaryReader?

橙三吉。 提交于 2019-11-26 15:39:32
问题 Both StreamReader and BinaryReader can be used to get data from binary file ( for example ) BinaryReader : using (FileStream fs = File.Open(@"c:\1.bin",FileMode.Open)) { byte[] data = new BinaryReader(fs).ReadBytes((int)fs.Length); Encoding.getstring.... } StreamReader : using (FileStream fs = File.Open(@"c:\1.bin",FileMode.Open)) { using (StreamReader sr = new StreamReader(fs,Encoding.UTF8)) { var myString=sr.ReadToEnd(); } } What is the difference and when should I use which ? 回答1: Both

Adding a Line to the Middle of a File with .NET

雨燕双飞 提交于 2019-11-26 14:45:22
问题 Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the string in the middle of the file more or less. Example: Hello my name is Brandon, I hope someone can help, //I want the string under this line. Thank you. Hopefully someone can help with a solution. Edit Alright thanks guys, I'll try to figure it out, probably going to just rewrite the whole file. Ok well the program I am

How to know position(linenumber) of a streamreader in a textfile?

蹲街弑〆低调 提交于 2019-11-26 14:37:08
问题 an example (that might not be real life, but to make my point) : public void StreamInfo(StreamReader p) { string info = string.Format( "The supplied streamreaer read : {0}\n at line {1}", p.ReadLine(), p.GetLinePosition()-1); } GetLinePosition here is an imaginary extension method of streamreader. Is this possible? Of course I could keep count myself but that's not the question. 回答1: It is extremely easy to provide a line-counting wrapper for any TextReader: public class PositioningReader :

Should I call Close() or Dispose() for stream objects?

被刻印的时光 ゝ 提交于 2019-11-26 13:03:15
Classes such as Stream , StreamReader , StreamWriter etc implements IDisposable interface. That means, we can call Dispose() method on objects of these classes. They've also defined a public method called Close() . Now that confuses me, as to what should I call once I'm done with objects? What if I call both? My current code is this: using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { using (StreamWriter writer = new StreamWriter(filename)) { int chunkSize = 1024; while (!reader.EndOfStream) { char[] buffer = new char

An elegant way to consume (all bytes of a) BinaryReader?

别等时光非礼了梦想. 提交于 2019-11-26 12:28:41
问题 Is there an elegant to emulate the StreamReader.ReadToEnd method with BinaryReader ? Perhaps to put all the bytes into a byte array? I do this: read1.ReadBytes((int)read1.BaseStream.Length); ...but there must be a better way. 回答1: Simply do: byte[] allData = read1.ReadBytes(int.MaxValue); The documentation says that it will read all bytes until the end of the stream is reached. Update Although this seems elegant, and the documentation seems to indicate that this would work, the actual

HTTPWebResponse + StreamReader Very Slow

会有一股神秘感。 提交于 2019-11-26 12:09:25
问题 I\'m trying to implement a limited web crawler in C# (for a few hundred sites only) using HttpWebResponse.GetResponse() and Streamreader.ReadToEnd() , also tried using StreamReader.Read() and a loop to build my HTML string. I\'m only downloading pages which are about 5-10K. It\'s all very slow! For example, the average GetResponse() time is about half a second, while the average StreamREader.ReadToEnd() time is about 5 seconds! All sites should be very fast, as they are very close to my

search text file using c# and display the line number and the complete line that contains the search keyword

两盒软妹~` 提交于 2019-11-26 09:53:25
问题 I require help to search a text file (log file) using c# and display the line number and the complete line that contains the search keyword. 回答1: This is a slight modification from: http://msdn.microsoft.com/en-us/library/aa287535%28VS.71%29.aspx int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt"); while((line = file.ReadLine()) != null) { if ( line.Contains("word") ) { Console.WriteLine (counter