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.

6722 questions
1989
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
  • 62,540
  • 32
  • 99
  • 117
1278
votes
44 answers

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

How do I get a line count of a large file in the most memory- and time-efficient manner? def file_len(filename): with open(filename) as f: for i, _ in enumerate(f): pass return i + 1
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
1048
votes
31 answers

Reading a plain text file in Java

It seems there are different ways to read and write data of files in Java. I want to read ASCII data from a file. What are the possible ways and their differences?
Tim the Enchanter
  • 10,837
  • 4
  • 21
  • 20
998
votes
22 answers

How can I read a large text file line by line using Java?

I need to read a large text file of around 5-6 GB line by line using Java. How can I do this quickly?
manoj singh
  • 10,045
  • 3
  • 15
  • 3
804
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
  • 8,273
  • 4
  • 19
  • 9
769
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
  • 9,571
  • 12
  • 35
  • 38
603
votes
30 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
  • 187,153
  • 97
  • 222
  • 204
390
votes
9 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
  • 4,217
  • 3
  • 18
  • 12
308
votes
16 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
  • 34,682
  • 42
  • 94
  • 119
294
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
  • 3,141
  • 3
  • 14
  • 9
277
votes
24 answers

Read resource text file to String in Java

Is there a way to read a text file in the resource into a String? I suppose this is a popular requirement, but I couldn't find any utility after Googling.
Loc Phan
  • 4,304
  • 4
  • 29
  • 35
234
votes
12 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.
  • 46,577
  • 46
  • 119
  • 147
194
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
  • 10,243
  • 22
  • 74
  • 108
187
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,516
  • 13
  • 57
  • 78
185
votes
15 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
  • 7,026
  • 18
  • 68
  • 102
1
2 3
99 100