Advertisement
brilliant_moves

FileCopy.java

Aug 30th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.00 KB | None | 0 0
  1. import java.io.*;               // for file input/output
  2. import javax.swing.*;               // required for input dialogs
  3. import javax.swing.filechooser.FileFilter;  // for choosing file name
  4.  
  5. public class FileCopy {
  6.  
  7.     /**
  8.     *   Program:    FileCopy.java
  9.     *   Purpose:    Copy a text file to another file or directory
  10.     *   Creator:    Chris Clarke
  11.     *   Created:    26.12.2012
  12.     *   Modified:   26.05.2013 for verbose type: java FileCopy -v
  13.     *           31.05.2013 MyTextFilter added (*.java, *.txt)
  14.     */
  15.  
  16.     public static void main(String[] args) {
  17.         boolean ver = false;
  18.         if (args.length>0) {
  19.             if (args[0].equals("-v")) {
  20.                 ver = true;
  21.             } else {
  22.                 System.out.println("Type \"-v\" for verbose");
  23.             }//end if
  24.         }//end if
  25.         String allText = openTextFile(ver);
  26.         saveAsTextFile( allText);
  27.     }// end main
  28.  
  29.     public static String openTextFile(boolean verbose) {
  30.         String textInput = "";
  31.         try {
  32.             // open a file chooser
  33.             JFileChooser chooser = new JFileChooser();
  34.             chooser.setCurrentDirectory(new File("."));
  35.             chooser.setFileFilter(new MyTextFilter());
  36.             if (chooser.showOpenDialog(null) != 1) { // not cancel button
  37.                 String filename = chooser.getSelectedFile().getPath();
  38.                 if (filename != null) {
  39.                     String str=" "; // anything but null
  40.                     BufferedReader fr = new BufferedReader(new FileReader( filename));
  41.                     LineNumberReader lnr = new LineNumberReader(fr);
  42.                     while (str != null) {
  43.                         str = lnr.readLine();
  44.                         if (str==null) break;
  45.                         if (verbose) {
  46.                             System.out.println(str); // display text
  47.                         }//end if
  48.                         // add text to textInput
  49.                         textInput += str + "\r\n";
  50.                     }//end while
  51.                     lnr.close();
  52.                     fr.close();
  53.                 }//end if
  54.             } else {//file dialog closed
  55.                 System.out.println("Program closing");
  56.                 System.exit(1);
  57.             }//end if
  58.         }
  59.         catch (IOException x) {
  60.             System.out.println("Caught IOException");
  61.         }
  62.         return textInput;
  63.     }// end openTextFile
  64.  
  65.     public static void saveAsTextFile(String completeText) {
  66.         // open a file chooser
  67.         JFileChooser chooser = new JFileChooser();
  68.         chooser.setCurrentDirectory(new File("."));
  69.         chooser.setFileFilter(new MyTextFilter());
  70.         chooser.setAcceptAllFileFilterUsed(true);
  71.         if (chooser.showSaveDialog(null) == 1) {
  72.             System.out.println("File not copied!");
  73.             return; // cancel button
  74.         }//end if
  75.         String filename = chooser.getSelectedFile().getPath();
  76.         if (filename == null) {
  77.             System.out.println("File not copied!");
  78.             return;
  79.         }//end if
  80.         try {
  81.             BufferedWriter fw = new BufferedWriter(new FileWriter( filename));
  82.             fw.write(completeText);
  83.             fw.close();
  84.             System.out.println("File copied successfully.");
  85.         }
  86.         catch (IOException x) {
  87.             System.out.println("Caught IOException");
  88.         }
  89.     }// end saveAsTextFile
  90. }// end class
  91.  
  92. class MyTextFilter extends FileFilter {
  93.     public boolean accept(File f) {
  94.         return f.getName().toLowerCase().endsWith(".txt") ||
  95.          f.getName().toLowerCase().endsWith(".java") ||  f.isDirectory();
  96.     }
  97.  
  98.     public String getDescription() {
  99.         return "Text File (*.txt, *.java)";
  100.     }
  101. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement