{"id":66318,"date":"2021-07-18T11:24:36","date_gmt":"2021-07-18T15:24:36","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/java-file-operation-example-java-short-notes\/"},"modified":"2021-07-18T11:24:36","modified_gmt":"2021-07-18T15:24:36","slug":"java-file-operation-example-java-short-notes","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=66318","title":{"rendered":"Java File Operation Example #Java Short Notes"},"content":{"rendered":"<p>   Code that helped me to create <a href='http:\/\/add.justEtc.net' target='new' rel=\"noopener\">\thttp:\/\/add.justEtc.net<\/a> &#8211; the Bangladesh section based on the the\t web-site <a href='http:\/\/winnipeg.justEtc.net' target='new' rel=\"noopener\">\thttp:\/\/winnipeg.justEtc.net<\/a><\/p>\n<p><pre style='padding-left:5px;font-size:12px'>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 \t\/\/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\tWinnipeg\";  \/\/JOptionPane.showInputDialog(null,\"Destination Path\");         destination = \"C:test_developmentjavahellosrcresources\tdestination\";                 \/\/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 \t\/\/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 \t\t\t\t\/\/| 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), \t\tcity);        }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 \tIOException {        \/\/if srcDirectory is a directory , create the directory        if (srcDir.isDirectory()) {            if (!dstDir.exists()) {                dstDir.mkdir();            }            \/\/if srcDir has child directories , create child directories - \t\tRecursively            String[] children = srcDir.list();            for (int i=0; i&lt;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 = &quot;&quot;;            \/\/replace old city name with the new city name            while ((thisLine = myInput.readLine()) != null) {              if (thisLine.contains(&quot;Winnipeg&quot;)){                  thisLine=thisLine.replaceAll(&quot;Winnipeg&quot;, city);                  thisLine=thisLine.replaceAll(&quot;Manitoba,&quot;, &quot;&quot;);                  thisLine=thisLine.replaceAll(&quot;Canada&quot;, &quot;Bangladesh&quot;);              }              myOutput.write(thisLine);              myOutput.write(&quot;n&quot;);            }            myOutput.flush();            in.close();            out.close();        } catch (IOException e) {            e.printStackTrace();  \/\/To change body of catch statement use File | \t\t\t\t\/\/Settings | File Templates.        } finally {        }    }    \/**       Creates a file with links to all cities     *\/    private void createFile() {        try {            FileWriter out = new FileWriter(&quot;test.txt&quot;);            PrintWriter myOutput = new PrintWriter(out);            Iterator it = cityList.iterator();            String city = &quot;&quot;;            while(it.hasNext()){            city = (String) it.next();String thisLine =                        &quot;<tr>n\" +                                \"t t  <td><a>\"\"+\t\t\t\tcity+\"\"<\/a><\/td>n\"\" +                        \"\"n\"\";                              myOutput.write(thisLine);                  myOutput.write(\"\"n\"\");                  myOutput.flush();            }        } catch (IOException e) {            e.printStackTrace();  \/\/To change body of catch statement use File \t\t\t\t\/\/| Settings | File Templates.        }    }}\/** * Main class - class containing the main method *\/public class Hello {    public static void main(String args[]){        Process process = new Process();    }}<\/tr><\/pre>\n<\/p>\n<p>&#8221; From: http:\/\/sitestree.com\/?p=4945<br \/> Categories:Java Short Notes<br \/>Tags:<br \/> Post Data:2013-03-18 14:29:36<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code that helped me to create http:\/\/add.justEtc.net &#8211; 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 &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=66318\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-66318","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":65938,"url":"http:\/\/bangla.sitestree.com\/?p=65938","url_meta":{"origin":66318,"position":0},"title":"EJB 3: Entity Bean Basic #Java Short Notes","author":"Author-Check- Article-or-Video","date":"July 18, 2021","format":false,"excerpt":"Plain Java objects with persistence storage. They are not remotable and must be accessed through the new javax.persistence.EntityManager service. An entity bean implementation is provided in the following three files\/classesBeansOrder.javaLineItem.javaEntity ManagerShoppingCartBean.java\/\/Order.javaimport javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue; import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;import javax.persistence.Id;import javax.persistence.CascadeType;import javax.persistence.FetchType;import java.util.ArrayList;import java.util.Collection;@Entity@Table(name = \"PURCHASE_ORDER\")public class Order implements\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26547,"url":"http:\/\/bangla.sitestree.com\/?p=26547","url_meta":{"origin":66318,"position":1},"title":"PrimeNumbers.java  Servlet that processes a request to generate n prime numbers, each with at least m digits. If these results are not complete, it sends a Refresh header instructing the browser to ask for new results a little while later. Uses the Primes #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"PrimeNumbers.java Servlet that processes a request to generate n prime numbers, each with at least m digits. If these results are not complete, it sends a Refresh header instructing the browser to ask for new results a little while later. Uses the Primes, PrimeList, and ServletUtilities classes. package cwp; import\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26856,"url":"http:\/\/bangla.sitestree.com\/?p=26856","url_meta":{"origin":66318,"position":2},"title":"DOM example that represents the basic structure of an XML document as a JTree #Programming Code Examples #Java\/J2EE\/J2ME #JavaScript","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"\/\/XMLTree.java \/\/Uses the following files Uses the following files: * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data on\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10308,"url":"http:\/\/bangla.sitestree.com\/?p=10308","url_meta":{"origin":66318,"position":3},"title":"Message.java Applet that reads customization parameters from an HTML file","author":"","date":"August 26, 2015","format":false,"excerpt":"******************* Message.java Applet that reads customization parameters from an HTML file ******************* import java.applet.Applet; import java.awt.*; **************** \u00a0 public class Message extends Applet { \u00a0 private int fontSize; \u00a0 private String message; \u00a0 \u00a0 public void init() { \u00a0\u00a0\u00a0 setBackground(Color.black); \u00a0\u00a0\u00a0 setForeground(Color.white); \u00a0\u00a0 \u00a0 \u00a0\u00a0\u00a0 \/\/ Base font size on\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10173,"url":"http:\/\/bangla.sitestree.com\/?p=10173","url_meta":{"origin":66318,"position":4},"title":"PrimeNumbers.java Servlet that processes a request to generate n prime numbers, each with at least m digits. If these results are not complete, it sends a Refresh header instructing the browser to ask for new results a little while later. Uses the Primes","author":"","date":"August 18, 2015","format":false,"excerpt":"PrimeNumbers.java\u00a0 Servlet that processes a request to generate n prime numbers, each with at least m digits. If these results are not complete, it sends a Refresh header instructing the browser to ask for new results a little while later. Uses the Primes, PrimeList, and ServletUtilities\u00a0 classes. \u00a0 package cwp;\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10245,"url":"http:\/\/bangla.sitestree.com\/?p=10245","url_meta":{"origin":66318,"position":5},"title":"DOM example that represents the basic structure of an XML document as a JTree","author":"","date":"August 25, 2015","format":false,"excerpt":"\/\/XMLTree.java \/\/Uses the following files Uses the following files: \u00a0\u00a0\u00a0 * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/66318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66318"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/66318\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66318"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}