Read lines of text from a file with the BufferedReader class

This example shows how to read the contents of a file line by line.
By using the BufferedReader class we can read line-by-line without having to worry about
linebreaks. We just call it’s readLine() method for as long as it doesn’t return null, which it does when it has reached the end of the file.
Don’t forget to call close() on the BufferedReader when done.

Continue reading

Write to file using a BufferedWriter

When you want to output text to a file it’s better to use a Writer class instead of a OutputStream such as the BufferedOutputStream since the purpose of Writer classes are to handle textual content.
With the BufferedWriter (as opposed to the BufferedOutputStream) you don’t have to translate your String parameter to a byte array, and there is also a handy method for writing a new line character.
In this example we simply write two lines of text, and finally we call flush on the BufferedWriter object before closing it.

Continue reading

Convert a String to int

The first and fastest way to convert a String to an integer value is to use the static method valueOf() of the Integer class. The valueOf() method can be used by other primitive wrapper classes too to convert between different types.

Another way is to use the static method parseInt of the Integer class to convert
a String to an int as below:

Continue reading

List file structure within folder or hard drive

This code example lists the files and folders and also the content in all subfolders.
With recursive calls the file/directory tree can be listed with a relatively small amount of code.
The purpose of the getTabs method and the variable tabCounter is only to make the output more viewable with indenting for each level.

What we do is to create a new File object with a path to the folder from where we want to begin to list the content.
Then we check that the File object really exists and that it points to a folder (not a physical file). If it does, we exit the program.
The we use the Arrays class to convert the array to a Collection object (a List) and use foreach method to loop on that list, passing an anonymous function to it.
For each item in the list we print out the name, and depending on what level in the structure the item is in we add tabs before the name to make it more viewable.
Down in the main-method we create an instance of the FileStructure class and provide the directory as parameter to the listFilesAndFolders method.

Continue reading

Count occurences of a String in another String in Java using Apache Commons

Here is a small example of how to count character sequences within a String using the Apache Commons StringUtils class.
The StringUtils class comes with a method countMatches() which takes two parameters, the String to analyze and the substring or sequence of characters to look for.
The method is case-sensitive so even if we have two occurences of the word ‘the’ below the method will only find one since the other is capitalized:

However, we can use the standard Java String-method toLowerCase if we want to ignore case: