import java.util.*; import java.awt.*; import java.io.*; class WildFilter implements FilenameFilter { private String suffix = null; WildFilter(String _suffix) { suffix = _suffix; } public boolean accept(File f, String fn) { return fn.endsWith(suffix); } } public class TrivialApplication { double sum =0; String s = "T,h#is i,s my n#ew str#ing!! 1 #2 3"; StringTokenizer st = new StringTokenizer(s,","); public void init() { //while (st.hasMoreTokens()) //System.out.println( st.nextToken()); writeFile(); } public void print(String s[]) { if (s==null) { System.out.println("Null in print!!"); } for (int i=0; i < s.length; i++) System.out.println(s[i]); } public String [] ls() { File f = getFile("select a file"); return f.list(new WildFilter("txt")); } public static void main(String args[]) { TrivialApplication ta = new TrivialApplication(); ta.init(); } public void addNumber(double d ) { sum += d; System.out.println(d+" sum is"+ sum); } public void processFile(StreamTokenizer st) throws IOException { int next = 0; while ((next = st.nextToken())!= st.TT_EOF) switch (next) { case st.TT_WORD: break; case st.TT_NUMBER: addNumber(st.nval); } } public void writeFile(PrintWriter pw) { for (int i=0; i < 100; i++) pw.println("Happy Thanksgiving!!"+i); } public void writeFile() { String fn = getWriteFileName("Enter a file"); try { FileOutputStream os = new FileOutputStream(fn); PrintWriter pw = new PrintWriter(os); writeFile(pw); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public void processFile(String fn) { try { FileInputStream fis = new FileInputStream(fn); StreamTokenizer st = new StreamTokenizer(fis); processFile(st); fis.close(); } catch(IOException e) { e.printStackTrace(); } } public File getFile(String prompt) { String fn = getDirectoryName(prompt); File f = new File(fn); //System.out.println("File="+f); return f; } public String getFileName(String prompt) { FileDialog fd = new FileDialog(new Frame(), prompt); fd.setVisible(true); fd.setVisible(false); return fd.getDirectory()+fd.getFile(); } public String getWriteFileName(String prompt) { FileDialog fd = new FileDialog(new Frame(), prompt,FileDialog.SAVE); fd.setVisible(true); fd.setVisible(false); return fd.getDirectory()+fd.getFile(); } public String getDirectoryName(String prompt) { FileDialog fd = new FileDialog(new Frame(), prompt); fd.setVisible(true); fd.setVisible(false); return fd.getDirectory(); } }
Kahindu