試想一下,一個(gè)沒有一句注釋的程序源碼,怎么讀,一個(gè)程序要上萬條代碼不可能全部記得住哪一塊是什么用,而且一個(gè)項(xiàng)目也不會(huì)是一個(gè)人獨(dú)自完成,那樣效率太低,多人合作就要給別人說明,每一塊是什么用,方便他人使用,方便自己更正,而這些說明文字就是注釋,注釋不會(huì)被執(zhí)行,不影響運(yùn)行結(jié)果。
臺(tái)山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
Java中代碼的注釋有三種:
// 注釋一行?
/* ...... */ 注釋若干行?
/** ...... */ 注釋若干行,并寫入 javadoc 文檔
前兩種比較容易理解,至于第三種,你學(xué)習(xí)Java看的api文檔就是javadoc程序根據(jù)第三種的注釋生成的。
擴(kuò)展資料
注釋就是對(duì)代碼的解釋和說明,其目的是讓人們能夠更加輕松地了解代碼。注釋是編寫程序時(shí),寫程序的人給一個(gè)語句、程序段、函數(shù)等的解釋或提示,能提高程序代碼的可讀性。
注釋是用來給開發(fā)者或者是軟件后續(xù)的維護(hù)者來看的。一般是用來描述這一段代碼或者是程序做了些什么樣的事情。實(shí)現(xiàn)了哪些功能,java里面注釋代碼,可以分為行注釋和塊注釋,方法注釋等。
行注釋就是在需要注釋的代碼前加 //
塊注釋則是用 /* 和 */ 把文本段落框起來即可
想要自動(dòng)添加注釋,首先需要對(duì)eclipse進(jìn)行一些設(shè)置。
打開Eclipse,然后點(diǎn)擊-windows-preferences-Java-code styles-code templates-comments-types-點(diǎn)擊右側(cè)的edit進(jìn)行編輯,在打開的編輯窗口中修改下面的文字
/**
* @author ${user}
*
* ${tags}
*/
替換成
/**
* @author 你的名字
*
* ${tags}
*/
上面的操作對(duì)應(yīng)的圖片是這樣的:
這樣作者就修改成你的名字了,更多的信息可以點(diǎn)擊本編輯窗口上面的insert varibles按鈕,會(huì)彈出一些文件名稱,項(xiàng)目名稱等參數(shù),你可以選擇性地添加到你的注視中。
然后在你的類中,你只要敲入/**然后換行,注釋就自動(dòng)滴添加到你的代碼中了~希望能幫上你的忙~^_^
追問:
這只是文檔前的版權(quán)聲明注釋,如果要在文檔中對(duì)代碼添加注釋,里面帶上修改時(shí)間,如何弄
回答:
這樣弄哈~
1.開始還是打開Eclipse,然后點(diǎn)擊-windows-preferences-Java-code styles-code templates-comments
2.下一步選擇methods,而不是types。然后edit,insert varibles,添加上date變量,長下面這個(gè)樣子。
然后你在你的函數(shù)上面同樣是敲入/**回車,就出現(xiàn)了修改的日期了
補(bǔ)充:
對(duì)于那個(gè)行首出現(xiàn)注釋的問題是java的代碼風(fēng)格的問題,可以這樣改
打開Eclipse,然后點(diǎn)擊-windows-preferences-Java-code styles-formatter
-edit-comment 的標(biāo)簽頁-never indent line comments on first column
這樣,注釋的//就會(huì)縮進(jìn)了,而不是頂在行首了~
Java文件操作大全
1.創(chuàng)建文件夾
//import java.io.*;
File myFolderPath = new File(%%1);
try {
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目錄操作出錯(cuò)");
e.printStackTrace();
}
2.創(chuàng)建文件
//import java.io.*;
File myFilePath = new File(%%1);
try {
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(%%2);
resultFile.close();
}
catch (Exception e) {
System.out.println("新建文件操作出錯(cuò)");
e.printStackTrace();
}
3.刪除文件
//import java.io.*;
File myDelFile = new File(%%1);
try {
myDelFile.delete();
}
catch (Exception e) {
System.out.println("刪除文件操作出錯(cuò)");
e.printStackTrace();
}
4.刪除文件夾
//import java.io.*;
File delFolderPath = new File(%%1);
try {
delFolderPath.delete(); //刪除空文件夾
}
catch (Exception e) {
System.out.println("刪除文件夾操作出錯(cuò)");
e.printStackTrace();
}
5.刪除一個(gè)文件下夾所有的文件夾
//import java.io.*;
File delfile=new File(%%1);
File[] files=delfile.listFiles();
for(int i=0;ifiles.length;i++){
if(files[i].isDirectory()){
files[i].delete();
}
}
6.清空文件夾
//import java.io.*;
File delfilefolder=new File(%%1);
try {
if (!delfilefolder.exists()) {
delfilefolder.delete();
}
delfilefolder.mkdir();
}
catch (Exception e) {
System.out.println("清空目錄操作出錯(cuò)");
e.printStackTrace();
}
7.讀取文件
//import java.io.*;
// 逐行讀取數(shù)據(jù)
FileReader fr = new FileReader(%%1);
BufferedReader br = new BufferedReader(fr);
String %%2 = br.readLine();
while (%%2 != null) {
%%3
%%2 = br.readLine();
}
br.close();
fr.close();
8.寫入文件
//import java.io.*;
// 將數(shù)據(jù)寫入文件
try {
FileWriter fw = new FileWriter(%%1);
fw.write(%%2);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
9.寫入隨機(jī)文件
//import java.io.*;
try {
RandomAcessFile logFile=new RandomAcessFile(%%1,"rw");
long lg=logFile.length();
logFile.seek(%%2);
logFile.writeByte(%%3);
}catch(IOException ioe){
System.out.println("無法寫入文件:"+ioe.getMessage());
}
10.讀取文件屬性
//import java.io.*;
// 文件屬性的取得
File af = new File(%%1);
if (af.exists()) {
System.out.println(f.getName() + "的屬性如下: 文件長度為:" + f.length());
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目錄" : "不是目錄");
System.out.println(f.canRead() ? "可讀取" : "不");
System.out.println(f.canWrite() ? "是隱藏文件" : "");
System.out.println("文件夾的最后修改日期為:" + new Date(f.lastModified()));
} else {
System.out.println(f.getName() + "的屬性如下:");
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目錄" : "不是目錄");
System.out.println(f.canRead() ? "可讀取" : "不");
System.out.println(f.canWrite() ? "是隱藏文件" : "");
System.out.println("文件的最后修改日期為:" + new Date(f.lastModified()));
}
if(f.canRead()){
%%2
}
if(f.canWrite()){
%%3
}
11.寫入屬性
//import java.io.*;
File filereadonly=new File(%%1);
try {
boolean b=filereadonly.setReadOnly();
}
catch (Exception e) {
System.out.println("拒絕寫訪問:"+e.printStackTrace());
}
12.枚舉一個(gè)文件夾中的所有文件
//import java.io.*;
//import java.util.*;
LinkedListString folderList = new LinkedListString();
folderList.add(%%1);
while (folderList.size() 0) {
File file = new File(folderList.peek());
folderList.removeLast();
File[] files = file.listFiles();
ArrayListFile fileList = new ArrayListFile();
for (int i = 0; i files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
%%2=f.getAbsoluteFile();
%%3
}
}
13.復(fù)制文件夾
//import java.io.*;
//import java.util.*;
LinkedListString folderList = new LinkedListString();
folderList.add(%%1);
LinkedListString folderList2 = new LinkedListString();
folderList2.add(%%2+ %%1.substring(%%1.lastIndexOf("\\")));
while (folderList.size() 0) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夾不存在 則建立新文件夾
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = 0; i file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator
+ file[i]);
} else {
temp = new File(folderList.peek() + File.separator
+ file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator
+ (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {// 如果是子文件夾
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek()
+ File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
//System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
}
14.復(fù)制一個(gè)文件夾下所有的文件夾到另一個(gè)文件夾下
//import java.io.*;
//import java.util.*;
File copyfolders=new File(%%1);
File[] copyfoldersList=copyfolders.listFiles();
for(int k=0;kcopyfoldersList.length;k++){
if(copyfoldersList[k].isDirectory()){
ArrayListStringfolderList=new ArrayListString();
folderList.add(copyfoldersList[k].getPath());
ArrayListStringfolderList2=new ArrayListString();
folderList2.add(%%2+"/"+copyfoldersList[k].getName());
for(int j=0;jfolderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夾不存在 則建立新文件夾
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" +
(temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夾
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)");
e.printStackTrace();
}
}
}
}
15.移動(dòng)文件夾
//import java.io.*;
//import java.util.*;
LinkedListString folderList = new LinkedListString();
folderList.add(%%1);
LinkedListString folderList2 = new LinkedListString();
folderList2.add(%%2 + %%1.substring(%%1.lastIndexOf("\\")));
while (folderList.size() 0) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夾不存在 則建立新文件夾
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = 0; i file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator
+ file[i]);
} else {
temp = new File(folderList.peek() + File.separator
+ file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator
+ (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
if (!temp.delete())
System.out.println("刪除單個(gè)文件操作出錯(cuò)!");
}
if (temp.isDirectory()) {// 如果是子文件夾
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek()
+ File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
// System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
}
File f = new File(%%1);
if (!f.delete()) {
for (File file : f.listFiles()) {
if (file.list().length == 0) {
System.out.println(file.getPath());
file.delete();
}
}
}
16.移動(dòng)一個(gè)文件夾下所有的文件夾到另一個(gè)目錄下
//import java.io.*;
//import java.util.*;
File movefolders=new File(%%1);
File[] movefoldersList=movefolders.listFiles();
for(int k=0;kmovefoldersList.length;k++){
if(movefoldersList[k].isDirectory()){
ArrayListStringfolderList=new ArrayListString();
folderList.add(movefoldersList[k].getPath());
ArrayListStringfolderList2=new ArrayListString();
folderList2.add(%%2+"/"+movefoldersList[k].getName());
for(int j=0;jfolderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夾不存在 則建立新文件夾
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" +
(temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
temp.delete();
}
if(temp.isDirectory()){//如果是子文件夾
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)");
e.printStackTrace();
}
}
movefoldersList[k].delete();
}
}
17.以一個(gè)文件夾的框架在另一個(gè)目錄創(chuàng)建文件夾和空文件
//import java.io.*;
//import java.util.*;
boolean b=false;//不創(chuàng)建空文件
ArrayListStringfolderList=new ArrayListString();
folderList.add(%%1);
ArrayListStringfolderList2=new ArrayListString();
folderList2.add(%%2);
for(int j=0;jfolderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夾不存在 則建立新文件夾
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
if (b) temp.createNewFile();
}
if(temp.isDirectory()){//如果是子文件夾
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)");
e.printStackTrace();
}
}
18.復(fù)制文件
//import java.io.*;
int bytesum = 0;
int byteread = 0;
File oldfile = new File(%%1);
try {
if (oldfile.exists()) { //文件存在時(shí)
FileInputStream inStream = new FileInputStream(oldfile); //讀入原文件
FileOutputStream fs = new FileOutputStream(new File(%%2,oldfile.getName()));
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字節(jié)數(shù) 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("復(fù)制單個(gè)文件操作出錯(cuò)");
e.printStackTrace();
}
19.復(fù)制一個(gè)文件夾下所有的文件到另一個(gè)目錄
//import java.io.*;
File copyfiles=new File(%%1);
File[] files=copyfiles.listFiles();
for(int i=0;ifiles.length;i++){
if(!files[i].isDirectory()){
int bytesum = 0;
int byteread = 0;
try {
InputStream inStream = new FileInputStream(files[i]); //讀入原文件
FileOutputStream fs = new FileOutputStream(new File(%%2,files[i].getName());
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字節(jié)數(shù) 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
} catch (Exception e) {
System.out.println("復(fù)制單個(gè)文件操作出錯(cuò)");
e.printStackTrace();
}
}
}
20.提取擴(kuò)展名
String %%2=%%1.substring(%%1.lastIndexOf(".")+1);
(1)單行注釋:以“ // ”開頭后面接所要加的說明的內(nèi)容。如下面所示: //定義變量a int a = 10; //定義變量b int b = 20;上面的語句中,在編譯的過程就會(huì)直接略過注釋,只會(huì)編譯 int a = 10 和 int b = 20這兩句。由此可見注釋只是起著說明的作用。
(2)多行注釋:以“/*”開頭,以“*/”結(jié)尾。 假設(shè)當(dāng)你要說明你所寫的代碼的功能時(shí)。要說明的內(nèi)容有很多。如果全部放在同一行會(huì)顯得很難看。所以一般會(huì)用多行來寫,如下所示// 說明//說明//說明//說明以上是用四個(gè)單行注釋來注釋四行說明。但如果有10行說明就要按十個(gè)“//”這顯示很麻煩,所以這時(shí)就可采用多行注釋。上面的可改成:/*說明說明說明說明*/也可以這樣/* 說明 說明 說明 說明 */
(3)文檔注釋:以“/**”開頭,以“*/”結(jié)尾。文檔注釋主要是生成文檔的。
網(wǎng)站名稱:簡單java代碼加注解,java代碼注釋
本文URL:http://sd-ha.com/article22/hcogcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、品牌網(wǎng)站建設(shè)、電子商務(wù)、企業(yè)建站、網(wǎng)站收錄、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)