working on it ...

Filters

snippets
4
followers
2
Published by pplant

Cool stuff by pplant

This channel collects the brain compositions of this guy
Sort by

Found 4 snippets

    public by pplant modified Jul 28, 2014  3289  3  6  1

    Read/Write File in Java

    An easy way to read a file and append text to it
    try {
      File aFile = new File("/Path/to/File/file_name.txt");
      
      //Set parameter true if you want to append content otherwise false
      BufferedWriter bW = new BufferedWriter(new FileWriter(aFile,true));
                
      String line = "Text to file";
    	bW.write(line);
      bW.newLine();
      bW.flush();
      bW.close();
    } catch (IOException e) {
    	//Put catching code here
    }

    public by pplant modified Jul 25, 2014  298339  3  6  2

    Simple e-mail validator

    This is a simple email validator which can be used in forms to check whether a given email address is valid or not.
    public static boolean isValidEmailAddress(String email) {
    	   boolean result = true;
    	   try {
    	      InternetAddress emailAddr = new InternetAddress(email);
    	      emailAddr.validate();
    	   } catch (AddressException ex) {
    	      result = false;
    	   }
    	   return result;
    }
    

    public by pplant modified Jul 25, 2014  2651  2  6  2

    Scale Image to fit dimensions

    This snippet of code allows you to scale a image proportional such that it fits the provided dimensions.
    public static ImageIcon getScaledImage(ImageIcon srcImg, int w, int h){
    		int imgWidth = srcImg.getIconWidth();
    		int imgHeight = srcImg.getIconHeight();
    		
    		//Find the right width
    		if(imgWidth > w){
    			float sWidth = imgWidth-w;
    			float sFactor = (sWidth/imgWidth);
    			imgWidth = w;
    			imgHeight = (int) (imgHeight-(imgHeight*sFactor));
    		}
    		
    		//Find the right hight 
    		if(imgHeight > h){
    			float sHeight = imgHeight-h;
    			float sFactor = sHeight/imgHeight;
    			imgHeight = h;
    			imgWidth = (int) (imgWidth-(imgWidth*sFactor));
    		}
    		
    		BufferedImage resizedImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    		Graphics2D g2 = resizedImg.createGraphics();
    		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    		g2.drawImage(srcImg.getImage(), 0, 0, imgWidth, imgHeight, null);
    		g2.dispose();
    		return new ImageIcon(resizedImg);
    	}

    public by pplant modified Jul 25, 2014  1745  1  6  0

    Get current date with format

    This method returns you the current date-time by providing a string format as parameter (e.g dd/MM/yyyy hh:mm or MM-dd-yyyy)
    public static String getCurrentDateTime(String format){
    	DateFormat df = new SimpleDateFormat(format);
    	Date date = new Date();
    
    	return df.format(date);
    }
    

    List Complete ( 4 snippets total )

    • Public Snippets
    • Channels Snippets