Home » Tutorial


Head of a Tutorial Trail

DS01 SingleLinkedList and Iterator

SingleLinkedList, Iterator and Unit Test This is the first article in a data structures tutorial series. We start with a linked list that is a singly linked list. This means it has nodes with only forward pointing pointers. This linked list is also an Iterator which is a design pattern. My version of this iterator has a reset() method to set the position pointer back to the first node in the list. Also this list does not give you access to the nodes themselves, only the element E that the node contains. The list can be made of any type of element E. As an example of unit testing I supply you with the TestNG code. If you are using Apache NetBeans 11 you may use TestNG for unit testing. Currently I could not get JUnit to work in NetBeans. Java API LinkedList class Interfaces and Classes Seve...

Profiling Java Efficiency

Profiling without an  IDE with Java Think you need an IDE or a paid IDE to profile? Think again. I explain in this post how to do this with the basic Java language. You will find some interesting results in the very simple test case that I made up for this article. Later I will try to come back and upgrade this article to test a couple of other methods mentioned and other types of objects. How to use Java to determine the size of something. How to Get the Size of an Object in Java is an article that talks about using getObjectSize(Object objectToSize) of Instrumentation interface It seems this interface was created to work with bytecode but it can be used to get an estimated object size. In his article he states. Minimum object size is 16 bytes for modern 64-bit JDK since the object has 12...

Local Data Storage 8 – Random Access Files with Java

Random Access Files with Java Working with Java and Files Tutorial Trail Serialized XML to Files SQLite Database Random Access Files(API) (RAF) in Java are super easy to use. Use an RAF when you are doing more database-like operations vs document-like operations. With an RAF you can move to any location in the file based on the byte number.  Then read or write primitives and Strings. You can also read and write arrays of bytes. It would be easy enough to write a set of primitives associated with an object which is the object’s state, then reload and rebuild the object later. Six Examples So in this article, I will give you 6 examples of storing and reading – one, two and three dimensioned arrays of integers. The first 3 examples simply generate random numbers in arrays then wri...

Local Data Storage 10 – JDBC and H2 (formerly HypersonicSQL)

H2 Database and Java When I began using Java years ago I heard of a database that was being coded entirely using Java. This was HypersonicSQL and you can still download it at Sourceforge. The author working on it decided it needed a rewrite and he began H2 database. First download H2 installer at H2 download.  Get the stable version 1.4.195 as the h2 jar file for the last version .196 gave a class not found exception on startup looking for the driver. And the tutorial can be found here H2 Tutorial.  Next look at your install and find the H2/bin folder. Here you find the jar file needed h2-some-version.jar i.e. h2-1.4.195.jar. Most if not all databases now create the database when you connect to it. All we need to do is make the JDBC connection and create a table, add some records with SQL ...

Local Data Storage 9 – JDBC and SQLite

SQLite and Java First, you must go to download page and get the binaries for either 32 or 64 bit, placing them in the system path. If you don’t know how to do that, google adding folders to the system path. There will be an SQLite.exe file. Run it as a command to verify that it is in the path and working. But for Java coding get the jar file at Jar file. A very good resource you will need is A good tutorial and Another tutorial. Working with Java and Files Tutorial Trail Random Access Files H2 DB and Java Setup for simple example Download the SQLite jar file from BitBucket SQL downloads. Put it and the source below in the same folder. Set the classpath to include that jar file and the ‘.’ current folder or use -cp option with ‘sqlite-jdbc-3.21.0....

Local Data Storage 7 – Serializing Java Objects to XML and Deserializing XML back to Java Objects

Working with Java and Files Tutorial Trail Read Write XML Files Random Access Files Like object serialization this is very easy and straight forward. Below is two examples of psuedo code for writing and then reading. This code merely shows the steps. We didn’t have to use the buffered streams. With small files you do not have to but with larger files it will speed up reading and writing. XMLEncoder Class //psuedo code does not run import java.beans.*; import java.io.*; public class SerializeToXML { public static void main(String args[]){ // an object, composition of objects or collection of objects AnObject anObject = new AnObject(); XMLEncoder encoder=null; try{ encoder=new XMLEncoder(new BufferedOutputStream(new FileOutputStream("objects.xml"))); }catch(FileNotFoundException fnfe){...

Local Data Storage 6 – Reading and Writing XML data files using Java

XML Data Files Working with Java and Files Tutorial Trail Serialize Objects to Files Serialize XML to Files In this post I will show you how to read and write data types in simple xml data files using XML DOM. DOM means Document Object Model. We are mainly concerned here with reading and writing some data of the same types we have been working with in previous articles. So i.e. xml data files <some document> <some element> <some data name=value/> <some data name="value"/> <some data> some text </some data> </some element> </some document> XML Documents XML document can be more complex with other parts to parse. In this, we have a document root which has as children which have children which have attributes which contain the data we want ...

Local Data Storage 5 – Reading and Writing Serialized Objects to a File

Reading and Writing Objects to files or streams is fairly straight forward. Here is a working example. There are a few things to know. Use serialver.exe to create a serial id number for the class. A class must implement Serializable interface and should use @serial tag in Javadoc comments. A cryptic coded file is created which is not very editable or readable. In a later article I will talk about serializing objects to xml streams and files.

Local Data Storage 4 – Java Fixed Width Text files using row, col type storage.

This article demonstrates using a fixed field width | delimited text file similar to a spread sheet. You can store and retrieve various data types in give row,col cells. It expands columns if new data is longer than the cell. It does not add new columns or rows however and if you try to access columns or rows that are no there you get out of bounds errors. In the example I show a survey note book sheet that we use in mapping caves. A future version of this might calculate variables that would be used in cave mapping and add them to the sheet. This could also be used for mapping things above ground.

Local Data Storage 3 – Reading and Writing Property Files using Java

I show you how to read and write your own property file which contains name=value pairs. Java comes with a Properties object to do this exact thing and I will write an article about that object later. Or expand this article to include an example. This article is another example of using a delimiter and in this case it is = symbol. Later I will upgrade this to include new line as delimiter to handle long strings.

Local Data Storage 2 – Reading and Writing delimited files using Java

Here I show you how to read and write | delimited files. Each column is a field in a record. If you are writing simple applications or prototyping why use a full database? This can work just as well. I show you how to convert data types to strings and strings back to basic datatypes.

Local Data Storage 1 – Reading and Writing files using Java

Simple examples for writing and reading to and from a text file line by line. Write, Read and Append. In Java we not only use FileWriter and FileReader but also BufferedWriter and BufferedReader to speed up reading and writing in just about all cases.