Java Programming Home Page: Archive: Message #97

Date: Oct 23 2000 09:22:31 EDT
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: how do I list a file?

SW409:
Here is an example of how you might list
a file....
FYI....
 - DL

import java.util.*;
import java.awt.Frame;
import java.awt.FileDialog;
import java.io.*;

public class Futil {
    public static void listFile(String fn) {
    	FileReader fr = null;
    	BufferedReader br = null;
       try {
       	fr = new FileReader(fn);
       	br = new BufferedReader(fr);
       	String s = null;
       	while ((s =
       		br.readLine()) != null) {
       			System.out.println(s);
       	}
       }
       catch(Exception e) {
       	e.printStackTrace();
       }
    }
	public static String getReadFileName() {
		FileDialog fd = new FileDialog(
			new Frame(), "please select a file");
		fd.setVisible(true);
		fd.setVisible(false);
		return
			fd.getDirectory() +
				fd.getFile();
	}
   	public void print(String s[]) {
   		for (int i=0; i < s.length; i++)
   			System.out.println(s[i]);
   	}
   	public String [] parseIt(String is) {
   		StringTokenizer st =
   			new StringTokenizer(is);
   		int n = st.countTokens();
   		System.out.println("found n="
   			+ n + " tokens");
   		String toks[] = new String[n];
   		for (int i=0; i < n; i++) {
   			toks[i] = st.nextToken();
   		}
   		return toks;
   	}

	public static void main(String args[]) {
		Futil ta =
			new Futil();
		ta.print(
			ta.parseIt("ok buddy, parse this!"
				)
		);
		listFile(
			getReadFileName());
	}
}