Questions tagged [text-files]

The TXT file type is primarily associated with 'Text File'. Open in Notepad, WordPad, or many other programs designated as text editors. A true text file will be pure ASCII text with no formatting.

6270 questions
1630
votes
19 answers

Why should text files end with a newline?

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?
Will Robertson
  • 55,636
  • 31
  • 91
  • 113
1134
votes
41 answers

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

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): …
SilentGhost
  • 264,945
  • 58
  • 291
  • 279
741
votes
24 answers

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

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?
Justin White
  • 7,643
  • 4
  • 16
  • 9
717
votes
31 answers

How to append text to an existing file in Java?

I need to append text repeatedly to an existing file in Java. How do I do that?
flyingfromchina
  • 8,991
  • 11
  • 33
  • 36
529
votes
27 answers

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

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?
Ray
  • 169,974
  • 95
  • 213
  • 200
348
votes
8 answers

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

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, …
Loren C Fortner
  • 3,795
  • 3
  • 15
  • 12
246
votes
12 answers

How to determine the encoding of text?

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#.
Nope
  • 29,782
  • 41
  • 91
  • 115
245
votes
7 answers

Getting all file names from a folder using C#

I wanted to know if it is possible to get all the names of text files in a certain folder. For example, I have a folder with the name Maps, and I would like to get the names of all the text files in that folder and add it to a list of strings. Is…
user2061405
  • 2,601
  • 3
  • 12
  • 8
215
votes
11 answers

Determine the number of lines within a text file

Is there an easy way to programmatically determine the number of lines within a text file?
TK.
  • 42,559
  • 46
  • 114
  • 145
184
votes
10 answers

How do I read a text file of about 2 GB?

I have a .txt file whose memory is more than 2 GB. The problem is I cannot open it with Notepad, Notepad++ or any other editor programs. Any solutions?
Abhishek Singh
  • 9,083
  • 17
  • 67
  • 97
177
votes
6 answers

Tools to search for strings inside files without indexing

I have to change some connection strings in an incredibly old legacy application, and the programmers who made it thought it would be a great idea to plaster the entire app with connection strings all over the place. Visual Studio's "current…
kitsune
  • 11,098
  • 13
  • 54
  • 77
169
votes
13 answers

Create a .txt file if doesn't exist, and if it does append a new line

I would like to create a .txt file and write to it, and if the file already exists I just want to append some more lines: string path = @"E:\AppServ\Example.txt"; if (!File.Exists(path)) { File.Create(path); TextWriter tw = new…
Berker Yüceer
  • 6,276
  • 17
  • 61
  • 99
134
votes
4 answers

How to add new line into txt file

I'd like to add new line with text to my date.txt file, but instead of adding it into existing date.txt, app is creating new date.txt file.. TextWriter tw = new StreamWriter("date.txt"); // write a line of text to the…
Elfoc
  • 3,421
  • 12
  • 43
  • 57
129
votes
11 answers

Read each line of txt file to new array element

I am trying to read every line of a text file into an array and have each line in a new element. My code so far.
Dan
  • 1,307
  • 3
  • 10
  • 4
122
votes
5 answers

How to create and write to a txt file using VBA

I have a file which is manually added or modified based on the inputs. Since most of the contents are repetitive in that file, only the hex values are changing, I want to make it a tool generated file. I want to write the c codes which are going to…
danny
  • 1,389
  • 2
  • 11
  • 12
1
2 3
99 100