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
238
votes
8 answers

Java: Path vs File

For new applications written in Java 7, is there any reason to use a java.io.File object any more or can we consider it deprecated? I believe a java.nio.file.Path can do everything a java.io.File can do and more.
dogbane
  • 266,786
  • 75
  • 396
  • 414
238
votes
13 answers

mmap() vs. reading blocks

I'm working on a program that will be processing files that could potentially be 100GB or more in size. The files contain sets of variable length records. I've got a first implementation up and running and am now looking towards improving…
jbl
  • 2,710
  • 3
  • 18
  • 13
233
votes
9 answers

Java FileOutputStream Create File if not exists

Is there a way to use FileOutputStream in a way that if a file (String filename) does not exist, then it will create it? FileOutputStream oFile = new FileOutputStream("score.txt", false);
Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84
230
votes
12 answers

How do I concatenate text files in Python?

I have a list of 20 file names, like ['file1.txt', 'file2.txt', ...]. I want to write a Python script to concatenate these files into a new file. I could open each file by f = open(...), read line by line by calling f.readline(), and write each line…
JJ Beck
  • 5,193
  • 7
  • 32
  • 36
226
votes
25 answers

How to delete a whole folder and content?

I want the users of my application to be able to delete the DCIM folder (which is located on the SD card and contains subfolders). Is this possible, if so how?
Beginner
  • 28,539
  • 63
  • 155
  • 235
210
votes
36 answers

Get last n lines of a file, similar to tail

I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item at the bottom. So I need a tail() method that can read n lines from the…
Armin Ronacher
  • 31,998
  • 13
  • 65
  • 69
205
votes
2 answers

What are the Ruby File.open modes and options?

Ruby's File.open takes modes and options as arguments. Where do I find a complete list of modes and options?
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
199
votes
17 answers

How to read and write into file using JavaScript?

Can anybody give some sample code to read and write a file using JavaScript?
ABC
198
votes
6 answers

How to delete a file via PHP?

How do I delete a file from my server with PHP if the file is in another directory? Here is my page layout: projects/backend/removeProjectData.php (this file deletes all my entries for the database and should also delete the related…
Ken
  • 2,654
  • 3
  • 26
  • 29
197
votes
3 answers

Python Pandas: How to read only first n rows of CSV files in?

I have a very large data set and I can't afford to read the entire data set in. So, I'm thinking of reading only one chunk of it to train but I have no idea how to do it.
bensw
  • 2,818
  • 5
  • 21
  • 31
197
votes
10 answers

How to write a UTF-8 file with Java?

I have some current code and the problem is its creating a 1252 codepage file, i want to force it to create a UTF-8 file Can anyone help me with this code, as i say it currently works... but i need to force the save on utf.. can i pass a parameter…
mark smith
  • 20,637
  • 47
  • 135
  • 187
184
votes
6 answers

getResourceAsStream() vs FileInputStream

I was trying to load a file in a webapp, and I was getting a FileNotFound exception when I used FileInputStream. However, using the same path, I was able to load the file when I did getResourceAsStream(). What is the difference between the two…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
182
votes
15 answers

How to read a text file from resources in Kotlin?

I want to write a Spek test in Kotlin. How to read an HTML file from the src/test/resources folder? class MySpec : Spek( { describe("blah blah") { given("blah blah") { var fileContent: String = "" …
Olaf
  • 3,786
  • 4
  • 25
  • 38
178
votes
10 answers

How to copy a file from one directory to another using PHP?

Say I've got a file test.php in foo directory as well as bar. How can I replace bar/test.php with foo/test.php using PHP? I'm on Windows XP, a cross platform solution would be great but windows preferred.
Ali
  • 261,656
  • 265
  • 575
  • 769
177
votes
13 answers

How to create a file in a directory in java?

If I want to create a file in C:/a/b/test.txt, can I do something like: File f = new File("C:/a/b/test.txt"); Also, I want to use FileOutputStream to create the file. So how would I do it? For some reason the file doesn't get created in the right…
Uerefer
  • 1,803
  • 2
  • 12
  • 4