I am trying to create a small library to read grib data from a flight simulator using Java.
Created a code to read a binary file and convert it into a string.
public class MainClass
{
final static String FILE_NAME = "PATH\\global_winds.grib";
public static void main(String... aArgs) throws IOException
{
MainClass binary = new MainClass();
byte[] bytes = binary.readFile(FILE_NAME);
String myString = asciiBytesToString(bytes);
System.out.println(myString);
}
byte[] readFile(String aFileName) throws IOException
{
Path path = Paths.get(aFileName);
return Files.readAllBytes(path);
}
public static String asciiBytesToString( byte[] bytes )
{
if ( (bytes == null) || (bytes.length == 0 ) )
{
return "";
}
char[] result = new char[bytes.length];
for ( int i = 0; i < bytes.length; i++ )
{
result[i] = (char)bytes[i];
}
return new String( result );
}
}
The file is read and returns gibberish. Is anyone familiar with the grib format? I thought it is using ASCII...
I do know about third-party libraries like NetCDF. The grib file I am trying to process includes limited data and I really wanted to make a small API for reading this file.
Thank you!