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
103
votes
19 answers

How to read a specific line using the specific line number from a file in Java?

In Java, is there any method to read a particular line from a file? For example, read line 32 or any other line number.
trinity
  • 10,394
  • 15
  • 49
  • 67
102
votes
8 answers

How to get all files under a specific directory in MATLAB?

I need to get all those files under D:\dic and loop over them to further process individually. Does MATLAB support this kind of operations? It can be done in other scripts like PHP,Python...
Gtker
  • 2,237
  • 9
  • 29
  • 37
102
votes
5 answers

How to read a binary file into a vector of unsigned chars

Lately I've been asked to write a function that reads the binary file into the std::vector where BYTE is an unsigned char. Quite quickly I came with something like this: #include #include typedef unsigned char…
LihO
  • 41,190
  • 11
  • 99
  • 167
101
votes
18 answers

File.exists() returns false when file exists

I've encountered a bug I can't seem to find any logic behind. I have this File object, which is created like this: File file = new File("utilities/data/someTextFile.txt"); I then do file.exists(), and it returns false (!?). If the file is not…
atsjoo
  • 2,412
  • 4
  • 20
  • 22
100
votes
8 answers

Delete file from internal storage

I'm trying to delete images stored in internal storage. I've come up with this so far: File dir = getFilesDir(); File file = new File(dir, id+".jpg"); boolean deleted = file.delete(); And this is from another question, which was answered with: File…
Crunch
  • 2,823
  • 5
  • 18
  • 17
100
votes
4 answers

How to read file from res/raw by name

I want to open a file from the folder res/raw/. I am absolutely sure that the file exists. To open the file I have tried File ddd = new File("res/raw/example.png"); The command ddd.exists(); yields FALSE. So this method does not…
Bernd
  • 3,405
  • 3
  • 18
  • 21
100
votes
8 answers

Ant: How to execute a command for each file in directory?

I want to execute a command from an Ant buildfile, for each file in a directory. I am looking for a platform-independent solution. How do I do this? Sure, I could write a script in some scripting language, but this would add further dependencies to…
ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100
100
votes
15 answers

Reliable File.renameTo() alternative on Windows?

Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says, Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from…
Jonik
  • 80,077
  • 70
  • 264
  • 372
99
votes
8 answers

How to delete directory content in Java?

After enumerating a directory, I now need to delete all the files. I used: final File[] files = outputFolder.listFiles(); files.delete(); But this hasn't deleted the directory.
lola
  • 5,649
  • 11
  • 49
  • 61
99
votes
9 answers

File.Move Does Not Work - File Already Exists

I've got a folder: c:\test I'm trying this code: File.Move(@"c:\test\SomeFile.txt", @"c:\test\Test"); I get exception: File already exists The output directory definitely exists and the input file is there.
Jack Kada
  • 24,474
  • 29
  • 82
  • 106
98
votes
8 answers

Creating files in C++

I want to create a file using C++, but I have no idea how to do it. For example I want to create a text file named Hello.txt. Can anyone help me?
Uffo
  • 9,628
  • 24
  • 90
  • 154
98
votes
20 answers

List all files from a directory recursively with Java

I have this function that prints the name of all the files in a directory recursively. The problem is that my code is very slow because it has to access a remote network device with every iteration. My plan is to first load all the files from the…
Hultner
  • 3,710
  • 5
  • 33
  • 43
98
votes
2 answers

Create a file from a ByteArrayOutputStream

Can someone explain how I can get a file object if I have only a ByteArrayOutputStream. How to create a file from a ByteArrayOutputStream?
Al Phaba
  • 6,545
  • 12
  • 51
  • 83
97
votes
3 answers

How to update selected rows with values from a CSV file in Postgres?

I'm using Postgres and would like to make a big update query that would pick up from a CSV file, lets say I got a table that's got (id, banana, apple). I'd like to run an update that changes the Bananas and not the Apples, each new Banana and their…
user519753
  • 1,527
  • 6
  • 18
  • 22
96
votes
15 answers

How can I read a single character at a time from a file in Python?

In Python, given the name of a file, how can I write a loop that reads one character each time through the loop?
kaushik
  • 5,609
  • 4
  • 23
  • 17