很多初學(xué)者包括一些有經(jīng)驗的程序員,在敲完代碼的最后一個字符后,馬上開始編譯和運行,迫不急待的想看到自己的工作成果。

成都創(chuàng)新互聯(lián)成立于2013年,我們提供高端重慶網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站定制、網(wǎng)絡(luò)營銷推廣、小程序制作、微信公眾號開發(fā)、seo優(yōu)化服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項目落地,為房屋鑒定企業(yè)提供源源不斷的流量和訂單咨詢。
快速反饋有助于滿足自己的成就感,但是同時也會帶來一些問題:讓編譯器幫你檢查語法錯誤可以省些時間,但程序員往往太專注這些錯誤了,以為改完這些錯誤就萬事大吉了。
其實不然,很多錯誤編譯器是發(fā)現(xiàn)不了的,像內(nèi)存錯誤和線程死鎖等等,這些錯誤可能逃過簡單的測試而遺留在代碼中,直到集成測試或者軟件發(fā)布之后才暴露出來,那時就要花更大代價去修改它們了。
修改完編譯錯誤之后就是運行程序了,運行起來有錯誤,就輪到調(diào)試器上場了。
花了不少時間去調(diào)試,發(fā)現(xiàn)無非是些低級錯誤,或許你會自責(zé)自己粗心大意,但是下次可能還是犯同樣的錯誤。
更嚴(yán)重的是這種debugfix的方法,往往是頭痛醫(yī)頭腳痛醫(yī)腳,導(dǎo)致低質(zhì)量的軟件。
讓編譯器幫你檢查語法錯誤,讓調(diào)試器幫你查BUG,這是天經(jīng)地義的事,但這確實是又慢又爛的方法。
就像你要到離家東邊1000米的地方開會,結(jié)果你往西邊走,又是坐車又是搭飛機,花了一周時間,也繞著地球轉(zhuǎn)了一周,終于到了會議室,你還大發(fā)感慨說,現(xiàn)代的交通工具真是發(fā)達(dá)啊。
其實你往東走,走路也只要十多分鐘就到了。
不管你的調(diào)試技巧有多高,都不如一次性寫好更高效。
下面是我在閱讀自己代碼時的一些方法:檢查常見錯誤第一遍閱讀時主要關(guān)注語法錯誤、代碼排版和命名規(guī)則等等問題,只要看不順眼就修改它們。
讀完之后,你的代碼很少有低級錯誤,看起來也比較干凈清爽。
第二遍重點關(guān)注常見編程錯誤,比如內(nèi)存泄露和可能的越界訪問,變量沒有初始化,函數(shù)忘記返回值等等,在后面的章節(jié)中,我會介紹這些常見錯誤,避免這些錯誤可以為你省大量的時間。
如果有時間,在測試完成之后,還可以考慮是否有更好的實現(xiàn)方法,甚至嘗試重新去實現(xiàn)它們。
說了讀者可能不相信,在學(xué)習(xí)編程的前幾年,我經(jīng)常重寫整個模塊,只我覺得能做得更好,能驗證我的一些想法,或提高我的編程能力,即使連續(xù)幾天加班到晚上十一點,我也要重寫它們。
模擬計算機執(zhí)行常見錯誤是比較死的東西,按照檢查列表一條一條的做就行了。
有些邏輯通常不是這么直觀的,這時可以自己模擬計算機去執(zhí)行,假想你自己是計算機,讀入這些代碼時你會怎么處理。
北大青鳥認(rèn)為這種方法能有效的完善我們的思路,考慮不同的輸入數(shù)據(jù),各種邊界值,這能幫助我們想到一些沒有處理的情況,讓程序的邏輯更嚴(yán)謹(jǐn)。
首先要理清楚代碼結(jié)構(gòu)和業(yè)務(wù)結(jié)構(gòu)(應(yīng)該有些文檔或者大的流程圖),這是閱讀具體代碼的前提。
閱讀Java?web項目的代碼:
你需要找到
View層的代碼:前端頁面、圖片、資源文件都在其中。
Controller層的代碼:控制試圖與模型層以及數(shù)據(jù)傳遞。
Service層的代碼:業(yè)務(wù)邏輯。
Dao層的代碼:數(shù)據(jù)庫訪問邏輯。
從web.xml?-?appcontext.xml?-?xxx
個人經(jīng)驗,
讀文件有4種方法,
1 按行讀
2 按規(guī)定大小字節(jié)讀
3 按流讀
4 隨機讀取文件
我認(rèn)為第3種是最好的,而且他是通吃的,
下面是我從網(wǎng)上找來的,你看看有用嗎?
====================================
前兩天用到讀寫文件的操作,上網(wǎng)搜了一些這方面的資料。很有用的。
java中多種方式讀文件
一、多種方式讀文件內(nèi)容。
1、按字節(jié)讀取文件內(nèi)容
2、按字符讀取文件內(nèi)容
3、按行讀取文件內(nèi)容
4、隨機讀取文件內(nèi)容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class ReadFromFile {
/**
* 以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
* @param fileName 文件的名
*/
public static void readFileByBytes(String fileName){
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
// 一次讀一個字節(jié)
in = new FileInputStream(file);
int tempbyte;
while((tempbyte=in.read()) != -1){
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
//一次讀多個字節(jié)
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//讀入多個字節(jié)到字節(jié)數(shù)組中,byteread為一次讀入的字節(jié)數(shù)
while ((byteread = in.read(tempbytes)) != -1){
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null){
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件
* @param fileName 文件名
*/
public static void readFileByChars(String fileName){
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
// 一次讀一個字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1){
//對于windows下,rn這兩個字符在一起時,表示一個換行。
//但如果這兩個字符分開顯示時,會換兩次行。
//因此,屏蔽掉r,或者屏蔽n。否則,將會多出很多空行。
if (((char)tempchar) != 'r'){
System.out.print((char)tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
//一次讀多個字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
//讀入多個字符到字符數(shù)組中,charread為一次讀取字符數(shù)
while ((charread = reader.read(tempchars))!=-1){
//同樣屏蔽掉r不顯示
if ((charread == tempchars.length)(tempchars[tempchars.length-1] != 'r')){
System.out.print(tempchars);
}else{
for (int i=0; icharread; i++){
if(tempchars[i] == 'r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行為單位讀取文件,常用于讀面向行的格式化文件
* @param fileName 文件名
*/
public static void readFileByLines(String fileName){
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次讀入一行,直到讀入null為文件結(jié)束
while ((tempString = reader.readLine()) != null){
//顯示行號
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 隨機讀取文件內(nèi)容
* @param fileName 文件名
*/
public static void readFileByRandomAccess(String fileName){
RandomAccessFile randomFile = null;
try {
System.out.println("隨機讀取一段文件內(nèi)容:");
// 打開一個隨機訪問文件流,按只讀方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件長度,字節(jié)數(shù)
long fileLength = randomFile.length();
// 讀文件的起始位置
int beginIndex = (fileLength 4) ? 4 : 0;
//將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
//一次讀10個字節(jié),如果文件內(nèi)容不足10個字節(jié),則讀剩下的字節(jié)。
//將一次讀取的字節(jié)數(shù)賦給byteread
while ((byteread = randomFile.read(bytes)) != -1){
System.out.write(bytes, 0, byteread);
}
} catch (IOException e){
e.printStackTrace();
} finally {
if (randomFile != null){
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**
* 顯示輸入流中還剩的字節(jié)數(shù)
* @param in
*/
private static void showAvailableBytes(InputStream in){
try {
System.out.println("當(dāng)前字節(jié)輸入流中的字節(jié)數(shù)為:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
二、將內(nèi)容追加到文件尾部
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 將內(nèi)容追加到文件尾部
*/
public class AppendToFile {
/**
* A方法追加文件:使用RandomAccessFile
* @param fileName 文件名
* @param content 追加的內(nèi)容
*/
public static void appendMethodA(String fileName,String content){
try {
// 打開一個隨機訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件長度,字節(jié)數(shù)
long fileLength = randomFile.length();
//將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e){
e.printStackTrace();
}
}
/**
* B方法追加文件:使用FileWriter
* @param fileName
* @param content
*/
public static void appendMethodB(String fileName, String content){
try {
//打開一個寫文件器,構(gòu)造函數(shù)中的第二個參數(shù)true表示以追加形式寫文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
String content = "new append!";
//按方法A追加文件
AppendToFile.appendMethodA(fileName, content);
AppendToFile.appendMethodA(fileName, "append end. n");
//顯示文件內(nèi)容
ReadFromFile.readFileByLines(fileName);
//按方法B追加文件
AppendToFile.appendMethodB(fileName, content);
AppendToFile.appendMethodB(fileName, "append end. n");
//顯示文件內(nèi)容
ReadFromFile.readFileByLines(fileName);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
寫入文件
try{
FileWriter fw=new FileWriter(SystemConfig.getRealPath()+"WEB-INF/url.txt");
fw.write("movie"+name);
fw.close();
}catch(IOException e){
e.printStackTrace();
}
讀文件中內(nèi)容
try{
FileReader fr = null;
fr = new FileReader(SystemConfig.getRealPath()+"WEB-INF/url.txt");
BufferedReader br=new BufferedReader(fr);
String Line = null;
Line = br.readLine();
while(Line!=null){
s=Line;
Line=null;
br.close();
fr.close();
}
}catch(IOException e1){
e1.printStackTrace();
}
上傳文件
try {
InputStream stream = getUpFile().getInputStream();//把文件讀入
OutputStream bos = new FileOutputStream(filePath + "movie" +name);//建立一個上傳文件的輸出流
int bytesRead = 0;
byte[] buffer = new byte[1026];
while ( (bytesRead = stream.read(buffer, 0, 1026)) != -1) {
bos.write(buffer, 0, bytesRead);//將文件寫入服務(wù)器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
網(wǎng)站標(biāo)題:怎么閱讀java代碼更快,如何讀懂java代碼
URL鏈接:http://chinadenli.net/article37/dsiiisj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、python、定制網(wǎng)站、Google、ChatGPT、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)