20220224 º¹½À³ëÆ®


ecilpsÀÇ ÆÄÀÏÀ» export ½ÃÄѼ­ jarÆÄÀÏÀ» ¸¸µé¾îÁÖ¸é,
ÀÚ¹Ù°¡ ±ò·ÁÀֱ⸸ ÇÑ´Ù¸é
cmd¿¡¼­ " java -jar ÆÄÀϸí.jar " ½ÇÇà °¡´É

¹Ì´Ï ÇÁ·ÎÁ§Æ® 4ÀÏÂ÷ ( addBackup(); )

     // p249(method)
     static void addRead() { // p904 (ÆÄÀÏÀÔÃâ·Â) p414 (¿¹¿Üó¸®)(»ó¼Ó)

          String fname="address.txt";
          String line="";
          //p915 (ÆÄÀÏÀÌ ¾øÀ¸¸é ¸¸µé¾î¶ó)
          File f = new File(fname);
          if (!f.exists()) {

               try {
                    FileWriter fw = new FileWriter(fname);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.close();

               } catch (IOException e) {

               }
          }

          try {
               FileReader fr=new FileReader(fname);
               BufferedReader br=new BufferedReader(fr);

               for (int i = 0; ( line= br.readLine() ) != null; i++) {
                    System.out.println(line);
               }
               br.close(); // ¿­·ÁÀÖ´Â FileÀ» ²À ´Ù½Ã ´Ý¾ÆÁÖ±â
          }catch(IOException e){
               System.out.println("¿¡·¯¹ß»ý");
          }

     }

     static void addCreate() {

          Scanner sc=new Scanner(System.in);
          String name;
          String phone;
          String email;
          String address;
          String add_list="";

          String fname="address.txt"; // FileWriter (µ¤À¸¾º¿ì±âX Ãß°¡ÇϱâO)
          String line="";

          try {
               FileWriter fw = new FileWriter(fname, true); // True·Î Ãß°¡ÇØÁÖ±â
               BufferedWriter bw = new BufferedWriter(fw);
               // Scanner·Î ÀԷ¹޾Ƽ­ add_list ä¿ì±â!!!
               System.out.print("Ãß°¡ÇÒ À̸§À» ÀÔ·ÂÇØÁÖ¼¼¿ä: ");
               name = sc.nextLine();
               System.out.println(name);
               System.out.print("Ãß°¡ÇÒ ÀüÈ­¹øÈ£¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä: ");
               phone = sc.nextLine();
               System.out.println(phone);
               System.out.print("Ãß°¡ÇÒ À̸ÞÀÏÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä: ");
               email = sc.nextLine();
               System.out.println(email);
               System.out.print("Ãß°¡ÇÒ ÁÖ¼Ò¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä: ");
               address = sc.nextLine();
               System.out.println(address);
               add_list = name +","+ phone +","+ email +","+ address;
               System.out.println("Ãß°¡ÇÑ ³»¿ë: "+add_list);

               bw.write(add_list);
               bw.newLine(); // µ¥ÀÌÅÍ ÀÔ·Â ÈÄ ¹ØÀ¸·Î °³Çà
               bw.close();
          } catch (IOException e) {

          }

     }

     static void addUpdate() {
          System.out.println("update!!");
     }

     static void addDelete() {
          System.out.println("delete!!");
     }

     static void addSearch() {
          System.out.println("search!!");
     }

     static void addBackup() {
          /*
           * ¼­¹ö°¡ Áغñ µÇ¾îÀÖ´ÂÁö È®ÀÎ
           * FTP 21
           * ¾ÆÀ̵ð, ºñ¹Ð¹øÈ£
           */

          String fname="address.txt";
          String server= "jslweb.izerone.co.kr";
          int port=21;
          String user="master";
          String pass="1qaz2wsx";

          FTPClient ftpClient = new FTPClient();

          try {

               ftpClient.connect(server, port);
               ftpClient.login(user, pass);

               /*¼¼Æà ½ÃÀÛ */
               ftpClient.enterLocalPassiveMode();
               ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
               /*¼¼Æà ³¡*/

               System.out.print("backupÀ» ½ÃÀÛÇÕ´Ï´Ù.");
               File firstLocalFile = new File(fname);
               String firstRemoteFile = "address.txt";
               InputStream inputStream = new FileInputStream(firstLocalFile);

               System.out.println("Start uploading first file");
               boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
               inputStream.close();

               if (done) {
                    System.out.println("ÁÖ¼Ò·ÏÀÌ Á¤»óÀûÀ¸·Î FTP¼­¹ö¿¡ ¾÷·Îµå µÇ¾ú½À´Ï´Ù.");
                    System.out.println("½Ã½ºÅÛÀÌ Á¾·áµÇ¾ú½À´Ï´Ù.");
               }
               ftpClient.disconnect();

          }catch(IOException ex) {
               System.out.println("Error: " + ex.getMessage());
               ex.printStackTrace();
          }finally {
               try {
                    if (ftpClient.isConnected()) {
                         ftpClient.logout();
                         ftpClient.disconnect();
                    }
               } catch (IOException ex) {
                    ex.printStackTrace();
               }
          }

     }

     static void addMenu() {
          //p36 (printf)
          System.out.println("< ¹Ì´Ï ÇÁ·Î±×·¥ À̸§ >");
          System.out.println("1. ÁÖ¼ÒÃâ·Â");
          System.out.println("2. ÁÖ¼Òµî·Ï");
          System.out.println("3. ÁÖ¼Ò»èÁ¦");
          System.out.println("4. ÁÖ¼Ò¼öÁ¤");
          System.out.println("5. ÁÖ¼Ò°Ë»ö");
          System.out.println("6. Á¾·á");
     }

     public static void main(String[] args) {

          Scanner sc=new Scanner(System.in);
          String select="";

          while (select!="6") {

               addMenu();
               System.out.println("-¹øÈ£¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä.-");
               select=sc.nextLine();

               switch (select) {
               case "1":
                    addRead();
                    break;
               case "2":
                    addCreate();
                    break;
               case "3":
                    addUpdate();
                    break;
               case "4":
                    addDelete();
                    break;
               case "5":
                    addSearch();
                    break;
               case "6":
                    addBackup();
                    return;

               default:
                    System.out.println("À߸øµÈ ¹øÈ£. ´Ù½Ã ÀÔ·ÂÇϽÿÀ.");
               }

          }

          /*
          addMenu();
          addRead(); 1
          addCreate(); 2
          addUpdate(); 3
          addDelete(); 4
          addSearch(); 5
          */
     }

}