• Post Reply Bookmark Topic Watch Topic
  • New Topic

copy file

 
Michael.H.Sun
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi My friends,
How can I modify the following to copy any file endWith xsl on the desktop in stead of only copying data.xsl?
Your explanation is greatly appreciated.
import java.io.*;
import java.lang.*;
public class cfXSL {
public static void main(String[] args) {
try {
File inputFile = new
File("c:/windows/desktop/data.xsl");
String [] filenames = inputFile.list ();
File outputFile = new
File("c:/windows/desktop/WelcomeApplet3.java");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
for (int i=0; i<filenames.length; i++)<br /> {<br /> if (filenames [i].endsWith (".xsl"))<br /> {<br /> LineNumberReader lnr = new LineNumberReader(in);<br /> String s;<br /> while ( (s = lnr.readLine()) != null ) {<br /> System.out.println(lnr.getLineNumber() + "==> " + s );
out.write(s);
}
lnr.close();
in.close();
out.close();
}
}
}
catch (IOException ie) { System.out.println("input/output error"); }
}
}
 
sai challa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO,
You can add an inner class
class XSLFileFilter implements FileNameFilter {
public boolean accept(File dir,String name) {
return (name.endsWith(".xsl"));
}
}
and you can add the following modification in your program:
File inputFile=new File("c:/windows/desktop");
String[] filenames=inputFile.list(new XSLFileFilter());
This will list all xsl files in "c:\windows\desktop" directory
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!