在大家開(kāi)發(fā)java程序的時(shí)候,往往都會(huì)用到一些代碼段,而這些代碼都可以很好的幫助程序員開(kāi)發(fā)程序,那接下來(lái),我們就來(lái)給大家分享一些java編程代碼。大家可以參考以下。
1.向文件末尾添加內(nèi)容
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(); } }
2.得到當(dāng)前方法的名字
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
3. 轉(zhuǎn)字符串到日期
java.util.Date = java.text.DateFormat.getDateInstance().parse(date String);
//或者是:
SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" ); Date date = format.parse( myString );
4.把 Java util.Date 轉(zhuǎn)成 sql.Date
java.util.Date utilDate = new java.util.Date(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
5. 使用NIO進(jìn)行快速的文件拷貝
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(); } } }
6.抓屏程序
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)); } ...
7.把 Array 轉(zhuǎn)換成 Map
import java.util.Map; import org.apache.commons.lang.ArrayUtils; public class Main { public static void main(String[] args) { String[][] countries = { { "United States" , "New York" } , { "United Kingdom" , "London" } , { "Netherland" , "Amsterdam" } , { "Japan" , "Tokyo" } , { "France" , "Paris" } }; Map countryCapitals = ArrayUtils.toMap(countries); System.out.println("Capital of Japan is " + countryCapitals.get("Japan")); System.out.println("Capital of France is " + countryCapitals.get("France")); } }
8.單實(shí)例Singleton 示例
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; } }
這些都是在實(shí)際工作中經(jīng)常用到的代碼,大家可以將這些收藏,方便以后開(kāi)發(fā)程序的時(shí)候拿出來(lái)使用!最后大家如果想要了解更多java初識(shí)知識(shí),敬請(qǐng)關(guān)注賦能網(wǎng)。
本文鏈接:
本文章“java編程代碼有哪些?java編程代碼大全”已幫助 99 人
免責(zé)聲明:本信息由用戶發(fā)布,本站不承擔(dān)本信息引起的任何交易及知識(shí)產(chǎn)權(quán)侵權(quán)的法律責(zé)任!
本文由賦能網(wǎng) 整理發(fā)布。了解更多培訓(xùn)機(jī)構(gòu)》培訓(xùn)課程》學(xué)習(xí)資訊》課程優(yōu)惠》課程開(kāi)班》學(xué)校地址等機(jī)構(gòu)信息,可以留下您的聯(lián)系方式,讓課程老師跟你詳細(xì)解答:
咨詢熱線:4008-569-579