Skip to content
Advertisement

Tag: java-io

How to sort files in a stream by partial filename?

I use the following to sort files first by name, then by modification date: Question: how can I refactor the File::getName part to only compare based on partial filename, like: StringUtils.substringBefore(file.getName(), “_”)? Answer You could replace the method reference with a lambda expression on the File object, although you’ll have to specify the types (see the additional discussion here):

How to use environment variables in CMD using Java?

I am new to using the ProcessBuilder object to run commands inside of Java. My issue is that when I put my environment variables into the builder.command(…) method, it doesn’t work. However, when I hardcode the strings of the environment variables, it works perfectly. Here is my code below along with an explanation to help make clear on what I

Java: Why does my program receive java.io.FileNotFoundException error for creating an input file when it successfully creates an output file?

I created two files in this program: “OutputFile.txt” and “InputFile.txt”. When I run my code, it displays an error called “java.io.FileNotFoundException” but it created “OutputFile.txt” in my system but not “InputFile.txt” Why is that? Answer 2 File objects are created: outFile and inFile, this will create text files in my system The first part of this is correct; the second

The Properties.load would close the InputStream?

I saw this example, and I didn’t see the close() method invoked on the InputStream, so would prop.load() close the stream automatically? Or is there a bug in the example? Answer The Stream is not closed after Properties.load () The above code returns “-1” so the stream is not closed. Otherwise it should have thrown java.io.IOException: Stream Closed

BufferedReader vs Console vs Scanner

Hi I’m new to Java and I would like to know what is the best choice to read a user Input in the console, as far as I know there are 3 ways to do it: Console console = System.console(); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Scanner reader = new Scanner(System.in); Which one should I choose? Why that one and

Advertisement