Skip to content
Advertisement

Tag: io

Moving files from one directory to another with Java NIO

I am using the NIO libraries but I am getting a strange error when I try to move files from one directory to another. Iterate over elements that start with “2014” and move them in the new directory (newDir, which is also called 2014) I get the java.nio.file.FileAlreadyExistsException because my folder (2014) already exists. What I actually want to do

Setting file permissions returns FALSE always

The code: On Ubuntu all 3 calls return false. On my Windows only the 3rd call to setWritable returns false. The target is to create the file/dir so the user (tomcat) and the group will be able to read/write. BUT the file created on Ubuntu without permissions for the group to write. Answer I found the solution and will answer

Java Read Large Text File With 70million line of text

I have a big test file with 70 million lines of text. I have to read the file line by line. I used two different approaches: and Is there another approach that can make this task faster? Answer 1) I am sure there is no difference speedwise, both use FileInputStream internally and buffering 2) You can take measurements and see

Sending Java POST request without calling getInputStream()

I would like to send a POST request in Java. At the moment, I do it like this: I do not understand why I have to call con.getInputStream() in order to actually send the request. If I do not call it, the request is not sent. Is there a problem using PrintStream? It should not matter if I take a

How to check for a sequence of keystrokes

I’m making a game and want to implement cheat codes like the Konami code. But how do I check for that sequence of keystrokes? I want it to work so that if a player just types the code it will trigger. Thanks in advance! Answer Below is a class that checks for the Konami code, including cases such as “UP,

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

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

Advertisement