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
176
votes
10 answers

How do you determine the ideal buffer size when using FileInputStream?

I have a method that creates a MessageDigest (a hash) from a file, and I need to do this to a lot of files (>= 100,000). How big should I make the buffer used to read from the files to maximize performance? Most everyone is familiar with the basic…
ARKBAN
  • 3,419
  • 4
  • 24
  • 22
175
votes
6 answers

How to save a BufferedImage as a File

I am using the imgscalr Java library to resize an image . The result of a resize() method call is a BufferedImage object. I now want to save this as a file (usually .jpg). How can I do that? I want to go from BufferedImage -> File but perhaps this…
Ankur
  • 50,282
  • 110
  • 242
  • 312
172
votes
5 answers

Reading file contents on the client-side in javascript in various browsers

I'm attempting to provide a script-only solution for reading the contents of a file on a client machine through a browser. I have a solution that works with Firefox and Internet Explorer. It's not pretty, but I'm only trying things at the…
Damovisa
  • 19,213
  • 14
  • 66
  • 88
172
votes
4 answers

Open files in 'rt' and 'wt' modes

Several times here on SO I've seen people using rt and wt modes for reading and writing files. For example: with open('input.txt', 'rt') as input_file: with open('output.txt', 'wt') as output_file: ... I don't see the modes…
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
167
votes
13 answers

FileSystemWatcher vs polling to watch for file changes

I need to setup an application that watches for files being created in a directory, both locally or on a network drive. Would the FileSystemWatcher or polling on a timer would be the best option. I have used both methods in the past, but not…
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
167
votes
9 answers

Deleting all files in a directory with Python

I want to delete all files with the extension .bak in a directory. How can I do that in Python?
slh2080
  • 1,681
  • 2
  • 11
  • 4
161
votes
19 answers

How to write to a file in Scala?

For reading, there is the useful abstraction Source. How can I write lines to a text file?
yura
  • 14,489
  • 21
  • 77
  • 126
160
votes
7 answers

ValueError : I/O operation on closed file

import csv with open('v.csv', 'w') as csvfile: cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) for w, c in p.items(): cwriter.writerow(w + c) Here, p is a dictionary, w and c both are…
GobSmack
  • 2,171
  • 4
  • 22
  • 28
158
votes
30 answers

How to delete a folder with files using Java

I want to create and delete a directory using Java, but it isn't working. File index = new File("/home/Work/Indexer1"); if (!index.exists()) { index.mkdir(); } else { index.delete(); if (!index.exists()) { index.mkdir(); } }
Mr.G
  • 3,413
  • 2
  • 16
  • 20
155
votes
5 answers

write() versus writelines() and concatenated strings

So I'm learning Python. I am going through the lessons and ran into a problem where I had to condense a great many target.write() into a single write(), while having a "\n" between each user input variable(the object of write()). I came up with: nl…
AbeLinkon
  • 1,675
  • 2
  • 12
  • 9
152
votes
13 answers

The input is not a valid Base-64 string as it contains a non-base 64 character

I have a REST service that reads a file and sends it to another console application after converting it to Byte array and then to Base64 string. This part works, but when the same stream is received at the application, it gets manipulated and is no…
Rohit Verma
  • 1,669
  • 2
  • 11
  • 10
150
votes
6 answers

Why doesn't java.io.File have a close method?

While java.io.RandomAccessFile does have a close() method java.io.File doesn't. Why is that? Is the file closed automatically on finalization or something?
Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105
150
votes
4 answers

Split files using tar, gz, zip, or bzip2

I need to compress a large file of about 17-20 GB. I need to split it into several files of around 1 GB per file. I searched for a solution via Google and found ways using the split and cat commands. But they did not work for large files at all.…
Aka
  • 2,121
  • 4
  • 17
  • 11
149
votes
14 answers

How to write a multidimensional array to a text file?

In another question, other users offered some help if I could supply the array I was having trouble with. However, I even fail at a basic I/O task, such as writing an array to a file. Can anyone explain what kind of loop I would need to write a…
Ivo Flipse
  • 10,222
  • 18
  • 50
  • 63
145
votes
12 answers

SVN command to delete all locally missing files

In SVN is there a command I can use to delete all locally missing files in a directory? Or failing that, some way of listing only those files that are missing (or, in the more general case, have status A, D, ?, etc.)
wheresrhys
  • 22,558
  • 19
  • 94
  • 162