本文目錄一覽:
- 1、怎麼看別人的java代碼
- 2、如何讀JAVA源碼
- 3、JAVA如何閱讀代碼更高效?
- 4、怎樣高效的閱讀JavaWeb項目源代碼
- 5、給段最簡單的java代碼 讓我新手看一下
- 6、北大青鳥java培訓:提高代碼閱讀能力的技巧有哪些?
怎麼看別人的java代碼
看別人的代碼是一件比較痛苦的事情,如果代碼注釋多並且編寫很規則的話那就比較好,
如果有項目需求文檔或流程設計圖最好先看看系統的整體功能
然後根據項目的模塊劃分對整體有一個認知
再根據模塊找到對應的代碼模塊(需要注意模塊間的關聯)
根據DB設計說明查看業務表的關聯關係
最後根據項目文檔重新查看項目整體構成,這樣看印象比較深刻一些,千萬不要摳某一個功能的具體業務與實現,只需要知道模塊的大概業務,然後當需要的時候再進行細細研究某一功能
以上是我的經驗之談,希望對你有幫助。
如何讀JAVA源碼
本來不想回答,翻到下面那些答覆實在看不過去,就花點功夫整理下吧,希望對有人心能有幫助。
閱讀分析源代碼,一些有效的方法是:
1、閱讀源代碼的說明文檔和API文檔。
2、如果源代碼有用法示例或嚮導,先閱讀這個。
3、了解整個項目的模塊結構,可以按模塊進行閱讀。
4、隨時使用查找功能(或超鏈接)閱讀關聯類或關聯方法。
5、對於有疑問的地方,不妨寫幾行單元測試。
6、由淺入深,由易到難,多閱讀優秀的開源項目,代碼閱讀水平會突飛猛進。
JAVA如何閱讀代碼更高效?
個人經驗,
讀文件有4種方法,
1 按行讀
2 按規定大小位元組讀
3 按流讀
4 隨機讀取文件
我認為第3種是最好的,而且他是通吃的,
下面是我從網上找來的,你看看有用嗎?
====================================
前兩天用到讀寫文件的操作,上網搜了一些這方面的資料。很有用的。
java中多種方式讀文件
一、多種方式讀文件內容。
1、按位元組讀取文件內容
2、按字符讀取文件內容
3、按行讀取文件內容
4、隨機讀取文件內容
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 {
/**
* 以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
* @param fileName 文件的名
*/
public static void readFileByBytes(String fileName){
File file = new File(fileName);
InputStream in = null;
try {
System.out.println(“以位元組為單位讀取文件內容,一次讀一個位元組:”);
// 一次讀一個位元組
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(“以位元組為單位讀取文件內容,一次讀多個位元組:”);
//一次讀多個位元組
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
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) {
}
}
}
}
/**
* 以字符為單位讀取文件,常用於讀文本,數字等類型的文件
* @param fileName 文件名
*/
public static void readFileByChars(String fileName){
File file = new File(fileName);
Reader reader = null;
try {
System.out.println(“以字符為單位讀取文件內容,一次讀一個位元組:”);
// 一次讀一個字符
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(“以字符為單位讀取文件內容,一次讀多個位元組:”);
//一次讀多個字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
//讀入多個字符到字符數組中,charread為一次讀取字符數
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(“以行為單位讀取文件內容,一次讀一整行:”);
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次讀入一行,直到讀入null為文件結束
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) {
}
}
}
}
/**
* 隨機讀取文件內容
* @param fileName 文件名
*/
public static void readFileByRandomAccess(String fileName){
RandomAccessFile randomFile = null;
try {
System.out.println(“隨機讀取一段文件內容:”);
// 打開一個隨機訪問文件流,按只讀方式
randomFile = new RandomAccessFile(fileName, “r”);
// 文件長度,位元組數
long fileLength = randomFile.length();
// 讀文件的起始位置
int beginIndex = (fileLength 4) ? 4 : 0;
//將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
//一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
//將一次讀取的位元組數賦給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) {
}
}
}
}
/**
* 顯示輸入流中還剩的位元組數
* @param in
*/
private static void showAvailableBytes(InputStream in){
try {
System.out.println(“當前位元組輸入流中的位元組數為:” + 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);
}
}
二、將內容追加到文件尾部
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 將內容追加到文件尾部
*/
public class AppendToFile {
/**
* A方法追加文件:使用RandomAccessFile
* @param fileName 文件名
* @param content 追加的內容
*/
public static void appendMethodA(String fileName,String content){
try {
// 打開一個隨機訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, “rw”);
// 文件長度,位元組數
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 {
//打開一個寫文件器,構造函數中的第二個參數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”);
//顯示文件內容
ReadFromFile.readFileByLines(fileName);
//按方法B追加文件
AppendToFile.appendMethodB(fileName, content);
AppendToFile.appendMethodB(fileName, “append end. n”);
//顯示文件內容
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();
}
讀文件中內容
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);//將文件寫入服務器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
怎樣高效的閱讀JavaWeb項目源代碼
首先要理清楚代碼結構和業務結構(應該有些文檔或者大的流程圖),這是閱讀具體代碼的前提。
閱讀Java web項目的代碼:
你需要找到
View層的代碼:前端頁面、圖片、資源文件都在其中。
Controller層的代碼:控制試圖與模型層以及數據傳遞。
Service層的代碼:業務邏輯。
Dao層的代碼:數據庫訪問邏輯。
從web.xml – appcontext.xml – xxx
給段最簡單的java代碼 讓我新手看一下
最簡單的java代碼肯定就是這個了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print(“Hello world”);
}
}
「hello world」就是應該是所有學java的新手看的第一個代碼了。如果是零基礎的新手朋友們可以來我們的java實驗班試聽,有免費的試聽課程幫助學習java必備基礎知識,有助教老師為零基礎的人提供個人學習方案,學習完成後有考評團進行專業測試,幫助測評學員是否適合繼續學習java,15天內免費幫助來報名體驗實驗班的新手快速入門java,更好的學習java!
北大青鳥java培訓:提高代碼閱讀能力的技巧有哪些?
對於學習軟件開發的人來說,學會閱讀源代碼是非常重要的,然而很多人並不具備閱讀源代碼的能力。
很多人不喜歡閱讀源代碼,認為這是非常無聊的事情。
如果不會閱讀源代碼,對於後面寫代碼是非常困難的,很多開發人員主要把重心放在寫代碼上,反而忽略代碼的閱讀。
閱讀代碼是非常關鍵的,下面天津電腦培訓為大家介紹閱讀代碼的技巧。
1、學會運行代碼運行代碼是閱讀代碼的第一步,這樣能夠了解關於項目的很多細節,並且了解怎麼進行運行,掌握庫的使用方法。
這樣是了解一個項目最好的方法,如果能夠自己了解和編寫相關的項目,這樣對於使用框架和庫會有自己的想法。
2、找到高層次的邏輯當您開始閱讀項目的代碼時,會涉及到每個細節。
相反的,你還需要掌握高層次結構,從這個地方找到入口點,在很多大型項目開發中都可以使用這種方法。
如果是進行web程序開發,那麼天津IT培訓建議應該查看不同的包,例如存儲業務邏輯的位置,存儲UI代碼的位置,控制器所在的位置等等。
3、了解和使用工具很多工具都可以有助於源代碼閱讀,並且對可視化代碼有很大的幫助。
在使用過程中,天津IT培訓認為IntelliJIdea工具能夠導航源代碼,允許使用單詞的一部分,甚至單詞的縮寫進行搜索。
您還應該學習鍵盤的快捷鍵。
使用鼠標導航源代碼可能會非常無聊和緩慢,鍵盤快捷鍵可以更快的進行跳轉。
4、了解語言更深入地了解特定語言有助於提高您的代碼閱讀技能。
每種語言都有自己的約定,樣式和語法。
這些知識可以幫助您快速熟悉特定代碼。
其中天津電腦培訓發現在Java語言中,方法名稱以小寫字母開頭,而在C#語言中,方法名稱以大寫字母開頭。
了解這種差異可以幫助你從源代碼中找到識別方法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/185953.html