1. Conversion of string and integer types to each other
String a= string.valueof (2); int i = Integer.parseint (a);
2. Add content to the end of the file
BufferedWriter out=null;try {out=new bufferedwriter (new FileWriter ("filename", true)); Out.write ("I am stringbuffer!");} catch (IOException e) {e.printstacktrace ();}
3. Get the name of the current method
String methodName = Thread.CurrentThread (). Getstacktrace () [1].getmethodname ();
4. Turn string to date and date to string
Date to string SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day"); String str = sdf.format (new Date ()); System.out.println (str); The string to the date of day : date = Sdf.parse (str); SYSTEM.OUT.PRINTLN (date);
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 ("Selec T sysdate from dual "); ResultSet rs = Ps.executequery (); while (Rs.next ()) {//Does the thing you do} rs.close (); Ps.close (); } public static void Main (string[] args) {oraclejdbctest test = new Oraclejdbctest (); Test.init (); Test.fetch (); } }
6. Util Java. Date turns into SQL. Date
Java.util.Date utildate = new Java.util.Date (); Java.sql.Date sqldate = new Java.sql.Date (Utildate.gettime ());
7. Fast file copy using NiO
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 have 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 . list files and directories
Import Java.io.file;import Java.io.filefilter;import Java.io.filenamefilter;public class Test {public static void main ( String[] args) {file Dir = new File ("f:\\ Hanshunping java"); String[] Children = dir.list (); if (children = = null) {//either Dir does not exist or was not a directory} else {for (int i=0; i < Children.length; i++) {//Get filename of file or directory String filename = children[i]; System.out.println (i+ "---" +filename); }}//It is also possible to filter the list of returned files. This example does no return any files that start with '. '. FilenameFilter filter = new FilenameFilter () {public Boolean accept (File dir, String name) {return!na Me.startswith ("."); } }; Children = dir.list (filter); The list of files can also be retrieved as File objects file[] files = dir.listfiles (); This filter is only returns directories FileFilter FileFilter = New FileFilter () {public boolean accept (file file) {return file.isdirectory (); } }; Files = dir.listfiles (FileFilter);} }
9. HTTP request to send generation data
try { url my_url = new URL ("link address"); BufferedReader br = new BufferedReader (New InputStreamReader (My_url.openstream ())); String strtemp = ""; while (null! = (strtemp = Br.readline ())) { System.out.println (strtemp); } } catch (Exception ex) { Ex.printstacktrace ();
Common Java codebase Summary