Posts

Showing posts from June 28, 2010

How to convert String to color code in java code examples

 public static Color convertStringToColor(String colorString)     {         int k = Integer.parseInt(colorString, 16);         int j1 = k >> 16 & 0xff;         int i2 = k >> 8 & 0xff;         int l2 = k & 0xff;         return new Color(j1, i2, l2);     }

How to copy file using FileReader in java

public static void copyFile(String sourcePath, String destinationPath){                                   File inputFile = new File(sourcePath);              File outputFile = new File(destinationPath);               try {                 FileReader in = new FileReader(inputFile);                 FileWriter out = new FileWriter(outputFile);                   int c;                   while ((c = in.read()) != -1)                   out.write(c);                                      in.close();                   out.close();             } catch (FileNotFoundException e) {                                 e.printStackTrace();             } catch (IOException e) {                                 e.printStackTrace();             }                                                         }

How to copy file using FromFile in Struts java

public String copyFile(FormFile formFile, String destinationFolder,             String outputFileName) throws IOException {         byte[] data = new byte[1000];                 InputStream input = formFile.getInputStream();         String filename = formFile.getFileName();         new File(destinationFolder).mkdirs();         File file = new File(destinationFolder, outputFileName);         FileOutputStream output = new FileOutputStream(file);         try {             try {                 while (true) {                     int count = input.read(data);                     if (count == -1)                         break;                     output.write(data, 0, count);                 }             } catch (Exception e) {                 e.printStackTrace();             }             finally {                 input.close();             }         } catch (Exception e) {             e.printStackTrace();         } finally {             output.close();        

How to Draw Color Picker applet Java code example

import java.awt.*; public class ColorPicker extends Panel {     public ColorPicker(String s, Color c)     {         b = new Button("...");         p = new Panel();         setLayout(null);         color = c;         l = new Label(s);         l.setBounds(0, 0, 100, 20);         b.setBounds(100, 0, 20, 20);         p.setBounds(120, 0, 40, 20);         add(l);         add(b);         add(p);         setSize(160, 20);     }     public ColorPicker(String s, Color c, int w)     {         this(s, c);         l.setBounds(0, 0, w, 20);         b.setBounds(w, 0, 20, 20);         p.setBounds(w + 20, 0, 40, 20);         setSize(60 + w, 20);     }     public void setColor(Color c)     {         color = c;         p.setBackground(c);     }     public Color getColor()     {         return color;     }     public boolean handleEvent(Event evt)     {         if(evt.target == b && evt.id == 1001)         {             ColorDialog cd = new Co

How to read data byte by byte in java code examples

public  void readDataByteByByte( byte[] data,int contentLength,InputStream raw){         InputStream in = new BufferedInputStream(raw);         int offset = 0;         try{         int bytesRead = 0;         while (offset < contentLength) {           bytesRead = in.read(data, offset, data.length - offset);           if (bytesRead == -1)             break;           offset += bytesRead;         }         }catch (Exception e) {             e.printStackTrace();         }         finally{             if (in!=null)                 try {                     in.close();                 } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                 }         }         if (offset != contentLength) {               try {                 throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");             } catch (IOException e)

Recent Post

Recent Posts Widget