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.
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 java.io.File 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 java.io.File 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.
Here’s an example that can solve the problem to sort rows in a file based upon a certain value on each row.
Say for example that we have a text file which has information about employees. The first value is employee nr, the second is the name of the employee and the third value is the salary.
The values on each row are separated by a comma. In this code example we are going to sort the list ascending on the salary value.
This is what the file ’employees.txt’ looks like:
This code example takes input from user and write the contents of the input to a file for every time the user hits the enter key.
This is continued until the user types the word ‘finished’, then the program exists. A StringBuilder is used to check whether the word ‘finished’ has been typed in.
This code example shows how to change the name of a file.
Loading parameters into a program is easily handled in Java through the Properties class.
The only thing that has to be done is to create an instance of the Properties class and one instance of
FileInputStream class that points to the configuration file.
The actual loading of the parameters are done through the method load() on the Properties object.
In the example below parameters are read from the configuration file and then printed out.
This java code example shows how you can use the StreamTokenizer class to count words and numbers in a file.
In the example we open a stream to a file with help of the FileReader class.
We construct the StreamTokenizer object with this stream as argument and start looping through it.
This java code example illustrates how you can use the RandomAccessFile class.
The example in itself might seem pretty useless but the purpose is to show that you can both write to and read from a file with the same RandomAccessFile instance.
The RandomAccessFile class also enables you to get the current position of the file pointer by calling the method ‘getFilePointer’, or setting the file pointer position by calling the ‘seek’ method.
This example shows how to read objects that has been serialized to a file with an ObjectOutputStream.
We simply create an instance of ObjectOuputStream and loop for as long as there are objects in the file.
You can use the ObjectOutputStream class to write objects to an underlying stream.
In this code example we use it to write objects to a file using the FileOutputStream class as argument to the ObjectOutputStream constructor.
The class from which we create objects to write is a simple java class with three attributes, first name, last name and age. The important thing here is that it implements the Serializable interface.
Any object that is written on an ObjectOutputStream is serialized and therefore has to implement either the java.io.Serializable interface or the java.io.Externalizable interface.