Yurttas/PL/OOL/Java/F/06/02/00/TextIO.java
Jump to navigation
Jump to search
1/**
2 * Copyright(C) 1998
3 * All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for EDUCATIONAL purposes
7 * and without fee is hereby granted provided that this
8 * copyright notice appears in all copies.
9 *
10 * @date : January 1, 1998.
11 * @author : Salih Yurttas.
12 */
13
14
15import java.io.*;
16
17public class TextIO {
18
19 public static int getList(final int[] list)
20 throws IOException
21 {
22 DataInputStream dIS = new DataInputStream(System.in);
23 Reader kR = new BufferedReader(new InputStreamReader(dIS));
24 StreamTokenizer kTokens = new StreamTokenizer(kR);
25
26 System.out.print("inFileName : ");
27 System.out.flush();
28 kTokens.nextToken();
29 String inFileName = kTokens.sval;
30
31 FileInputStream fIS = new FileInputStream(inFileName);
32 Reader fR = new BufferedReader(new InputStreamReader(fIS));
33 StreamTokenizer fTokens = new StreamTokenizer(fR);
34
35 int count=0;
36 while(fTokens.nextToken()!=fTokens.TT_EOF)
37 list[count++] = (int) fTokens.nval;
38
39 return count;
40 }
41
42 public static PrintWriter fileO()
43 throws IOException
44 {
45 DataInputStream dIS = new DataInputStream(System.in);
46 Reader kR = new BufferedReader(new InputStreamReader(dIS));
47 StreamTokenizer kTokens = new StreamTokenizer(kR);
48
49 System.out.print("outFileName : ");
50 System.out.flush();
51 kTokens.nextToken();
52 String outFileName = kTokens.sval;
53
54 FileOutputStream fOS = new FileOutputStream(outFileName);
55 PrintWriter pW = new PrintWriter(fOS);
56
57 return (pW);
58 }
59
60 public static void putList(final int[] list,
61 final int n)
62 throws IOException
63 {
64 PrintWriter pW = fileO();
65
66 for(int i=0; i<n; i++)
67 pW.println(list[i]);
68
69 pW.close();
70 }
71
72 public static char getChoice()
73 throws IOException
74 {
75 DataInputStream dIS = new DataInputStream(System.in);
76 Reader kR = new BufferedReader(new InputStreamReader(dIS));
77 StreamTokenizer kTokens = new StreamTokenizer(kR);
78
79 System.out.print("choiceCharacter : ");
80 System.out.flush();
81 char choiceCharacter = (char) dIS.read();
82
83 return choiceCharacter;
84 }
85
86 public static int getKey()
87 throws IOException
88 {
89 DataInputStream dIS = new DataInputStream(System.in);
90 Reader kR = new BufferedReader(new InputStreamReader(dIS));
91 StreamTokenizer kTokens = new StreamTokenizer(kR);
92
93 System.out.print("key : ");
94 System.out.flush();
95 kTokens.nextToken();
96 int key = (int) kTokens.nval;
97
98 return key;
99 }
100
101 public static void putKey(final int key,
102 final boolean found)
103 throws IOException
104 {
105 PrintWriter pW = fileO();
106
107 if(found)
108 pW.println("key "+
109 key+
110 " was present in the givenList.");
111 else
112 pW.println("key "+
113 key+
114 " was not present in the givenList.");
115
116 pW.close();
117 }
118
119}