最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 讀取文件方法大全

 更新時間:2014年11月01日 13:31:00   投稿:mdxy-dxy  
這篇文章主要介紹了Java 讀取文件方法大全,需要的朋友可以參考下

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

public class ReadFromFile {

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) {
}
}
}
}

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

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下,\r\n這兩個字符在一起時,表示一個換行。
// 但如果這兩個字符分開顯示時,會換兩次行。
// 因此,屏蔽掉\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; i < charread; 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) {
}
}
}
}

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

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) {
}
}
}
}

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

public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("隨機(jī)讀取一段文件內(nèi)容:");
// 打開一個隨機(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個字節(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) {
}
}
}
}

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

5、將內(nèi)容追加到文件尾部

public class AppendToFile {

public static void appendMethodA(String fileName, String content) {
try {
// 打開一個隨機(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();
}
}


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

相關(guān)文章

  • SpringIOC DI循環(huán)依賴實(shí)例詳解

    SpringIOC DI循環(huán)依賴實(shí)例詳解

    這篇文章主要介紹了SpringIOC——DI循環(huán)依賴,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • SQL注入攻擊及其在SpringBoot中使用MyBatisPlus的防范策略的方法

    SQL注入攻擊及其在SpringBoot中使用MyBatisPlus的防范策略的方法

    本文介紹了如何使用SpringBoot整合JavaDeeplearning4j構(gòu)建一個文本摘要生成系統(tǒng),該系統(tǒng)能夠自動從長篇文本中提取關(guān)鍵信息,生成簡潔的摘要,幫助用戶快速了解文本的主要內(nèi)容,系統(tǒng)使用LSTM神經(jīng)網(wǎng)絡(luò)模型進(jìn)行訓(xùn)練,并通過SpringBoot創(chuàng)建RESTful?API進(jìn)行調(diào)用
    2024-11-11
  • springboot2.0和springcloud Finchley版項目搭建(包含eureka,gateWay,F(xiàn)reign,Hystrix)

    springboot2.0和springcloud Finchley版項目搭建(包含eureka,gateWay,F(xiàn)re

    這篇文章主要介紹了springboot2.0和springcloud Finchley版項目搭建(包含eureka,gateWay,F(xiàn)reign,Hystrix),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • java String類功能、原理與應(yīng)用案例【統(tǒng)計、判斷、轉(zhuǎn)換等】

    java String類功能、原理與應(yīng)用案例【統(tǒng)計、判斷、轉(zhuǎn)換等】

    這篇文章主要介紹了java String類功能、原理與應(yīng)用案例,結(jié)合實(shí)例形式詳細(xì)分析了java String類的基本功能、構(gòu)造方法,以及使用String類實(shí)現(xiàn)統(tǒng)計、判斷、轉(zhuǎn)換等功能相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • 使用mybatis攔截器處理敏感字段

    使用mybatis攔截器處理敏感字段

    這篇文章主要介紹了mybatis攔截器處理敏感字段方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 五分鐘帶你了解Java的接口數(shù)據(jù)校驗

    五分鐘帶你了解Java的接口數(shù)據(jù)校驗

    這篇文章主要介紹了五分鐘帶你了解Java的接口數(shù)據(jù)校驗,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 在spring?boot3中使用native?image的最新方法

    在spring?boot3中使用native?image的最新方法

    這篇文章主要介紹了在spring?boot3中使用native?image?,今天我們用具體的例子來給大家演示一下如何正確的將spring boot3的應(yīng)用編譯成為native image,需要的朋友可以參考下
    2023-01-01
  • Java進(jìn)程內(nèi)緩存框架EhCache詳解

    Java進(jìn)程內(nèi)緩存框架EhCache詳解

    這篇文章主要介紹了Java進(jìn)程內(nèi)緩存框架EhCache,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-12-12
  • Java實(shí)現(xiàn)公眾號功能、關(guān)注及消息推送實(shí)例代碼

    Java實(shí)現(xiàn)公眾號功能、關(guān)注及消息推送實(shí)例代碼

    公眾號開發(fā)近些年是一個比較熱門的方向,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)公眾號功能、關(guān)注及消息推送的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • springboot連接neo4j報錯的解決方案

    springboot連接neo4j報錯的解決方案

    這篇文章主要介紹了springboot連接neo4j報錯的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評論

石棉县| 吉安市| 无为县| 安丘市| 铁岭县| 宣城市| 吕梁市| 长岛县| 旌德县| 津南区| 叶城县| 镇原县| 苍梧县| 台安县| 保亭| 洪洞县| 阿克苏市| 五寨县| 呼图壁县| 和田县| 双城市| 北川| 南康市| 河间市| 浦县| 西青区| 鄄城县| 昆明市| 连云港市| 远安县| 花垣县| 广东省| 洱源县| 连山| 开封市| 咸丰县| 汶上县| 沂源县| 大名县| 河北区| 潼南县|