text-files

What's the fastest way to read a text file line-by-line?

走远了吗. 提交于 2019-11-25 23:59:12
问题 I want to read a text file line by line. I wanted to know if I\'m doing it as efficiently as possible within the .NET C# scope of things. This is what I\'m trying so far: var filestream = new System.IO.FileStream(textFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); var file = new System.IO.StreamReader(filestream, System.Text.Encoding.UTF8, true, 128); while ((lineOfText = file.ReadLine()) != null) { //Do something with the lineOfText } 回答1: To

Why should text files end with a newline?

泄露秘密 提交于 2019-11-25 23:57:53
问题 I assume everyone here is familiar with the adage that all text files should end with a newline. I\'ve known of this \"rule\" for years but I\'ve always wondered — why? 回答1: Because that’s how the POSIX standard defines a line: 3.206 Line A sequence of zero or more non- <newline> characters plus a terminating <newline> character. Therefore, lines not ending in a newline character aren't considered actual lines. That's why some programs have problems processing the last line of a file if it

How to determine the encoding of text?

柔情痞子 提交于 2019-11-25 23:57:15
问题 I received some text that is encoded, but I don\'t know what charset was used. Is there a way to determine the encoding of a text file using Python? How can I detect the encoding/codepage of a text file deals with C#. 回答1: Correctly detecting the encoding all times is impossible . (From chardet FAQ:) However, some encodings are optimized for specific languages, and languages are not random. Some character sequences pop up all the time, while other sequences make no sense. A person fluent in

How do I save a String to a text file using Java?

夙愿已清 提交于 2019-11-25 23:05:36
问题 In Java, I have text from a text field in a String variable called \"text\". How can I save the contents of the \"text\" variable to a file? 回答1: If you're simply outputting text, rather than any binary data, the following will work: PrintWriter out = new PrintWriter("filename.txt"); Then, write your String to it, just like you would to any output stream: out.println(text); You'll need exception handling, as ever. Be sure to call out.close() when you've finished writing. If you are using Java

How can you find and replace text in a file using the Windows command-line environment?

倖福魔咒の 提交于 2019-11-25 22:52:53
问题 I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. \"FOO\") with another (ex. \"BAR\"). What is the simplest way to do that? Any built in functions? 回答1: A lot of the answers here helped point me in the right direction, however none were suitable for me, so I am posting my solution. I have Windows 7, which comes with PowerShell built-in. Here is the script I used to find/replace all instances of text in a file

How to get line count of a large file cheaply in Python?

为君一笑 提交于 2019-11-25 22:20:02
问题 I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most efficient way both memory- and time-wise? At the moment I do: def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 is it possible to do any better? 回答1: You can't get any better than that. After all, any solution will have to read the entire file, figure out how many \n you have, and return that result. Do you have a better way of doing that without

How to delete a line from a text file in C#?

北城以北 提交于 2019-11-25 22:16:01
I have a problem: how can I delete a line from a text file in C#? Read the file, remove the line in memory and put the contents back to the file (overwriting). If the file is large you might want to read it line for line, and creating a temp file, later replacing the original one. For very large files I'd do something like this string tempFile = Path.GetTempFileName(); using(var sr = new StreamReader("file.txt")) using(var sw = new StreamWriter(tempFile)) { string line; while((line = sr.ReadLine()) != null) { if(line != "removeme") sw.WriteLine(line); } } File.Delete("file.txt"); File.Move

How to append text to an existing file in Java

早过忘川 提交于 2019-11-25 21:35:23
问题 I need to append text repeatedly to an existing file in Java. How do I do that? 回答1: Are you doing this for logging purposes? If so there are several libraries for this. Two of the most popular are Log4j and Logback. Java 7+ If you just need to do this one time, the Files class makes this easy: try { Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND); }catch (IOException e) { //exception handling left as an exercise for the reader } Careful : The above