S6423: Java User Experience: Confessions of and old COBOL Programmer
(Code Samples)
SHARE Technical Conference, August 20, 1998
jsrDebug.java 2026 Thu Jan 15 19:48:02 CST
1998
1: /******************************************************************************
2: // jsrDebug.java:
3: * to simplify Display/Accept logic for debugging.
4: * Sample Use:
5: * import jsrDebug; ...
6: * jsrDebug debug = new jsrDebug(); ...
7: * debug.Display("Text to display");
8: * debug.Accept ("Text to display");
9: * each instance of jsrDebug will save the last
character entered from Accept.
10: * if that character is "q" (113),
lower case q, subsequent calls to that
11: * instance will neither display (System.out.println),
12: * nor accept (System.in.read()).
13: */
14:
15: import java.io.*;
16: public class jsrDebug
17: {
18: static final String copyright =
19: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
20:
21: static int maxLineLen = 80;
22: static int maxScreen = 21;
23: static int qBig = ' '; // 81 "Q"
to terminate all instances
24: int qChar = ' '; // 113 "q"
to terminate this instance
25: static int nLines = 0;
26: public jsrDebug() // null constructor
27: {
28: }
29:
30: void Display(String displayString)
31: {
32: if (qChar != 113 && qBig != 81)
33: {
34: // System.out.println("Last q="+qChar+",
Last Q="+qBig);
35: nLines += (displayString.length()+maxLineLen)/maxLineLen;
36: if (nLines > maxScreen)
37: Accept(displayString);
38: else
39: System.out.println(displayString);
40: }
41: return;
42: }
43:
44: void Accept(String displayString)
45: {
46: if (qChar != 113 && qBig != 81)
47: {
48: // System.out.println("Last q="+qChar+",
Last Q="+qBig);
49: nLines = (displayString.length()+maxLineLen)/maxLineLen;
50: System.out.println(displayString);
51: System.out.println("Waiting for
input...(Press [Enter], \"q\", or \"Q\")");
52: try {qChar = System.in.read();System.in.read();}
catch (IOException eIO){}
53: qBig = qChar; // if "Q" will
terminate all instances.
jsrDebug.java 2026 Thu Jan 15 19:48:02 CST
1998
54: // if "q" will
terminate this instance.
55: }
56: return;
57: }
58: }
59:
jsrDir.java 2690 Sat Aug 08 23:03:34 CDT
1998
1: //******************************************************************************
2: // jsrDir.java: Sorted directory listing.
3: // If args[0] not null, filename
must contain args[0]
4: //******************************************************************************
5: import java.io.*;
6: import java.util.*;
7: import jsrDebug;
8: import jsrSortString;
9: public class jsrDir
10: {
11: static final String copyright =
12: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
13: // jsrDir Class Constructor
14: //--------------------------------------------------------------------------
15: public jsrDir()
16: {
17: }
18: //--------------------------------------------------------------------------
19: public static void main(String args[])
20: {
21: jsrDebug debug = new jsrDebug();
22: debug.Display("jsrDir--"+copyright);
23: jsrSortString sortDir = new jsrSortString();
24: String thisDir = System.getProperty("user.dir");
25: debug.Display("Current User Directory:
"+thisDir);
26: File listDir = new File(".");
// get current directory
27: thisDir = listDir.getAbsolutePath();
28: listDir = new File(thisDir); //
listDir is now full path
29: thisDir = listDir.getParent(); //
thisDir is string of just DirName
30: //FilenameFilter htmFilter;
31: //htmFilter.accept(listDir, "*.htm");
32: String[] listFiles = listDir.list();
33: int lenDir = listFiles.length;
34: debug.Display("Directory "+thisDir+"
Directory entries="+lenDir);
35: try {sortDir.sort(listFiles);}
36: catch (Exception Sort) {debug.Display("Sort
Error");}
37: int numSelected = 0;
38: for (int i=0; i < lenDir; i++)
39: { if ((args.length == 0) ||
40: (listFiles[i].indexOf(args[0])
> 0 ))
41: {
42: File thisFile = new File(listFiles[i]);
43: if (thisFile.isFile())
44: {
jsrDir.java 2690 Sat Aug 08 23:03:34 CDT
1998
45: Long intCount = new Long(thisFile.length());
// integer->String
46: String strCount = intCount.toString();
47: while ((listFiles[i].length()+strCount.length())
< 25)
48: strCount = " "+strCount;
49: Date thisDate = new Date(thisFile.lastModified());
50: debug.Display("File: "+listFiles[i]+strCount+"
"+thisDate);
51: }
52: else
53: if (thisFile.isDirectory())
54: debug.Display("Dir: "+listFiles[i]);
55: else
56: debug.Display("???: "+listFiles[i]);
57: numSelected++;
58: }
59: }
60: debug.Display("Directory "+thisDir+"
Selected entries="+numSelected);
61: debug.Display("jsrDir--"+copyright);
62: }
63: }
jsrDump.java 7021 Sat Aug 08 22:56:18 CDT
1998
1: /* ****************************************************************************
2: * jsrDump.java: Application
3: * see help[] for details.
4: ******************************************************************************
5: */
6: import java.awt.*;
7: import java.io.*;
8: import java.util.*;
9: import jsrDebug;
10: //==============================================================================
11: public class jsrDump
12: {
13: static final String copyright =
14: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
15: static final String[] help =
16: {
17: "jsrDump.java: Application",
18: "dump selected file, line1 = code line,
line2=printable ASCII code.",
19: "line1: values: line2:",
20: " ^ ( 0- 31) ascii ctrl character
(0-31) add 64 to print.",
21: " . ( 32-126) ascii printable characters.",
22: " | ( 127) print \"|\".",
23: " ! (128-159) add 128 + 64 to print.",
24: " + (160-254) add 128 to print.",
25: " + ( 255) print \"|\".",
26: "For each screen, jsrDump requests input,
displays remaining bytes.",
27: "[Enter] will continue, \"q\"
or \"Q\" then [Enter] will quit dump."
28: };
29:
jsrDump.java 7021 Sat Aug 08 22:56:18 CDT
1998
30: static final int maxChar = 78;
31: static final int maxLine = 10;
32: static final String scaleLine =
33: "....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+678";
34: //--------------------------------------------------------------------------
35: public static void main(String args[])
36: {
37: jsrDump jsrDumpC = new jsrDump();
38: String inFDName, inDirName;
39: // jsrDebug debug = new jsrDebug();
40: //for (int i = 0; i < args.length; i++)
41: //{
42: // debug.Display("Args["+i+"]="+args[0]+"*");
43: //}
44: if (args.length < 1)
45: {
46: //debug.Display("Creating Dialog");
47: Frame jsrFrame = new Frame();
48: FileDialog inFileDiag;
49: inFileDiag = new FileDialog(jsrFrame,
"Select File to Dump", FileDialog.LOAD);
50: inFileDiag.show();
51: inFDName = inFileDiag.getFile();
52: inDirName = inFileDiag.getDirectory();
53: jsrFrame.dispose();
54: }
55: else
56: {
57: inFDName = args[0];
58: inDirName = "";
59: }
60: File thisFile = new File(inFDName);
61: long fileSize = thisFile.length();
62: Long intLength = new Long(fileSize); //
convert integer to String
63: String strLength = intLength.toString();
64: Date thisDate = new Date(thisFile.lastModified());
65: String jsrTitle1=inDirName +
66: inFDName+" Length="+strLength+"
Mod: "+thisDate;
67: String jsrTitle2=" "+copyright;
68: System.out.println(jsrTitle1);
69: System.out.println(jsrTitle2);
70: System.out.println(scaleLine);
71: jsrDumpC.dumpFile(inFDName, fileSize, jsrTitle1,
jsrTitle2);
72: System.exit(0);
73: }
74:
75: // jsrDump Class Constructor
76: //--------------------------------------------------------------------------
77: public jsrDump()
78: {
79: }
80:
81: // the dumpFile method 1) copies inputFile
82: void dumpFile(String inputFile, long fileSize,
String jsrTitle1, String jsrTitle2)
jsrDump.java 7021 Sat Aug 08 22:56:18 CDT
1998
83: {
84: int qChar = 0;
85: int chars = 0;
86: int lines = 0;
87: long fileChar= 0;
88: long fileRemain = fileSize;
89: // jsrDebug debug = new jsrDebug();
90: try
91: {
92: DataInputStream inFileStream =
93: new DataInputStream (new
FileInputStream ( inputFile));
94: DataInput inFile = inFileStream;
95:
96: String line1 = new String("");
97: String line2 = new String("");
98: String strChar;
99: int intVal;
100: //byte bChar = -128;
101: byte[] lineChar = {0};
102: //fileSize = 256;
103: try { while (fileChar < fileSize &&
qChar != 81 && qChar != 113)
104: {
// Q q
105: fileChar++;
106: lineChar[0] = inFile.readByte();
107: //lineChar[0] = bChar;
108: //bChar++;
109: intVal = lineChar[0];
110: strChar = new String(lineChar);
111: // debug.Accept("Reading
Byte="+fileChar+"="+intVal+
112: // "="+strChar);
113:
114: if (lineChar[0] < -96)
115: {
116: line1 += "!";
117: lineChar[0] += 192; // 128
+ 64 = 192
118: strChar = new String(lineChar);
119: line2 += strChar;
120: }
121: else if (lineChar[0] < -1)
122: {
123: line1 += "+";
124: lineChar[0] += 128;
125: strChar = new String(lineChar);
126: line2 += strChar;
127: }
128: else if (lineChar[0] == -1)
129: {
130: line1 += "+";
131: line2 += "|";
132: }
133: else if (lineChar[0] < 32)
134: {
135: line1 += "^";
jsrDump.java 7021 Sat Aug 08 22:56:18 CDT
1998
136: lineChar[0] += 64;
137: strChar = new String(lineChar);
138: line2 += strChar;
139: }
140: else if (lineChar[0] < 127)
141: {
142: line1 += ".";
143: strChar = new String(lineChar);
144: line2 += strChar;
145: }
146: else if (lineChar[0] == 127)
147: {
148: line1 += "|";
149: line2 += "|";
150: }
151: //debug.Display(line1);
152: //debug.Display(line2);
153: chars++;
154: if (chars == maxChar)
155: {
156: System.out.println(line1);
157: System.out.println(line2);
158: chars = 0;
159: line1 = "";
160: line2 = "";
161: lines++;
162: if (lines == maxLine)
163: {
164: fileRemain = fileSize -
fileChar;
165: System.out.println("jsrDump-Waiting...(Press
[Enter], \"q\", or \"Q\")"
166: +"
Remaining bytes="+fileRemain);
167: try {qChar = System.in.read();
System.in.read();} catch (IOException eIO){}
168: lines = 0;
169: System.out.println(jsrTitle1);
170: System.out.println(jsrTitle2);
171: System.out.println(scaleLine);
172:
173: }
174: }
175: }
176: System.out.println(line1);
177: System.out.println(line2);
178: System.out.println(jsrTitle1);
179: System.out.println(jsrTitle2);
180: System.out.println(scaleLine);
181: if (lines < (maxLine - 6))
182: for(int i=0; i<11; i++) System.out.println(help[i]);
183:
184: } catch (EOFException eEOF) {System.out.println("EndOfFile!");
}
185: catch ( IOException eIO ) {System.out.println("I/OError!
"); }
186: inFileStream.close(); // free up resource
prior to rename
187: } catch (FileNotFoundException eFNF) {System.out.println("FileNotFound");}
188: catch ( IOException eIO ) {System.out.println("I/O
on Output! "); }
jsrDump.java 7021 Sat Aug 08 22:56:18 CDT
1998
189:
190: return;
191: }
192:
193: }
jsrEdit.java 10036 Thu Jan 15 19:21:54 CST
1998
1: //******************************************************************************
2: // jsrEdit.java: Application
3: // jsrEdit will apply Edit fromTo strings read
from file arg[0]
4: // the fromTo are read as pairs, to delete a
string the second line is null.
5: // if to line == *delete*, then the whole
line containing from string
6: // will be deleted.
7: // if an input line ends with a "-",
the next line is read and
8: // appended to the target string (w/o the
"-").
9: // if it ends with --, then it becomes line\r\nnextline.
(i.e. "--" -> \r\n).
10: // for example: line-
11: // next. becomes linenext.
12: // line--
13: // next. becomes line\r\nnext.
14: // the input file is specified from the panel.
15: // after the edits the input file is renamed
to *.$$$
16: // for example: week03.htm becomes week03.$$$,
afile becomes afile.$$$
17: // a temporary file "jsrEdit.$$$"
is used for intermediate output.
18: //******************************************************************************
19: import java.applet.*;
20: import java.awt.*;
21: import java.io.*;
22: import jsrLoadArray; // loads fromTo array,
appending "-" and "--" lines.
23: //==========================================================================
24: // Main Class for applet jsrEdit
25: //==========================================================================
26: public class jsrEdit extends Applet
27: {
28: static final String copyright =
29: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
30: static Label inDirLabel;
31: static Label inLabel;
32: static TextField inText;
33: static Label outLabel;
34: static Label outText;
35: static Label recText;
36: static Button copyButton, exitButton;
37: static int fromToMax;
38: static String[] fromTo;
39: static int qChar = ' '; //
113 "q" to terminate input pause
40: Frame frame;
41:
42: //--------------------------------------------------------------------------
43: public static void main(String args[])
jsrEdit.java 10036 Thu Jan 15 19:21:54 CST
1998
44: {
45: jsrEdit jsrEditC = new jsrEdit();
46: System.out.println("jsrEdit--"+copyright);
47:
48: jsrEditC.frame = new Frame("jsrEdit");
49: // Must show Frame before we size it
so insets() will return valid values
50: //----------------------------------------------------------------------
51: jsrEditC.frame.show();
52: jsrEditC.frame.hide();
53: jsrEditC.frame.resize(jsrEditC.frame.insets().left
+
54: jsrEditC.frame.insets().right
+ 400,
55: jsrEditC.frame.insets().top
+
56: jsrEditC.frame.insets().bottom
+ 200);
57:
58: jsrLoadArray fromToFile = new jsrLoadArray(args[0],
100);
59: fromTo = fromToFile.getEntry();
60: fromToMax = fromTo.length;
61: System.out.println("Number fromTo
Pairs="+fromToMax+"/2"+
62: " Max array
size=100");
63:
64: jsrEditC.frame.add("Center",
jsrEditC);
65: jsrEditC.init();
66: jsrEditC.frame.show();
67: }
68:
69: // jsrEdit Class Constructor
70: //--------------------------------------------------------------------------
71: public jsrEdit()
72: {
73: // TODO: Add constructor code here
74: }
75:
76: //--------------------------------------------------------------------------
77: public void init()
78: {
79: String inDirectory, inDirLabelText;
80: inDirectory = System.getProperty("user.dir");
81: System.out.println("Current User
Directory: "+inDirectory);
82:
83: setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
84: inDirLabel = new Label("
");
85: inLabel = new Label(" Input File:");
86: inText = new TextField(40);
87: outLabel = new Label("Backup File:");
88: outText = new Label("
");
89: recText = new Label("Press Copy
Button to copy file ");
90: copyButton = new Button("Copy");
91: exitButton = new Button("Exit");
92:
93: add (inDirLabel);
94: add (inLabel);
95: add (inText);
96: add (outLabel);
jsrEdit.java 10036 Thu Jan 15 19:21:54 CST
1998
97: add (outText);
98: add (copyButton);
99: add (exitButton);
100: add (recText);
101: inText.requestFocus();
102: inDirLabelText = "Current User
Directory: "+inDirectory;
103: while (inDirLabelText.length() <
100)
104: inDirLabelText=inDirLabelText+"
";
105: inDirLabel.setText(inDirLabelText);
106:
107: }
108:
109: public boolean action(Event event, Object
arg)
110: {
111: String inputFileName;
112: String bakFileName;
113: int whereDot;
114: if (event.target instanceof Button &&
arg.equals("Copy"))
115: {
116: inputFileName = inText.getText();
117: File testFile = new File(inputFileName);
118: if (testFile.exists())
119: {
120: whereDot = inputFileName.indexOf(".");
121: if (whereDot > 0)
122: bakFileName = inputFileName.substring(0,whereDot)
+ ".$$$";
123: else
124: bakFileName = inputFileName
+ ".$$$";
125: outText.setText(bakFileName);
126: recText.setText("Lines copied:
"+
127: jsrEdit.copyFile(inputFileName,
bakFileName, "jsrEdit.$$$"));
128: exitButton.requestFocus();
129: }
130: else
131: {
132: recText.setText("File: "
+ inputFileName + " does not exist");
133: inText.requestFocus();
134: }
135: }
136: if (event.target instanceof Button &&
arg.equals("Exit"))
137: {
138: System.out.println("jsrEdit--"+copyright);
139:
140: recText.setText("Exiting...");
141: frame.dispose();
142: System.exit(0);
143: }
144: return true;
145: }
146:
147:
148:
149: // jsrEdit Paint Handler
jsrEdit.java 10036 Thu Jan 15 19:21:54 CST
1998
150: //--------------------------------------------------------------------------
151: public void paint(Graphics g)
152: {
153: g.drawString(" ", 10, 20);
154: }
155:
156: // the newString() method modifies all occurences
of "from" to "to"
157: String newString(String oldString, String
from, String to)
158: {
159: int lenFrom = from.length();
160: int lenTo = to.length();
161: String newSB = new String();
162: char endFrom = from.charAt(lenFrom-1);
163: char numChar = ' ';
164: int where = 0;
165: int last = 0;
166: int lenOld = oldString.length();
167: where = oldString.indexOf(from);
168: // System.out.println("where="+where+"
from=*"+from+"*to=*"+to+" line="+oldString);
169: // if (qChar != 113 ) // 113 == 'q', 10,
13 == Enter...
170: // {try {qChar = System.in.read();} catch
(IOException eIO){} }
171: if (to.equals("*delete*") &&
where != -1)
172: newSB = "*delete*";
173: else
174: {
175: while (where != -1)
176: {
177: //System.out.println("last: "+last+"
where: "+where+"end="+endFrom);
178: newSB = newSB+oldString.substring(last,where)+to;
179: //System.out.println(newSB);
180: last = where + lenFrom;
181: if (endFrom == '=')
182: {
183: numChar = oldString.charAt(last);
184: while (numChar >= ' ' &&
numChar != '>' && last < lenOld)
185: // skip to next white space
or '>' or end of line
186: {
187: last++;
188: numChar = oldString.charAt(last);
189: //System.out.println("numChar="+numChar);
190: }
191: }
192: where = oldString.indexOf(from,last);
193: }
194: //System.out.println("last: "+last+"
where: "+where);
195: newSB = newSB+oldString.substring(last,lenOld);
196: }
197: // System.out.println("newSB=*"+newSB+"*");
198: return newSB;
199: }
200:
201: // the copyFile method 1) copies inputFile
to tempFile (with changes)
202: // 2) renames inputFile
to bakFile
jsrEdit.java 10036 Thu Jan 15 19:21:54 CST
1998
203: // 3) renames tempFile
to inputFile
204: int copyFile(String inputFile, String bakFile,
String tempFile)
205: {
206: String outputFile = tempFile;
207: String thisDir;
208: int lines = 0;
209: try
210: {
211: DataInputStream inFileStream =
212: new DataInputStream(new
FileInputStream ( inputFile));
213: DataOutputStream outFileStream =
214: new DataOutputStream(new
FileOutputStream(outputFile));
215: DataInput inFile = inFileStream;
216: DataOutput outFile = outFileStream;
217: String line;
218: try { while ((line = inFile.readLine())
!= null)
219: {
220: for (int i=1;i<fromToMax;i+=2)
// 1 to max so ending odd line will be skipped
221: {
222: line = jsrEdit.newString(line,fromTo[i-1],fromTo[i]);
223: // line = jsrEdit.newString(line,"\\r\\n","\r\n");
224: // this is now done in loadFile...
225: // "--" at end of
line becomes \r\n + next line.
226: }
227: if (!(line.equals("*delete*")))
228: {
229: outFile.writeBytes(line);
230: outFile.writeBytes("\r\n");
231: }
232: lines++;
233: }
234: } catch (EOFException eEOF) {System.out.println("EndOfFile!:
"+ inputFile); }
235: catch ( IOException eIO ) {System.out.println("I/OError!
"); }
236: inFileStream.close(); // free up resource
prior to rename
237: outFileStream.close();
238: } catch (FileNotFoundException eFNF) {System.out.println("FileNotFound");}
239: catch ( IOException eIO ) {System.out.println("I/O
or Output! "); }
240:
241: File inFD = new File(inputFile);
242: File bakFD = new File(bakFile);
243: File tempFD = new File(tempFile);
244: System.out.println("copy " +inputFile+
" to " +tempFile+ " rename to " +bakFile);
245: if (bakFD.exists() == true)
246: System.out.println("delete "
+ bakFD+"=" + bakFD.delete());
247: System.out.println("rename "+inFD+
" to "+ bakFD+"=" + inFD.renameTo(bakFD));
248: System.out.println("rename "+tempFD+"
to " +inFD+"=" + tempFD.renameTo(inFD));
249:
250: return lines;
251: }
252: }
jsrList.java 4783 Sat Aug 08 23:23:58 CDT
1998
1: //****************************************************************
2: // jsrList.java: List (to disk) all files
in directory.
3: // filename must contain args[0]
(i.e. "java")
4: // output will be "listed"
to file args[1].
5: //****************************************************************
6: import java.io.*;
7: import java.util.*;
8: import jsrDebug;
9: import jsrSortString;
10: public class jsrList
11: {
12: static final String copyright =
13: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
14: // jsrList Class Constructor
15: //------------------------------------------------------------
16: public jsrList()
17: {
18: }
19: //------------------------------------------------------------
20: public static void main(String args[])
21: { jsrList jsrListC = new jsrList();
22: jsrDebug debug = new jsrDebug();
23: debug.Display("jsrList--"+copyright);
24: if (args.length < 2)
25: { debug.Display("jsrList--must contain
two arguments:");
26: debug.Display("---------arg 1:
file suffix (i.e., java)");
27: debug.Display("---------arg 2:
target file for output.");
28: return;
29: }
30: jsrSortString sortDir = new jsrSortString();
31: String thisDir = System.getProperty("user.dir");
32: debug.Display("Current User Directory:
"+thisDir);
33: File listDir = new File(".");
// get current directory
34: thisDir = listDir.getAbsolutePath();
35: listDir = new File(thisDir); // listDir
is now full path
36: thisDir = listDir.getParent(); // thisDir
is just DirName
37: String[] listFiles = listDir.list();
38: int lenDir = listFiles.length;
39: try {sortDir.sort(listFiles);}
40: catch (Exception Sort) {debug.Display("Sort
Error");}
41: String outputFile = args[1];
42: System.out.println("jsrList----Output
File: "+outputFile);
43: try
44: { DataOutputStream outFileStream =
45: new DataOutputStream(new FileOutputStream(outputFile));
46: DataOutput outFile = outFileStream;
47: // open stream separately so can close
Stream
48: int numSelected = 0;
49: for (int i=0; i < lenDir; i++)
50: { if (listFiles[i].indexOf(args[0])
> 0 )
51: {
52: File thisFile = new File(listFiles[i]);
53: if (thisFile.isFile())
jsrList.java 4783 Sat Aug 08 23:23:58 CDT
1998
54: {
55: Long intCount = new Long(thisFile.length());
56: // int->String
57: String strCount = intCount.toString();
58: while ((listFiles[i].length()+strCount.length())
< 25)
59: strCount = " "+strCount;
60: Date thisDate = new Date(thisFile.lastModified());
61: String jsrTitle = listFiles[i]+strCount+"
"+thisDate;
62: debug.Display("jsrList-----Input
File: "+jsrTitle);
63: numSelected++;
64: debug.Display("jsrList---Lines
copied: "+
65: jsrListC.copyFile(listFiles[i],
outFile, jsrTitle));
66: }
67: }
68: }
69: outFileStream.close();
70: debug.Display("Directory "+thisDir+"
Selected entries="+
71: numSelected);
72: debug.Display("jsrList--"+copyright);
73: }catch (FileNotFoundException eFNF)
74: {System.out.println("FileNotFound");}
75: catch ( IOException eIO )
76: {System.out.println("I/O or
Output! ");}
77: }
78:
79: int copyFile (String argv, DataOutput outFile,
String jsrTitle)
80: {
81: String inputFile = argv;
82: int lines = 0;
83: try
84: {
85: DataInputStream inFileStream =
86: new DataInputStream (new FileInputStream
( inputFile));
87: DataInput inFile = inFileStream;
88: String line;
89: outFile.writeBytes(jsrTitle+"\r\n");
90: try { while ((line = inFile.readLine())
!= null)
91: { lines++;
92: StringBuffer modLine = new StringBuffer(line);
93: Integer intLines = new Integer(lines);
94: String strLines = intLines.toString();
95: while (strLines.length() <
3) strLines = " "+strLines;
96: outFile.writeBytes(strLines+":
");
97: outFile.writeBytes(modLine.toString());
98: outFile.writeBytes("\r\n");
99: }
100: inFileStream.close(); // free
up resource
101: } catch (EOFException eEOF)
102: {System.out.println("EndOfFile!");
}
103: catch ( IOException eIO )
104: {System.out.println("I/OError!
"); }
105: } catch (FileNotFoundException eFNF)
106: {System.out.println("FileNotFound");}
jsrList.java 4783 Sat Aug 08 23:23:58 CDT
1998
107: catch ( IOException eIO )
108: {System.out.println("I/O or
Output! "); }
109: return lines;
110: }
111: }
jsrLoadArray.java 3238 Thu Jan 15 20:06:38 CST
1998
1: /* jsrLoadArray
2: * jsrLoadArray loadIt = new jsrLoadArray(String
inputFile, int arrMax);
3: * opens file inputFile, and allocates
a temporary array[arrMax]).
4: *
5: * loadIt.getEntry() copies inputFile to the
temporary array
6: * if an input line ends with a "-",
the next line is read and
7: * appended to the target string (w/o the
"-").
8: * if it ends with --, then "--" ->
"\r\n".
9: * for example: line-
10: * next. becomes linenext.
11: * line--
12: * next. becomes line\r\nnext.
13: * if arrMax is exceeded, extra elements are
discarded, with error messages.
14: *
15: * after input is read, an array of exact #
of array elements
16: * is allocated and returned.
17: */
18: import java.io.*;
19:
20: public class jsrLoadArray
21: {
22: static final String copyright =
23: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
24:
25: DataInputStream inFileStream;
26: DataInput inFile;
27: String[] chgLine;
28: int arrayMax;
29: jsrLoadArray(String inputFile, int arrMax)
// constructor opens file.
30: {
31: arrayMax = arrMax;
32: chgLine = new String[arrayMax];
33: try
34: {
35: inFileStream = new DataInputStream (new
FileInputStream ( inputFile));
36: inFile = inFileStream;
37: } catch (FileNotFoundException eFNF) {System.out.println("FileNotFound:"+
inputFile);}
38: catch ( IOException eIO ) {System.out.println("I/O
Error! "+ inputFile); }
39:
40: }
41: String[] getEntry()
42: {
43: int lines = 0;
44: String line;
45: int lenLine;
jsrLoadArray.java 3238 Thu Jan 15 20:06:38 CST
1998
46: try
47: { while ((line = inFile.readLine()) != null)
48: {
49: if ( lines == 0)
50: {
51: chgLine[lines] = line;
52: // System.out.println("chgLine="+lines+",
"+line);
53: lines++;
54: }
55: else
56: {
57: lenLine = chgLine[lines-1].length();
58: if (lenLine > 1 && chgLine[lines-1].charAt(lenLine-1)
== '-')
59: {
60: if (chgLine[lines-1].charAt(lenLine-2)
== '-') // line ends with "--"
61: chgLine[lines-1] = chgLine[lines-1].substring(0,lenLine-2)+"\r\n"+line;
62: else
63: chgLine[lines-1] = chgLine[lines-1].substring(0,lenLine-1)+line;
64: // System.out.println("Append="+chgLine[lines-1]);
65: }
66: else
67: { if (lines < arrayMax)
68: {
69: chgLine[lines] = line;
70: // System.out.println("chgLine="+lines+",
"+line);
71: lines++;
72: }
73: else
74: {
75: System.out.println("Max
input="+arrayMax+" discarded input="+line);
76: }
77: }
78: }
79: }
80: } catch (EOFException eEOF) {System.out.println("EndOfFile!");
}
81: catch ( IOException eIO ) {System.out.println("I/OError!
"); }
82: try {inFileStream.close();} // free up
resource
83: catch ( IOException eIO ) {System.out.println("I/OError!
"); }
84: if (lines > arrayMax) lines = arrayMax;
85: String[] newArray = new String[lines];
86: for (int i=0; i < lines; i++) newArray[i]
= chgLine[i];
87: return newArray;
88: }
89: }
jsrSortString.java 2151 Thu Jan 15 21:51:24 CST
1998
1: /*
2: * jsrSortString
3: */
4: public class jsrSortString
5: {
6: static final String copyright =
jsrSortString.java 2151 Thu Jan 15 21:51:24 CST
1998
7: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
8: /* This is a generic version of C.A.R Hoare's
Quick Sort
9: * algorithm. This will handle arrays that
are already
10: * sorted, and arrays with duplicate keys.
11: *
12: * @param a a String array
13: * @param lo0 left boundary of array
partition
14: * @param hi0 right boundary of array
partition
15: */
16: void jsrQuickSort(String a[], int lo0, int
hi0) throws Exception
17: {
18: int lo = lo0;
19: int hi = hi0;
20: String mid;
21:
22: if ( hi0 > lo0)
23: {
24:
25: // Arbitrarily establishing partition
element at the midpoint.
26: mid = a[ ( lo0 + hi0 ) / 2 ];
27:
28: // loop through the array until indices
cross
29: while( lo <= hi )
30: {
31: // find the first element that is
greater than or equal to
32: // the partition element starting
from the left Index.
33: while( ( lo < hi0 ) && ( a[lo].compareTo(mid)
< 0 ))
34: ++lo;
35:
36: // find an element that is smaller
than or equal to
37: // the partition element starting
from the right Index.
38: while( ( hi > lo0 ) && ( a[hi].compareTo(mid)
> 0 ))
39: --hi;
40:
41: // if the indexes have not crossed,
swap
42: if( lo <= hi )
43: {
44: swap(a, lo, hi);
45: ++lo;
46: --hi;
47: }
48: }
49:
50: // If the right index has not reached
the left side of array
51: // must now sort the left partition.
52: if( lo0 < hi )
53: jsrQuickSort( a, lo0, hi );
54:
55: /* If the left index has not reached
the right side of array
56: * must now sort the right partition.
57: */
58: if( lo < hi0 )
59: jsrQuickSort( a, lo, hi0 );
jsrSortString.java 2151 Thu Jan 15 21:51:24 CST
1998
60:
61: }
62: }
63:
64: private void swap(String a[], int i, int
j)
65: {
66: String T;
67: T = a[i];
68: a[i] = a[j];
69: a[j] = T;
70:
71: }
72:
73: public void sort(String a[]) throws Exception
74: {
75: jsrQuickSort(a, 0, a.length - 1);
76: }
77: }
logHTM.java 5924 Sat Jul 04 11:36:48 CDT
1998
1: /* logHTM inputFile outputFile1 outputFile2
2: * read inputFile
3: * write outputFile1
if ".htm" is in line
4: write outputFile2
with counts by page
5: */
6: import java.io.*;
7: import jsrSortString;
8: public class logHTM
9: {
10: static final String copyright =
11: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
12: static final int countMax = 300; // max entries
in count Array
13:
14: public static void main(String argv[])
15: {
16: System.out.println("logHTM--"+copyright);
17:
18: logHTM fixLine = new logHTM();
19: String inputFile = argv[0];
20: System.out.println(" Input File: "+inputFile);
21: String outputFile1 = argv[1];
22: System.out.println("Output File: "+outputFile1);
23: String outputFile2 = argv[2];
24: System.out.println("Output File: "+outputFile2);
25: String matchIt = ".htm";
26: System.out.println("Selecting: "+matchIt+"
log records.");
27: int linesin = 0;
28: int lines100 = 0;
29: int lines1 = 0;
30: String htmName[] = new String[countMax];
31: htmName[0] = "Total Hits...";
32: int htmCount[] = new int[countMax];
logHTM.java 5924 Sat Jul 04 11:36:48 CDT
1998
33: htmCount[0] = 0;
34: int htmIndex = 1;
35: int i = 1;
36:
37: try
38: {
39: DataInput inFile = new DataInputStream
(new FileInputStream ( inputFile));
40: DataOutput outFile1 = new DataOutputStream(new
FileOutputStream(outputFile1));
41: DataOutput outFile2 = new DataOutputStream(new
FileOutputStream(outputFile2));
42: String line;
43: String whoLooked;
44: String dateLooked;
45: String htmLooked;
46: String resultHTM;
47: String outLine;
48: int index1, lenline1;
49: try { while ((line = inFile.readLine())
!= null)
50: {
51: linesin++;
52: lines100++;
53: if (lines100 == 100)
54: {
55: lines100 = 0;
56: System.out.print("Reading
line: "+linesin+"\r");
57: System.out.flush();
58: }
59:
60: if (line.indexOf(matchIt) >=
0)
61: {
62: index1 = line.indexOf("
- - [");
63: lenline1 = line.length();
// 1998/07/04 add length check
64: if (index1 < 0 || (index1+35
> lenline1) )
65: System.out.println("Line
skipped="+line);
66: else
67: {
68: whoLooked = line.substring(0,index1);
69: dateLooked = line.substring(index1+6,index1+26);
70: htmLooked = line.substring(index1+34);
71: resultHTM = "
";
72: index1 = htmLooked.indexOf("
"); // strip GET or HEAD...
73: if (index1 < 0) htmLooked
= " ";
74: else
75: {
76: htmLooked = htmLooked.substring(index1+1);
77: index1 = htmLooked.indexOf("
"); // find HTML tag
78: if (index1 >= 0)
79: {
80: resultHTM = htmLooked.substring(index1+1);
81: htmLooked = htmLooked.substring(0,index1);
82: index1 = resultHTM.indexOf("
");
83: resultHTM = resultHTM.substring(index1+1);
84: }
85: }
logHTM.java 5924 Sat Jul 04 11:36:48 CDT
1998
86: while (htmLooked.length()
< 25) htmLooked=htmLooked+" ";
87: outLine = dateLooked+"+"+htmLooked+resultHTM+"<"+whoLooked+">";
88: outFile1.writeBytes(outLine);
89: outFile1.writeBytes("\r\n");
90: lines1++;
91: htmCount[0]++;
92: for (i=0; i < htmIndex;
i++)
93: {
94: // System.err.println("i="+i+"htmIndex="+htmIndex+
95: // htmLooked+"=="+htmName[i]+htmCount[i]);
96: if (htmLooked.equals(htmName[i]))
97: {
98: htmCount[i]++;
99: break;
100: }
101: }
102: // System.err.println("i="+i+"htmIndex="+htmIndex+
103: // htmLooked);
104: // System.in.read();
105: if (i == htmIndex) //
not in array
106: {
107: htmName[i] = htmLooked;
108: htmCount[i] = 1;
109: htmIndex++;
110: }
111: }
112: }
113: }
114: } catch (EOFException eEOF)
{System.out.println("EndOfFile!");
}
115: catch ( IOException eIO )
{System.out.println("I/OError!
"); }
116: String[] countArray = new String[htmIndex];
117:
118: for (i = 0; i < htmIndex; i++)
119: {
120: Integer intCount = new Integer(htmCount[i]);
// convert integer to String
121: String strCount = intCount.toString();
122: while (strCount.length() < 10)
strCount = " "+strCount;
// pad to 10 bytes
123: countArray[i] = strCount +"
accesses to: "+htmName[i];
124: }
125: jsrSortString sortIt = new jsrSortString();
126: try {sortIt.sort(countArray);}
127: catch (Exception Sort) {System.out.println("Sort
Error");}
128:
129: for (i=htmIndex-1; i >= 0; i--)
130: {
131: outFile2.writeBytes(countArray[i]);
132: outFile2.writeBytes("\r\n");
133: }
134:
logHTM.java 5924 Sat Jul 04 11:36:48 CDT
1998
135:
136: } catch (FileNotFoundException eFNF)
{System.out.println("FileNotFound");}
137: catch ( IOException eIO )
{System.out.println("I/O or
Output! "); }
138: System.out.println("Lines with \""+matchIt+"\"
on file "
+outputFile1+":
"+lines1);
139: System.out.println("logHTM--"+copyright);
140: }
141: }
logSplit.java 2714 Thu Jan 15 22:53:56 CST
1998
1: /* logSplit inputFile outputFile1 outputFile2
matchString
2: * read inputFile
3: * write outputFile1
if matchString is in line
4: * write outputFile2
if ... not in line
5: */
6: import java.io.*;
7: public class logSplit
8: {
9: static final String copyright =
10: "Copyright 1998, JSR Systems. See: www.jsrsys.com/copyright.";
11:
12: public static void main(String argv[])
13: {
14: System.out.println("logSplit--"+copyright);
15:
16: logSplit fixLine = new logSplit();
17: String inputFile = argv[0];
18: System.out.println("Input File: *"+inputFile+"*");
19: String outputFile1 = argv[1];
20: System.out.println("Output File1: "+outputFile1);
21: String outputFile2 = argv[2];
22: System.out.println("Output File2: "+outputFile2);
23: String matchIt = argv[3];
24: if (inputFile.equals("error.log"))
matchIt = " "+
matchIt.substring(0,3) +" ";
25: // change matchIt string from Dec/1997
to " Dec ".
26: System.out.println("Split String: *"+matchIt+"*");
27: int linesin = 0;
28: int lines100 = 0;
29: int lines1 = 0;
30: int lines2 = 0;
31: try
32: {
33: DataInput inFile = new DataInputStream
(new FileInputStream ( inputFile));
34: DataOutput outFile1 = new DataOutputStream
(new FileOutputStream(outputFile1));
35: DataOutput outFile2 = new DataOutputStream
(new FileOutputStream(outputFile2));
36: String line;
logSplit.java 2714 Thu Jan 15 22:53:56 CST
1998
37: try { while ((line = inFile.readLine())
!= null)
38: {
39: linesin++;
40: lines100++;
41: if (lines100 == 100)
42: {
43: lines100 = 0;
44: System.out.print("Reading
line: "+linesin+"\r");
45: System.out.flush();
46: }
47:
48: if (line.indexOf(matchIt) <
0)
49: {
50: outFile2.writeBytes(line);
51: outFile2.writeBytes("\r\n");
52: lines2++;
53: }
54: else
55: {
56: outFile1.writeBytes(line);
57: outFile1.writeBytes("\r\n");
58: lines1++;
59: }
60: }
61: } catch (EOFException eEOF) {System.out.println("EndOfFile!");
}
62: catch ( IOException eIO ) {System.out.println("I/OError!
"); }
63: } catch (FileNotFoundException eFNF)
{System.out.println("FileNotFound");}
64: catch ( IOException eIO ) {System.out.println("I/O
or Output! "); }
65: System.out.println("Lines with \""+matchIt+"\"
on file "
+outputFile1+":
"+lines1);
66: System.out.println("Lines w/o \""+matchIt+"\"
on file "
+outputFile2+":
"+lines2);
67: System.out.println("logSplit--"+copyright);
68: }
69: }
End of Appendix Java User Experience: Confessions
of an Old COBOL Programmer
Return to JSR Systems Services.
Return to JSR Systems' home page.
Copyright © 1998, JSR Systems, All rights reserved.