1 import fi.jyu.mit.ohj2.*;
2 import java.io.*;
3
4
9 public class LueTuote {
10
11 public static boolean tulosta_tuotteet() {
12 String srivi,pala;
13 String nimike; double hinta; int kpl;
14
15 BufferedReader fi = Tiedosto.avaa_lukemista_varten("tuotteet.dat");
16 if ( fi == null ) return false;
17
18 System.out.println(); System.out.println(); System.out.println();
19 System.out.println("-------------------------------------------");
20
21 try {
22 while ( ( srivi = fi.readLine() ) != null ) {
23 StringBuffer rivi = new StringBuffer(srivi);
24 try {
25 nimike = Mjonot.erotaEx(rivi,'|',"");
26 hinta = Mjonot.erotaEx(rivi,'|',0.0);
27 kpl = Mjonot.erotaEx(rivi,'|',0);
28 } catch (NumberFormatException ex) {
29 System.out.println("Virhe: " + ex.getMessage());
30 continue;
31 }
32 System.out.println(Mjonot.fmt(nimike,-20) +" " + Mjonot.fmt(hinta,7,0) +
33 Mjonot.fmt(kpl,4));
34 }
35 } catch (IOException ex) {
36 System.out.println("Vikaa tiedostoa luettaessa");
37 } finally {
38 try {
39 fi.close();
40 } catch (IOException ex) {
41 System.out.println("Ongelmia tiedoston sulkemisessa");
42 }
43 }
44
45 System.out.println("-------------------------------------------");
46 System.out.println(); System.out.println(); System.out.println();
47
48 return true;
49 }
50
51 public static void main(String[] args) {
52 if ( !tulosta_tuotteet() ) System.out.println("Tuotteita ei saada luetuksi");
53 }
54 }
55