package futils;
import java.util.*;
import java.awt.*;
import java.io.*;
public final class Futil {
/**
* Don't let anyone instantiate this class.
*/
private Futil() {}
private static final Frame
frame = new Frame();
public static void listFile() {
File f = getReadFile(
"select a file");
BufferedReader br =
getBufferedReader(f);
listFile(br);
}
public static void listFile(
BufferedReader br) {
String line = null;
try {
while ( (line =
br.readLine()) != null) {
System.out.println(line);
}
}
catch(IOException e) {
}
}
public static void copyFile() {
File f = getReadFile(
"select an input file");
BufferedWriter bw =
getBufferedWriter(
"enter an output file");
BufferedReader br =
getBufferedReader(f);
String line = null;
try {
while ( (line =
br.readLine()) != null) {
println(bw, line);
}
}
catch(IOException e) {
}
close(bw);
}
public static int addTokens(String l){
System.out.println(l);
int sum = 0;
StringTokenizer st
= new StringTokenizer(
l,", \t\r\f\n\"\\;");
int tc = st.countTokens();
System.out.println("tc="+tc);
for (int i=0; i < tc; i++) {
String s= st.nextToken();
int j = 0;
try {
j = Integer.parseInt(s);
}
catch(NumberFormatException e){
}
System.out.println(j);
sum = sum + j;
}
return sum;
}
public static int processFile() {
File f = getReadFile(
"select a file");
BufferedReader br =
getBufferedReader(f);
String line = null;
int k = 0;
try {
while ( (line =
br.readLine()) != null) {
k = k + addTokens(line);
}
}
catch(IOException e) {
}
return k;
}
public static BufferedWriter
getBufferedWriter(String prompt) {
File f = getWriteFile(prompt);
return getBufferedWriter(f);
}
public static BufferedWriter
getBufferedWriter(File f) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(
new FileWriter(f));
}
catch(IOException e) {
}
return bw;
}
public static BufferedReader
getBufferedReader(File f) {
try {
return new BufferedReader(
new FileReader(
f));
}
catch(IOException e) {
e.printStackTrace();
}
return null;
}
public static void close(
BufferedWriter bw) {
try {
bw.close();
}
catch(IOException e) {
}
}
public static void println(
BufferedWriter bw, Object o){
try{
bw.write(o+"\n");
}
catch(IOException e) {
}
}
public static
void main(String args[]) {
copyFile();
//System.out.println(
// getWriteFile("gimme a file"));
//listFile();
System.out.println("done!");
}
public static File getReadFile(String prompt) {
FileDialog fd = new FileDialog(
frame,
prompt);
fd.setVisible(true);
return new File(fd.getDirectory()+fd.getFile());
}
public static FileOutputStream getFileOutputStream() {
try {
return
new FileOutputStream(getWriteFile());
}
catch (Exception e) {
System.out.println(
"Er: FileOutputStream in Futil.java");}
return null;
}
public static File getWriteFile() {
return getWriteFile(
"Select a file for output");
}
public static File getWriteFile(
String prompt) {
FileDialog fd =
new FileDialog(
new Frame(),
prompt,
FileDialog.SAVE);
fd.setVisible(true);
return new File(
fd.getDirectory()
+ fd.getFile());
}
public static String getWriteDirectoryName() {
FileDialog dialog = new FileDialog(
new Frame(),
"Enter file name",
FileDialog.SAVE);
dialog.show();
String fs = dialog.getDirectory();
System.out.println("Opening file: "+fs);
dialog.dispose();
return FilterFileNameBug(fs);
}
// Some versions of windows will
// create a .* suffix on a file name
// The following code will strip it:
public static String FilterFileNameBug(String fname) {
if (fname.endsWith(".*.*")) {
fname=fname.substring(0, fname.length() - 4);
}
return fname;
}
public static File getDirFile() {
return new File(getDirName());
}
static public String getDirName() {
FileDialog fd =
new FileDialog(
new Frame(),
"select file");
fd.show();
String dirName = fd.getDirectory();
fd.dispose();
return dirName;
}
public static File getReadFile() {
return getReadFile("Enter an input file");
}
public static FileReader getFileReader() {
return getFileReader(getReadFile());
}
public static FileReader getFileReader(File file) {
FileReader fr = null;
try
{fr = new FileReader(file);}
catch (IOException e)
{System.out.println("futil:Could not open file");}
return fr;
}
// Open the file, return -1 if file cannot be opened
// otherwise return the size in bytes
public static int available(File file) {
FileInputStream fis = null;
int sizeInBytes = -1;
try {
fis = new FileInputStream(file);
sizeInBytes = fis.available();
fis.close();
}
catch (IOException e)
{System.out.println("Futil:Could not open file");}
return sizeInBytes;
}
public static void closeOutputStream(OutputStream os) {
try {
os.flush();
os.close();
} // end try
catch (IOException exe)
{System.out.println(
"futil: could not close output stream");}
}
public static void closeFileReader(FileReader fr) {
try {fr.close();} // end try
catch (IOException exe)
{System.out.println("could not close FileReader");}
}
public static void lsWordPrintMerge() {
String wild = "pict";
String[] files = Ls.getWildNames(wild);
System.out.println(files.length + " file(s):");
for (int i=0; i < files.length; i++)
System.out.println("\t" + files[i]);
}
static public void filterHtmls() {
String[] files = Ls.getWildNames(".html");
File input_dir = getDirFile();
System.out.println(files.length + " file(s) to process");
File output_dir = new File(input_dir.getParent(),"out");
if (output_dir.exists())
{System.out.println("Output dir exists:"+ output_dir);}
else {output_dir.mkdir();}
for (int i=0; i < files.length; i++)
writeFilteredHrefFile(
input_dir+files[i],
output_dir+
"/"+
files[i]);
}
public static void writeFilteredHrefFile(
String inputName,
String outputName) {
System.out.println(
"Filtering:\t" +
inputName +
"\t>\t"+
outputName);
try {
FileReader is = new FileReader(inputName);
StreamTokenizer st =
new StreamTokenizer(is);
FileOutputStream fos = new FileOutputStream(outputName);
PrintWriter output = new PrintWriter(fos);
int i;
int next = 0;
st.resetSyntax();
st.wordChars(0,255);
st.quoteChar('"');
while ((next = st.nextToken()) != st.TT_EOF) {
switch (next) {
case '"':
output.print('"');
for (i=0;i<st.sval.length();i++)
if (st.sval.charAt(i) == ' ')
output.print("%20");
else
output.print(st.sval.charAt(i));
output.print('"');
break;
case StreamTokenizer.TT_WORD:
output.print(st.sval+" ");
break;
case StreamTokenizer.TT_NUMBER:
output.print(st.nval+" ");
break;
case StreamTokenizer.TT_EOL:
output.println();
break;
} // end switch
} // end while
is.close();
fos.close();
} // end try
catch (Exception exe)
{System.out.println("writeFilteredHrefFile:er!");}
}
public static void writeFilteredJavaFile( ) {
try {
FileReader is = getFileReader();
StreamTokenizer tokens = new StreamTokenizer(is);
PrintStream output = System.out;
int i;
int next = 0;
tokens.resetSyntax();
tokens.wordChars(0,255);
tokens.quoteChar(';');
while ((next = tokens.nextToken()) != tokens.TT_EOF) {
switch (next) {
case ';':
output.print("got a line:"+tokens.lineno());
break;
case StreamTokenizer.TT_WORD:
output.print(tokens.sval+" ");
break;
case StreamTokenizer.TT_NUMBER:
output.print(tokens.nval+" ");
break;
case StreamTokenizer.TT_EOL:
output.println();
break;
} // end switch
} // end while
is.close();
} // end try
catch (Exception exe)
{System.out.println("writeFilteredHrefFile:er!");}
}
public static void list_filtered_href_file(File file) {
System.out.println("processing:\t" + file);
try {
FileReader fr = getFileReader(file);
StreamTokenizer tokens = new
StreamTokenizer(fr);
int next = 0;
tokens.resetSyntax();
tokens.wordChars(0,255);
tokens.quoteChar('"');
while ((next = tokens.nextToken()) != tokens.TT_EOF)
{
switch (next) {
case '"':
System.out.print('"');
StringTokenizer st =
new StringTokenizer(tokens.sval," ");
while (st.hasMoreTokens()) {
System.out.print(st.nextToken());
if (st.countTokens() > 1)
{System.out.print("%20");}
}
System.out.print('"');
break;
case StreamTokenizer.TT_WORD:
System.out.print(tokens.sval+" ");
break;
case StreamTokenizer.TT_NUMBER:
System.out.print(tokens.nval+" ");
break;
case StreamTokenizer.TT_EOL:
System.out.println();
break;
}
}
System.out.println();
fr.close();
}
catch (Exception exe)
{System.out.println("list_filtered_href_file:er!");}
}
public static void filterFileHrefs() {
FileReader fis = getFileReader();
StreamTokenizer tokens = new StreamTokenizer(fis);
int next = 0;
tokens.resetSyntax();
tokens.wordChars(0,255);
tokens.quoteChar('"');
try { while ((next = tokens.nextToken())
!= tokens.TT_EOF) {
switch (next) {
case '"':
System.out.print('"');
StringTokenizer st =
new StringTokenizer(tokens.sval," ");
while (st.hasMoreTokens()) {
System.out.print(st.nextToken());
if (st.countTokens() > 1)
{System.out.print("%20");}
}
System.out.print('"');
break;
case StreamTokenizer.TT_WORD:
System.out.print(tokens.sval+" ");
break;
case StreamTokenizer.TT_NUMBER:
System.out.print(tokens.nval+" ");
break;
case StreamTokenizer.TT_EOL:
System.out.println();
break;
} // switch
}} // try while
catch (IOException e) {
System.out.println("Futil:ER! filterFileHrefs");
}
System.out.println();
closeFileReader(fis);
}
public static void processJava() {
FileReader fis = getFileReader();
String line;
BufferedReader dis = new BufferedReader(fis);
System.out.println("<HTML><BODY><PRE>");
try { while ((line = dis.readLine()) != null)
System.out.println(line);
}
catch (IOException e) {
System.out.println("Futil: ER! in processJava");
}
System.out.println("</PRE></BODY></HTML>");
closeFileReader(fis);
}
public void lowerFileNames( File thePath ){
String[] fileNames = thePath.list();
String pathstr = thePath.getPath();
for( int i=0;
fileNames != null &&
i< fileNames.length; i++ ) {
String aFileName = fileNames[ i ];
String newFileName = aFileName.toLowerCase();
File theFile = new File( pathstr, aFileName );
if( theFile.isFile() ){
//rename theFile to lower case
System.out.print( i+":" + aFileName );
theFile.renameTo( new File( pathstr, newFileName ) );
System.out.println( "\t==>\t"+newFileName );
}else{
//case theFile is Dir, in the Dir, repeat same procedure
System.out.println( "Dir:"+aFileName );
lowerFileNames( new File( pathstr+aFileName ));
}
}
return;
}//lowerFileNames
public static void makeTocHtml() {
File dir = getDirFile();
String[] files = dir.list(new FileFilter());
System.out.println(files.length +
" file(s):");
File fn = getWriteFile();
FileWriter fw=null;
try {
fw = new FileWriter(fn);
}
catch (Exception e) {
}
PrintWriter pw = new PrintWriter(fw);
pw.println("<HTML>");
pw.println("<BODY>");
pw.println("<ul>");
for (int i=0; i < files.length; i++){
pw.println(
"<LI><a href = \"" +
files[i]+
"\">"+
files[i]+
"</a><P>");
System.out.println(files[i]);
}
pw.println("</ul>");
pw.println("</BODY>");
pw.println("</HTML>");
try {
fw.close();
}
catch (Exception e) {
}
}
public static void readDataFile(File file,
double data[]) {
System.out.println("processing:\t" + file);
FileReader inputFile =
getFileReader(file);
StreamTokenizer tokens = new
StreamTokenizer(inputFile);
int next = 0;
int num = 0;
try {
while (
(next = tokens.nextToken()) !=
tokens.TT_EOF) {
switch (next) {
case StreamTokenizer.TT_WORD:
break;
case StreamTokenizer.TT_NUMBER:
data[num] = (double) tokens.nval;
System.out.println(num+": "+
data[num]);
num = num + 1;
break;
case StreamTokenizer.TT_EOL:
break;
}
}
}
catch (Exception exe)
{System.out.println("listFilteredHrefFile:er!");}
closeFileReader(inputFile);
}
void writeObject(ObjectOutputStream oos)
throws IOException {
oos.defaultWriteObject();
}
void readObject(ObjectInputStream ois) throws ClassNotFoundException,
IOException {
ois.defaultReadObject();
}
}
|