欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎樣閱讀java代碼 如何閱讀jdk源碼

JAVA如何閱讀代碼更高效?

個(gè)人經(jīng)驗(yàn),

成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作服務(wù)團(tuán)隊(duì)是一支充滿著熱情的團(tuán)隊(duì),執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時(shí)竭誠為客戶提供服務(wù)是我們的理念。成都創(chuàng)新互聯(lián)把每個(gè)網(wǎng)站當(dāng)做一個(gè)產(chǎn)品來開發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!

讀文件有4種方法,

1 按行讀

2 按規(guī)定大小字節(jié)讀

3 按流讀

4 隨機(jī)讀取文件

我認(rèn)為第3種是最好的,而且他是通吃的,

下面是我從網(wǎng)上找來的,你看看有用嗎?

====================================

前兩天用到讀寫文件的操作,上網(wǎng)搜了一些這方面的資料。很有用的。

java中多種方式讀文件

一、多種方式讀文件內(nèi)容。

1、按字節(jié)讀取文件內(nèi)容

2、按字符讀取文件內(nèi)容

3、按行讀取文件內(nèi)容

4、隨機(jī)讀取文件內(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)容,一次讀一個(gè)字節(jié):");

// 一次讀一個(gè)字節(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)容,一次讀多個(gè)字節(jié):");

//一次讀多個(gè)字節(jié)

byte[] tempbytes = new byte[100];

int byteread = 0;

in = new FileInputStream(fileName);

ReadFromFile.showAvailableBytes(in);

//讀入多個(gè)字節(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)容,一次讀一個(gè)字節(jié):");

// 一次讀一個(gè)字符

reader = new InputStreamReader(new FileInputStream(file));

int tempchar;

while ((tempchar = reader.read()) != -1){

//對于windows下,rn這兩個(gè)字符在一起時(shí),表示一個(gè)換行。

//但如果這兩個(gè)字符分開顯示時(shí),會換兩次行。

//因此,屏蔽掉r,或者屏蔽n。否則,將會多出很多空行。

if (((char)tempchar) != 'r'){

System.out.print((char)tempchar);

}

}

reader.close();

} catch (Exception e) {

e.printStackTrace();

}

try {

System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié):");

//一次讀多個(gè)字符

char[] tempchars = new char[30];

int charread = 0;

reader = new InputStreamReader(new FileInputStream(fileName));

//讀入多個(gè)字符到字符數(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) {

}

}

}

}

/**

* 隨機(jī)讀取文件內(nèi)容

* @param fileName 文件名

*/

public static void readFileByRandomAccess(String fileName){

RandomAccessFile randomFile = null;

try {

System.out.println("隨機(jī)讀取一段文件內(nèi)容:");

// 打開一個(gè)隨機(jī)訪問文件流,按只讀方式

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個(gè)字節(jié),如果文件內(nèi)容不足10個(gè)字節(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 {

// 打開一個(gè)隨機(jī)訪問文件流,按讀寫方式

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è)寫文件器,構(gòu)造函數(shù)中的第二個(gè)參數(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);//建立一個(gè)上傳文件的輸出流

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);

}

怎樣高效的閱讀JavaWeb項(xiàng)目源代碼

首先要理清楚代碼結(jié)構(gòu)和業(yè)務(wù)結(jié)構(gòu)(應(yīng)該有些文檔或者大的流程圖),這是閱讀具體代碼的前提。

閱讀Java?web項(xiàng)目的代碼:

你需要找到

View層的代碼:前端頁面、圖片、資源文件都在其中。

Controller層的代碼:控制試圖與模型層以及數(shù)據(jù)傳遞。

Service層的代碼:業(yè)務(wù)邏輯。

Dao層的代碼:數(shù)據(jù)庫訪問邏輯。

從web.xml?-?appcontext.xml?-?xxx

怎么看懂java代碼?

想要看到都能java代碼,需要了解編程的基礎(chǔ)知識,變量,表達(dá)式,程序執(zhí)行結(jié)構(gòu),邏輯判斷等等。最重要是要學(xué)會如何調(diào)試代碼,慢慢練習(xí),熟練了自然就會了。

java代碼看不懂怎么辦?

看代碼前需要先了解那塊代碼是做什么的,這樣才能跟得上代碼邏輯。如果知道代碼功能,還是不懂的話,那么看代碼行數(shù)。

1、低于20行的代碼看不懂,你需要鞏固java基礎(chǔ)

2、超過20行的話,需要仔細(xì)理解代碼意圖,另外需要了解更多的java API以及開源代碼

一般看別人的代碼是很痛苦的,java高手也有看不懂的時(shí)候,譬如代碼本身就寫的爛,邏輯不明確。

Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強(qiáng)大和簡單易用兩個(gè)特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。

網(wǎng)頁標(biāo)題:怎樣閱讀java代碼 如何閱讀jdk源碼
網(wǎng)頁URL:http://chinadenli.net/article38/dohgdpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)響應(yīng)式網(wǎng)站全網(wǎng)營銷推廣微信公眾號云服務(wù)器移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司