Skip to content

Tag: file

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 lower limit but I never run into such a

How do I get the file extension of a file in Java?

Just to be clear, I’m not looking for the MIME type. Let’s say I have the following input: /path/to/file/foo.txt I’d like a way to break this input up, specifically into .txt for the extension. Is there any built in way to do this in Java? I would like to avoid writing my own parser. Answer In this case, use FilenameUtils.getExtension

How to check whether the file is binary?

I wrote the following method to see whether particular file contains ASCII text characters only or control characters in addition to that. Could you glance at this code, suggest improvements and point out oversights? The logic is as follows: “If first 500 bytes of a file contain 5 or more Control characters – report it as binary file” thank you.

How to create own filetype?

I want to create own filetype to save objects in my app. Basically, I urgently do not need new filetype, but it will be better. I have class. For example Car. It has constructor (String name, String color, int length, Driver driver). When a car is created (its instance), how to save it like a file? Answer To save object

Dumping a Java StringBuilder to File

What is the most efficient/elegant way to dump a StringBuilder to a text file? You can do: But is this efficient for a very long file? Is there a better way? Answer As pointed out by others, use a Writer, and use a BufferedWriter, but then don’t call writer.write(stringBuilder.toString()); instead just writer.append(stringBuilder);. EDIT: But, I see that you accepted a

Too many open file handles

I’m working on a huge legacy Java application, with a lot of handwritten stuff, which nowadays you’d let a framework handle. The problem I’m facing right now is that we are running out of file handles on our Solaris Server. I’d like to know what’s the best way to track open file handles? Where to look at and what can

Simulate touch command with Java

I want to change modification timestamp of a binary file. What is the best way for doing this? Would opening and closing the file be a good option? (I require a solution where the modification of the timestamp will be changed on every platform and JVM). Answer The File class has a setLastModified method. That is what ANT does.

Reading a remote file using Java

I am looking for an easy way to get files that are situated on a remote server. For this I created a local ftp server on my Windows XP, and now I am trying to give my test applet the following address: and of course I receive the following error: URI scheme is not “file” I’ve been trying some other

Java FileReader encoding issue

I tried to use to read some text files and convert them into a string, but I found the result is wrongly encoded and not readable at all. Here’s my environment: Windows 2003, OS encoding: CP1252 Java 5.0 My files are UTF-8 encoded or CP1252 encoded, and some of them (UTF-8 encoded files) may contain Chinese (non-Latin) characters. I