初學(xué)java的時候,我們不會寫代碼,其實(shí)這時候不妨趁有空的時候多背一些java代碼,這樣在進(jìn)行java項(xiàng)目的時候,說不定還能用得了呢!那java初學(xué)者必背代碼有哪些?下面來我們就來給大家講解一下。
1. 字符串有整型的相互轉(zhuǎn)換
String a = String.valueOf(2); //integer to numeric string int i = Integer.parseInt(a); //numeric string to an int
2. 向文件末尾添加內(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(); } }
3. 得到當(dāng)前方法的名字
String methodName = Thread.currentThread() .getStackTrace()[1].getMethodName();
4. 轉(zhuǎn)字符串到日期
java.util.Date = java.text.DateFormat.getDateInstance() .parse(date String); 或者是: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse(myString);
5、類的屬性和方法
public class Liwl { public String name = "liwanliang"; public Integer age = 30; public static void main(String[] args) { Liwl liwl01 = new Liwl(); //liwl01.name = "hello,world" System.out.println(liwl01.age) } } //通過liwl01.name = "hello,world"的語句,無法保證類的安全性,不太滿足類的封裝
6、列出文件和目錄
File dir = new File("directoryName"); String children = dir.list; if (children == ) { // Either dir does not exist or is not a directory } else { for (int i = 0; i < children.length; i++) { // Get filename of file or directory String filename = children[i]; } } // It is also possible to filter the list of returned files. // This example does not return any files that start with `.. FilenameFilter filter = new FilenameFilter { public boolean accept(File dir, String name) { return !name.startsWith("."); } }; children = dir.list(filter); // The list of files can also be retrieved as File objects File files = dir.listFiles; // This filter only returns directories FileFilter fileFilter = new FileFilter { public boolean accept(File file) { return file.isDirectory; } }; files = dir.listFiles(fileFilter);
多背java代碼其實(shí)是積累java知識的一種方式,很多java開發(fā)語句我們不會寫的時候,就可以將代碼背下來,然后靈活應(yīng)用!最后大家如果想要了解更多初識java知識,敬請關(guān)注賦能網(wǎng)。
本文鏈接:
本文章“java初學(xué)者必背代碼有哪些?java初學(xué)者必背代碼”已幫助 53 人
免責(zé)聲明:本信息由用戶發(fā)布,本站不承擔(dān)本信息引起的任何交易及知識產(chǎn)權(quán)侵權(quán)的法律責(zé)任!
本文由賦能網(wǎng) 整理發(fā)布。了解更多培訓(xùn)機(jī)構(gòu)》培訓(xùn)課程》學(xué)習(xí)資訊》課程優(yōu)惠》課程開班》學(xué)校地址等機(jī)構(gòu)信息,可以留下您的聯(lián)系方式,讓課程老師跟你詳細(xì)解答:
咨詢熱線:4008-569-579