Convert a String to an InputStream in Java

This code example shows how to convert a String to an InputStream. This is sometimes necessary when calling different api:s that require a InputStream object as parameter but the value you need to pass is stored in a String object. To convert the parameter ‘name’ in the example below to an InputStream we create a ByteArrayInputStream object which takes a byte array as parameter, so we simply call getBytes on the String we want to convert. To make sure it is in the proper format we also provide the character set UTF-8 to the getBytes method which we supply using a constant from the StandardCharsets class.
Finally to make sure it has succeeded we print out every byte (character) in the stream on a separate row. Since the read-method of the inputstream returns an int, which is the decimal value of the byte, we convert the int value to a char before we print it out.

The output from the code above will be:

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

Remove a line from a text file

This example shows how to delete a line from a file.
The method removeLineFromFile() takes two parameters, the first parameter is the file to remove from and the second parameter is the content of the line to remove.
A temp file is created and written to, except for the content that matches the second parameter.
This way very large files can be handled without demanding so much internal memory.
The original file is then deleted and the temp file is renamed to the original filename.

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

Take a screenshot and save as image in Java

This code example shows how to take a screen capture and save the data to a file as a PNG-image. The code consists of three methods (plus main method), one that gets the screen size, one that captures the actual screenshot, and one that writes the image data to the file.

Continue reading

How to Use a FileFilter in Java to List Files

In this example we use a FileFilter to list files of a certain type. To implement the FileFilter interface we need to create an accept() method which takes a object as parameter.
In the method we check whether the name ends with “.txt” and if so we return true, else false.
When we have created the FileFilter we can pass it to the listFiles method of the class which will use it to return an array of all files that the filter returned true for in its accept method.
In the example we check the C:\temp folder for files ending with the .txt extension.

Continue reading

Read Data from InputStream to a String

This code example shows how to read data from an InputStream object and ultimately store it in a String object.
To read data from our file we use the method getResourceAsStream() which we get from our class object.
The class object is retrieved by calling getClass() on our Main class.
The method getResourceAsStream() enables us to read a file located within the same jar-file as the actual program, in case it is packaged as such.
It can also be used to read from the root directory of the application as done in the example by adding a slash before the filename.

Continue reading