Java File Operation Example #Java Short Notes

Code that helped me to create http://add.justEtc.net – the Bangladesh section based on the the web-site http://winnipeg.justEtc.net

package hello;import javax.swing.JOptionPane;import java.util.ArrayList;import java.util.Iterator;import java.io.*;/** * Created by IntelliJ IDEA. * User: Sayed Ahmed * Date: May 30, 2008 * Time: 9:45:08 PM * To change this template use File | Settings | File Templates. *//**Class containing major methods - operation methods*/class Process{    private FileReader src = null; //source folder    private FileWriter dest = null; //destination folder    private ArrayList cityList = new ArrayList(); //arraylist to contain 	//the names of all cities    private String cityFileName; //reference to the file containing all city names    private String folderToCopy; //path to the folder to be copied    private String destination; // path  to the folder where the copy will be kept    /**     * Constructor     */    Process(){//JOptionPane.showInputDialog(null,"City File Name");        cityFileName = "C:test_developmentjavaresourcescityList.txt";  //JOptionPane.showInputDialog(null,"Folder to Copy");        folderToCopy = "C:test_developmentjavahellosrcresourcessource	Winnipeg";  //JOptionPane.showInputDialog(null,"Destination Path");         destination = "C:test_developmentjavahellosrcresources	destination";                 //load city list from the file to an arraylist        copyCityList(cityFileName);        //to create a file with links to all the cities of Bangladesh        //createFile();        //copy a folder (winnipeg) and rename it to all the city names of 	//Bangladesh -- one by one        Iterator it = cityList.iterator();        while(it.hasNext()){            copyDirectory((String) it.next());        }    }    /**     * load city list into an ArrayList     */    private void copyCityList(String cityFileName){        String city = "";        try {            BufferedReader bfCity = new BufferedReader(new FileReader(cityFileName));            while(null != (city = bfCity.readLine())){                cityList.add(city);            }        } catch (IOException e) {            e.printStackTrace();  //To change body of catch statement use File 				//| Settings | File Templates.        } finally {        }    }    /**     * copy a folder , name the folder as the city name     */    private void copyDirectory(String city){        try {            copyDirectory(new File(folderToCopy), new File(destination+"/"+city), 		city);        }catch (IOException e){            e.printStackTrace();        }    }    /**     * Copies all files under srcDir to dstDir.     * If dstDir does not exist, it will be created.     * @param srcDir - folder to copy     * @param dstDir - where to keep     * @param city  - new folder name     * @throws IOException     */    public void copyDirectory(File srcDir, File dstDir, String city) throws 	IOException {        //if srcDirectory is a directory , create the directory        if (srcDir.isDirectory()) {            if (!dstDir.exists()) {                dstDir.mkdir();            }            //if srcDir has child directories , create child directories - 		Recursively            String[] children = srcDir.list();            for (int i=0; i<children .length; i++) {                copyDirectory(new File(srcDir, children[i]),                                     new File(dstDir, children[i]),city);            }        } else {            //srcDir is a file, so copy it to the new location            copyFile(srcDir, dstDir, city);        }    }    /**     * Copy a file     * @param src - source file     * @param dst - destination file     * @param city - city under consideration     */    private void copyFile(File src, File dst, String city) {        InputStream in;        OutputStream out;        try {            in = new FileInputStream(src);            out = new FileOutputStream(dst);            BufferedReader myInput = new BufferedReader   (new InputStreamReader(in));            PrintWriter myOutput = new PrintWriter(dst);            String thisLine = "";            //replace old city name with the new city name            while ((thisLine = myInput.readLine()) != null) {              if (thisLine.contains("Winnipeg")){                  thisLine=thisLine.replaceAll("Winnipeg", city);                  thisLine=thisLine.replaceAll("Manitoba,", "");                  thisLine=thisLine.replaceAll("Canada", "Bangladesh");              }              myOutput.write(thisLine);              myOutput.write("n");            }            myOutput.flush();            in.close();            out.close();        } catch (IOException e) {            e.printStackTrace();  //To change body of catch statement use File | 				//Settings | File Templates.        } finally {        }    }    /**       Creates a file with links to all cities     */    private void createFile() {        try {            FileWriter out = new FileWriter("test.txt");            PrintWriter myOutput = new PrintWriter(out);            Iterator it = cityList.iterator();            String city = "";            while(it.hasNext()){            city = (String) it.next();String thisLine =                        "n" +                                "t t  ""+				city+""n"" +                        ""n"";                              myOutput.write(thisLine);                  myOutput.write(""n"");                  myOutput.flush();            }        } catch (IOException e) {            e.printStackTrace();  //To change body of catch statement use File 				//| Settings | File Templates.        }    }}/** * Main class - class containing the main method */public class Hello {    public static void main(String args[]){        Process process = new Process();    }}

” From: http://sitestree.com/?p=4945
Categories:Java Short Notes
Tags:
Post Data:2013-03-18 14:29:36

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada