Questions tagged [file-io]

File I/O is input/output that involves the file system. This could include performing operations on directories and files, such as creation and deletion, reading files, and writing output to files.

For an introduction on File I/O in C, see C Programming / File I/O on Wikibooks.

21104 questions
144
votes
4 answers

How do I append text to a file?

What is the easiest way to append text to a file in Linux? I had a look at this question, but the accepted answer uses an additional program (sed) I'm sure there should be an easier way with echo or similar.
Dchris
  • 2,867
  • 10
  • 42
  • 73
143
votes
4 answers

Does Java have a path joining method?

Exact Duplicate: combine paths in java I would like to know if there is such a method in Java. Take this snippet as example : // this will output a/b System.out.println(path_join("a","b")); // a/b System.out.println(path_join("a","/b");
Geo
  • 93,257
  • 117
  • 344
  • 520
142
votes
6 answers

Append TimeStamp to a File Name

I have come across this problem several times in which I would like to have multiple versions of the same file in the same directory. The way I have been doing it using C# is by adding a time stamp to the file name with something like this…
Mark
  • 1,425
  • 2
  • 9
  • 6
141
votes
18 answers

Objective-C: Reading a file line by line

What is the appropriate way of dealing with large text files in Objective-C? Let's say I need to read each line separately and want to treat each line as an NSString. What is the most efficient way of doing this? One solution is using the NSString…
Jonathan
137
votes
8 answers

Python, Pandas : write content of DataFrame into text File

I have pandas DataFrame like this X Y Z Value 0 18 55 1 70 1 18 55 2 67 2 18 57 2 75 3 18 58 1 35 4 19 54 2 70 I want to write this data to a text file…
Sounak
  • 4,803
  • 7
  • 30
  • 48
136
votes
11 answers

File being used by another process after using File.Create()

I'm trying to detect if a file exists at runtime, if not, create it. However I'm getting this error when I try to write to it: The process cannot access the file 'myfile.ext' because it is being used by another process. string filePath =…
Brett
  • 1,923
  • 3
  • 18
  • 24
135
votes
9 answers

Deleting a file in VBA

Using VBA, how can I: test whether a file exists, and if so, delete it?
inglesp
  • 3,299
  • 9
  • 32
  • 30
135
votes
6 answers

How do I serialize an object and save it to a file in Android?

Say I have some simple class and once it's instantiated as an object I want to be able to serialize its contents to a file, and retrieve it by loading that file at some later time... I'm not sure where to start here, what do I need to do to…
Ben Lesh
  • 107,825
  • 47
  • 247
  • 232
135
votes
11 answers

How to search file text for a pattern and replace it with a given value

I'm looking for a script to search a file (or list of files) for a pattern and, if found, replace that pattern with a given value. Thoughts?
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
134
votes
7 answers

What happens to an open file handle on Linux if the pointed file gets moved or deleted

What happens to an open file handle on Linux if the pointed file meanwhile gets: Moved away -> Does the file handle stay valid? Deleted -> Does this lead to an EBADF, indicating an invalid file handle? Replaced by a new file -> Does the file handle…
Maus
  • 2,724
  • 5
  • 32
  • 37
133
votes
8 answers

How can I lock a file using java (if possible)

I have a Java process that opens a file using a FileReader. How can I prevent another (Java) process from opening this file, or at least notify that second process that the file is already opened? Does this automatically make the second process get…
Paralife
  • 6,116
  • 8
  • 38
  • 64
132
votes
7 answers

What is a good way to handle exceptions when trying to read a file in python?

I want to read a .csv file in python. I don't know if the file exists. My current solution is below. It feels sloppy to me because the two separate exception tests are awkwardly juxtaposed. Is there a prettier way to do it? import csv fName =…
Charles Holbrow
  • 3,967
  • 6
  • 30
  • 35
132
votes
7 answers

Is it necessary to close each nested OutputStream and Writer separately?

I am writing a piece of code: OutputStream outputStream = new FileOutputStream(createdFile); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream); BufferedWriter bw = new BufferedWriter(new…
Adon Smith
  • 1,849
  • 7
  • 19
  • 19
130
votes
4 answers

Is file append atomic in UNIX?

In general, what can we take for granted when we append to a file in UNIX from multiple processes? Is it possible to lose data (one process overwriting the other's changes)? Is it possible for data to get mangled? (For example, each process is…
Lajos Nagy
  • 9,075
  • 11
  • 44
  • 55
129
votes
3 answers

Implementing use of 'with object() as f' in custom class in python

I have to open a file-like object in python (it's a serial connection through /dev/) and then close it. This is done several times in several methods of my class. How I WAS doing it was opening the file in the constructor, and then closing it in the…
Falmarri
  • 47,727
  • 41
  • 151
  • 191