// TiedostoNumeroiksi.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-04-12 Tiedosto luotu. // 2006-04-12 Viimeisin muutos. // The best way to execute this program is to run it in a // command prompt window where command line parameters can // be given. It may be difficult, and even impossible, to // supply the command line parameters if this program is // executed with a modern development tool such as JCreator // or Eclipse. // In the javafilesextra folder you can find a program // named TiedostoNumeroiksiAlternative.java which works like this // program but it asks the file name from the user. import java.io.* ; class TiedostoNumeroiksi { public static void main( String[] komentoriviparametrit ) { if ( komentoriviparametrit.length == 1 ) { try { FileInputStream luettava_tiedosto = new FileInputStream( komentoriviparametrit[ 0 ] ) ; byte[] tavut_tiedostosta = new byte[ 16 ] ; int luettujen_tavujen_maara ; while (( luettujen_tavujen_maara = luettava_tiedosto.read( tavut_tiedostosta )) > 0 ) { StringBuilder rivi_tavuja = new StringBuilder() ; StringBuilder tavut_merkkeina = new StringBuilder () ; for ( int tavun_indeksi = 0 ; tavun_indeksi < luettujen_tavujen_maara ; tavun_indeksi ++ ) { rivi_tavuja.append( String.format( " %02X", tavut_tiedostosta[ tavun_indeksi ] ) ) ; char tavu_merkkina = (char) tavut_tiedostosta[ tavun_indeksi ] ; if ( tavu_merkkina >= ' ' ) { tavut_merkkeina.append( tavu_merkkina ) ; } else { tavut_merkkeina.append( ' ' ) ; } } System.out.printf( "\n%-48s %s", rivi_tavuja, tavut_merkkeina ) ; } luettava_tiedosto.close() ; } catch ( FileNotFoundException vangittu_tiedostoa_ei_loydy_poikkeus ) { System.out.print( "\n Cannot open file " + komentoriviparametrit[ 0 ] ) ; } catch ( IOException vangittu_io_poikkeus ) { System.out.print( "\n Error while processing file " + komentoriviparametrit[ 0 ] ) ; } } else { System.out.print( "\n You have to command this program as: \n" + "\n java TiedostoNumeroiksi file.ext \n") ; } } } /* NOTES: This program uses Java 2 Standard Edition, Version 1.5 features. Kaannos with older Java compilers is thus not possible. */