file-handling

Write text files without Byte Order Mark (BOM)?

落爺英雄遲暮 提交于 2019-11-26 04:19:58
问题 I am trying to create a text file using VB.Net with UTF8 encoding, without BOM. Can anybody help me, how to do this? I can write file with UTF8 encoding but, how to remove Byte Order Mark from it? edit1: I have tried code like this; Dim utf8 As New UTF8Encoding() Dim utf8EmitBOM As New UTF8Encoding(True) Dim strW As New StreamWriter(\"c:\\temp\\bom\\1.html\", True, utf8EmitBOM) strW.Write(utf8EmitBOM.GetPreamble()) strW.WriteLine(\"hi there\") strW.Close() Dim strw2 As New StreamWriter(\"c:\

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

元气小坏坏 提交于 2019-11-26 04:06:56
问题 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. 回答1: 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

Return value of assignment operation in Java

旧巷老猫 提交于 2019-11-26 03:59:33
问题 I encountered a statement in Java while ((line = reader.readLine()) != null) { out.append(line); } How do assignment operations return a value in Java? The statement we are checking is line = reader.readLine() and we compare it with null . Since readLine will return a string, how exactly are we checking for null ? 回答1: The assignment operator in Java returns the assigned value (like it does, e.g., in c). So here, readLine() will be executed, and it's return value stored in line . That value

How to read line by line or a whole text file at once?

前提是你 提交于 2019-11-26 03:49:41
问题 I\'m in a tutorial which introduces files (how to read and write from\\to file) First of all, this is not a homework, this is just general help I\'m seeking. I know how to read one word at a time, but I don\'t know how to read one line at a time or how to read the whole text file. What if my file contains 1000 words? It is not practical to read each word. My text file named (Read) contains the following: I love to play games I love reading I have 2 books This is what I have accomplished so

How to move a file in Python

*爱你&永不变心* 提交于 2019-11-25 22:30:00
问题 I looked into the Python os interface, but was unable to locate a method to move a file. How would I do the equivalent of $ mv ... in Python? >>> source_files = \'/PATH/TO/FOLDER/*\' >>> destination_folder = \'PATH/TO/FOLDER\' >>> # equivalent of $ mv source_files destination_folder 回答1: os.rename(), shutil.move(), or os.replace() All employ the same syntax: import os import shutil os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo") shutil.move("path/to/current/file