Yurttas/PL/OOL/Java/F/09/02/01/00/InputFile.java
Jump to navigation
Jump to search
1import java.awt.*;
2import java.io.*;
3
4public class InputFile {
5
6 RandomAccessFile f = null;
7 boolean errflag;
8 String s = null;
9
10 public InputFile(String fname) {
11 errflag = false;
12 try {
13 //open file
14 f = new RandomAccessFile(fname, "r");
15 }
16 catch(IOException e) {
17 // print error if not found
18 System.out.println("no file found");
19 errflag = true; //and set flag
20 }
21 }
22
23 public boolean checkErr() {
24 return errflag;
25 }
26
27 public String read() {
28 //read a single field up to a comma or end of line
29 String ret = "";
30 if(s==null) //if no data in string
31 {
32 s = readLine(); //read next line
33 }
34 if(s!=null) //if there is data
35 {
36 s.trim(); //trim off blanks
37 int i = s.indexOf(","); //find next comma
38 if(i<=0)
39 {
40 ret = s.trim(); //if no commas go to end of line
41 s = null; //and null out stored string
42 }
43 else {
44 ret = s.substring(0, i).trim(); //return left of comma
45 s = s.substring(i+1); //save right of comma
46 }
47 }
48 else
49 ret = null;
50 return ret; //return string
51 }
52
53 public String readLine() {
54 //read in a line from the file
55 s = null;
56 try {
57 s = f.readLine(); //could throw error
58 }
59 catch(IOException e) {
60 errflag = true;
61 System.out.println("File read error");
62 }
63 return s;
64 }
65
66 public void close() {
67 try {
68 f.close(); //close file
69 }
70 catch(IOException e) {
71 System.out.println("File close error");
72 errflag = true;
73 }
74 }
75
76}