Skip to content

Tag: file-io

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: 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 directory. Answer The best way to do it is:

Loading a text file into a textarea

First of all, I am very basic at java. I am trying to browse a .txt file and load the contents of it, into the text area. I am completed the part, till which I receive the file from the JFileChooser, now I dont know how to do the remaining. Answer Use the read(…) and write(…) methods that are supp…

Does Java IO have a maximum file name length limit?

Different operating systems have different file name max lengths. Does Java have any limit on file name length when working with files? Answer Java has no maximum file name length, except obviously for the String max length limit (which is the array max length, i.e. Integer.MAX_VALUE). Maybe some JVMs have a …

Java IOException “Too many open files”

I’m doing some file I/O with multiple files (writing to 19 files, it so happens). After writing to them a few hundred times I get the Java IOException: Too many open files. But I actually have only a few files opened at once. What is the problem here? I can verify that the writes were successful. Answer…

Working with Zip and GZip files in Java

It’s been a while since I’ve done Java I/O, and I’m not aware of the latest “right” ways to work with Zip and GZip files. I don’t necessarily need a full working demo – I’m primarily looking for the right interfaces and methods to be using. Yes, I could look up …

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 directory recursively and then after that go through all files …

Delete directories recursively in Java

Is there a way to delete entire directories recursively in Java? In the normal case it is possible to delete an empty directory. However when it comes to deleting entire directories with contents, it is not that simple anymore. How do you delete entire directories with contents in Java? Answer You should chec…

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 an exception if the file is open (which solves my problem) or…