1. Conversion of strings with integral type
String a = string.valueof (2); Integer to numeric string
int i = Integer.parseint (a); numeric string to a int
2. Add content to end of file
BufferedWriter out = null;
try {
out = new BufferedWriter (new FileWriter ("filename", true);
Out.write ("astring");
catch (IOException e) {
Error processing code
finally {
if (out!= null) {
Out.close ();
}
}
3. Get the name of the current method
String methodname = Thread.CurrentThread (). Getstacktrace () [1].getmethodname ();
4. Turn string to date
Java.util.Date = Java.text.DateFormat.getDateInstance (). Parse (Date String);
Or is:
SimpleDateFormat format = new SimpleDateFormat ("DD. Mm.yyyy ");
Date date = Format.parse (myString);
5. Using JDBC to link Oracle
public class Oraclejdbctest
{
String Driverclass = "Oracle.jdbc.driver.OracleDriver";
Connection con;
public void init (FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException
{
Properties Props = new properties ();
Props.load (FS);
String url = props.getproperty ("Db.url");
String userName = Props.getproperty ("Db.user");
String Password = props.getproperty ("Db.password");
Class.forName (Driverclass);
Con=drivermanager.getconnection (URL, userName, password);
}
public void Fetch () throws SQLException, IOException
{
PreparedStatement PS = con.preparestatement ("Select Sysdate from Dual");
ResultSet rs = Ps.executequery ();
while (Rs.next ())
{
Do the Thing
}
Rs.close ();
Ps.close ();
}
public static void Main (string[] args)
{
Oraclejdbctest test = new Oraclejdbctest ();
Test.init ();
Test.fetch ();
}
}
6. Util the Java. Date is converted to SQL. Date
Java.util.Date utildate = new Java.util.Date ();
Java.sql.Date sqldate = new Java.sql.Date (Utildate.gettime ());
7. Use of NiO for fast file copies
public static void FileCopy (file in, file out)
Throws IOException
{
FileChannel Inchannel = new FileInputStream (in). Getchannel ();
FileChannel Outchannel = new FileOutputStream (out). Getchannel ();
Try
{
Inchannel.transferto (0, Inchannel.size (), Outchannel); Original--apparently has trouble copying large files on Windows
Magic number for Windows, 64MB-32KB)
int maxcount = (64 * 1024 * 1024)-(32 * 1024);
Long size = Inchannel.size ();
Long position = 0;
while (Position < size)
{
Position + = Inchannel.transferto (position, Maxcount, Outchannel);
}
}
Finally
{
if (Inchannel!= null)
{
Inchannel.close ();
}
if (Outchannel!= null)
{
Outchannel.close ();
}
}
}
8. Create thumbnails of pictures
private void Createthumbnail (string filename, int thumbwidth, int thumbheight, int quality, string outfilename)
Throws Interruptedexception, FileNotFoundException, IOException
{
Load image from filename
Image image = Toolkit.getdefaulttoolkit (). getImage (filename);
Mediatracker mediatracker = new Mediatracker (new Container ());
Mediatracker.addimage (image, 0);
Mediatracker.waitforid (0);
Use the ' to ' test for errors in this point:System.out.println (Mediatracker.iserrorany ());
Determine thumbnail size from WIDTH and HEIGHT
Double thumbratio = (double) thumbwidth/(double) thumbheight;
int imagewidth = Image.getwidth (null);
int imageheight = Image.getheight (null);
Double imageratio = (double) imagewidth/(double) imageheight;
if (Thumbratio < Imageratio) {
Thumbheight = (int) (thumbwidth/imageratio);
} else {
Thumbwidth = (int) (thumbheight * imageratio);
}
Draw original image to thumbnail image object and
Scale it to the new size On-the-fly
BufferedImage thumbimage = new BufferedImage (Thumbwidth, Thumbheight, Bufferedimage.type_int_rgb);
Graphics2D graphics2d = Thumbimage.creategraphics ();
Graphics2d.setrenderinghint (Renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear);
Graphics2d.drawimage (image, 0, 0, thumbwidth, thumbheight, NULL);
Save thumbnail image to Outfilename
Bufferedoutputstream out = new Bufferedoutputstream (new FileOutputStream (Outfilename));
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
JPEGEncodeParam param = Encoder.getdefaultjpegencodeparam (thumbimage);
Quality = Math.max (0, Math.min (quality, 100));
Param.setquality ((float) quality/100.0f, false);
Encoder.setjpegencodeparam (param);
Encoder.encode (Thumbimage);
Out.close ();
}
9. Create JSON-formatted data
Import Org.json.JSONObject;
...
...
Jsonobject json = new Jsonobject ();
Json.put ("City", "Mumbai");
Json.put ("Country", "India");
...
String output = json.tostring ();
...
10. Using Itext jar to generate PDF
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Java.util.Date;
Import com.lowagie.text.Document;
Import Com.lowagie.text.Paragraph;
Import Com.lowagie.text.pdf.PdfWriter;
public class Generatepdf {
public static void Main (string[] args) {
try {
OutputStream file = new FileOutputStream (new file ("C:test.pdf"));
Document document = new document ();
Pdfwriter.getinstance (document, file);
Document.open ();
Document.add (New Paragraph ("Hello Kiran"));
Document.add (New Paragraph () (New Date (). toString ()));
Document.close ();
File.close ();
catch (Exception e) {
E.printstacktrace ();
}
}
}
One. HTTP Proxy settings
System.getproperties (). Put ("Http.proxyhost", "Someproxyurl");
System.getproperties (). Put ("Http.proxyport", "Someproxyport");
System.getproperties (). Put ("Http.proxyuser", "someusername");
System.getproperties (). Put ("Http.proxypassword", "Somepassword");
12. Single Instance Singleton sample
public class Simplesingleton {
private static Simplesingleton singleinstance = new Simplesingleton ();
Marking default constructor Private
To avoid direct instantiation.
Private Simplesingleton () {
}
Get instance for class Simplesingleton
public static Simplesingleton getinstance () {
return singleinstance;
}
}
Another kind of implementation
public enum Simplesingleton {
INSTANCE;
public void dosomething () {
}
}
Call the "method" from Singleton:
SimpleSingleton.INSTANCE.doSomething ();
13. Screen-catching procedure
Import java.awt.Dimension;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import Java.awt.Toolkit;
Import Java.awt.image.BufferedImage;
Import Javax.imageio.ImageIO;
Import Java.io.File;
...
public void Capturescreen (String fileName) throws Exception {
Dimension screensize = Toolkit.getdefaulttoolkit (). Getscreensize ();
Rectangle screenrectangle = new Rectangle (screensize);
Robot Robot = new Robot ();
BufferedImage image = Robot.createscreencapture (Screenrectangle);
Imageio.write (Image, "PNG", New File (FileName));
}
...