2015년 1월 27일 화요일

[Java] Unzip zip file.

Unzip zip file.

Source.

public class UnZip
{
    List<String> fileList;
    public UnZip(String zipFile, String outputFolder){

     byte[] buffer = new byte[1024];

     try{
     File folder = new File(outputFolder);
     if(!folder.exists()){
      folder.mkdir();
     }

     ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
     ZipEntry ze = zis.getNextEntry();

     while(ze!=null){

        String fileName = ze.getName();
           File newFile = new File(outputFolder + File.separator + fileName);

           MainFrame.str = ""+newFile.getAbsoluteFile();
           new File(newFile.getParent()).mkdirs();

           FileOutputStream fos = new FileOutputStream(newFile);            

           int len;
           while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
           }
           fos.close();  
           ze = zis.getNextEntry();
     }
        zis.closeEntry();
     zis.close();
   }   
}
explane
 
​public class UnZip //class
{
    List<String> fileList; //file that compressed.
    public UnZip(String zipFile, String outputFolder){ //Method

     byte[] buffer = new byte[1024]; //data

     try{ //Check forder and Zip File.
     File folder = new File(outputFolder);
     if(!folder.exists()){
      folder.mkdir();
     }

     ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //read ZipFile.
     ZipEntry ze = zis.getNextEntry(); //new ZipEntry.

     while(ze!=null){ //Unzip

        String fileName = ze.getName(); //File name.
           File newFile = new File(outputFolder + File.separator + fileName); //File Dir

           new File(newFile.getParent()).mkdirs();

           FileOutputStream fos = new FileOutputStream(newFile); //File write.

           int len;
           while ((len = zis.read(buffer)) > 0) { //read File.
            fos.write(buffer, 0, len);
           }
           fos.close();  
           ze = zis.getNextEntry(); //Next File
     }
        zis.closeEntry(); //fin
     zis.close();
   }   
}


You can use this source like new UnZip("Zip file location","output Dir");

[Java] Play wav file.

wav file play source.

public class Sound {
 public Sound(String file, boolean Loop){
  Clip clip;
  try {
  AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));
  clip = AudioSystem.getClip();
  clip.open(ais);
  clip.start();
  if ( Loop) clip.loop(-1);
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }
}

explane.

public class Sound { // class
 public Sound(String file, boolean Loop){ // method
  Clip clip; //new Clip
  try {
  AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file))); //read wav file.
  clip = AudioSystem.getClip(); //setting clip.
  clip.open(ais); //read ais in clip
  clip.start(); //Music start.
  if ( Loop) clip.loop(-1); //if Loof = true, play over and over.
  }
  catch (Exception e) { //exception
   e.printStackTrace();
  }
 }
}

in main method write new Sound("File Dir",false);

[Java] File download.(BufferInputStream)

Java File Download source.



inputstream과 outputstream을 사용.
try {
         URL url=new URL("URL");
         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
         int filesize = connection.getContentLength();
         float totalDataRead=0;
         java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
         java.io.FileOutputStream fos = new java.io.FileOutputStream("C:/temp.zip");
         java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
         byte[] data = new byte[1024];
         int i=0;
         while((i=in.read(data,0,1024))>=0) {
               totalDataRead=totalDataRead+i;
               bout.write(data,0,i);
               float Percent=(totalDataRead*100)/filesize;
         }
         bout.close();
         in.close();
}
catch(Exception e1) { }

explane
try {
         URL url=new URL("URL");  //File download adress
         HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //connect URL
         int filesize = connection.getContentLength(); //파일 사이즈를 가져옵니다.
         float totalDataRead=0; // totalData read (byte)
         BufferedInputStream in = newBufferedInputStream(connection.getInputStream()); //read File from url
         OutputStream fos = new FileOutputStream("C:/temp.zip"); //save adress.
         BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
         byte[] data = new byte[1024];
         int i=0;
         while((i=in.read(data,0,1024))>=0) { //Start Downloading
               totalDataRead=totalDataRead+i;
               bout.write(data,0,i); //download
               float Percent=(totalDataRead*100)/filesize; //Percent of Download.
               System.out.println(Percent);
         }
         bout.close();
         in.close();
}
catch(Exception e1) { }
ㅃㅃ.
 

2014년 12월 12일 금요일

[Java] JFrame Fullscreen 윈도우창 전체화면 만들기

I will make JFrame to Fullscreen.
JFrame을 전체화면으로 만들어볼 것이다.




You can just add short code for make foolscreen.
짧은 코드만 쓰면 전체화면으로 만들 수 있다.





add this code
이 코드를 쓰자.


Like this
이렇게





finish!


끝이다
이거 키면 전체화면으로 JFrame이 실행된다.





2014년 12월 11일 목요일

[Java][자바] JFrame 윈도우 창 띄우기.

Today I will write about JFrame.
오늘은 JFrame 에 대해 적어보겠다.


JFrame은 일종의 객체이다. 윈도우 창을 만들기 위해 필요하다.




code :






JFrame jfm = new JFrame("title");
jfm.setSize(500,500);
jfm.setVisible(true);






Finish.


끝!





실행시켜보면
launch.





finish.









2014년 12월 9일 화요일

[Minecraft] How to make a bukkit plugin : 1 setting and add commands

download minecraft bukkit in here http://plugins.bukkit.org/
I use 1.7.2 version.


launching eclipse and make new JavaProject


make pakage and class


add your bukkit-craftbukkit.jar to buildpath
write extends JavaPlugin next your class name.
last make 2 methods name onEnable and onDisable




like this






and then make boolean type method named onCommand


public boolean onCommand(CommandSender sender, Command command , String Label, String[] args)
 {
           if(Label.equalsIgnoreCase("a")) {
                    Player player = (Player) sender;
                    player.sendMessage("I am genius");
           }
  return true;


}

full code :




package hy.main;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class main extends JavaPlugin{
 public void onEnable() {

 }
 public void onDisable() {

 }
 public boolean onCommand(CommandSender sender, Command command , String Label, String[] args)
 {
  if(Label.equalsIgnoreCase("a")) {
   Player player = (Player) sender;
            player.sendMessage("I am genius");
  }
  return true;
 }
}


left side your project name - new - file


file name = plugin.yml


write






name : your project name
main : pakage name.class name
version : 1.0


commands :
  a:






save and Export to jar file


and put it to your bukkit-plugins forder and launching server
if you use command "/a" then server send Message to you "I am genius"