Java讀寫txt文件代碼實例
讀文件
// 讀取文件內(nèi)容 參數(shù)要完成路徑和文件名 String filePathName="D:/test/tgj/test1.txt";
private static List<String> ReadFileCon(String filePathName){
List<String> strList = new ArrayList<>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(filePathName));
String line = reader.readLine();
while (line != null) {
strList.add(line);
line = reader.readLine();// 繼續(xù)讀取下一行
}
reader.close();
return strList;
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件不存在");
return strList;
}
}如果想直接輸出,將代碼 strList.add(line); 換成 System.out.println(line); 就行。
方法加個返回值,可方便后續(xù)對內(nèi)容的操作
調(diào)用
public static void main(String[] args) {
List<String> list = ReadFileCon("F:\\img\\test1_r.txt");
for (String str: list) {
System.out.println(str);
System.out.println("-------------------------");
}
}需要引入maven包
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
private static void readTxtFileByFileUtils(String fileName) {
File file = new File(fileName);
try {
LineIterator lineIterator = FileUtils.lineIterator(file, "UTF-8");
while (lineIterator.hasNext()) {
String line = lineIterator.nextLine();
System.out.println(line);
}
} catch (IOException e) {
logger.error(e.getMessage());
}
}寫文件
// 給指定文件寫入內(nèi)容。若沒有就創(chuàng)建,但不能創(chuàng)建目錄。String filePathName="D:/test/tgj/test2.txt";
private static void WriteFileCon(String filePathName, String[] str){
try (FileWriter fw = new FileWriter(filePathName);
BufferedWriter info = new BufferedWriter(fw))
{
for (int i=0; i<str.length; i++) {
info.write(String.format(str[i] + "%n")); // 加個 %n 相當(dāng)于換行
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("寫入失敗");
}
}調(diào)用
public static void main(String[] args) {
String[] as = {"熊大", "狗二", "張三", "李四", "王五"};
WriteFileCon("F:/img/test2_w.txt", as);
}方法中可以換成傳字符串,那整個for循環(huán)就能替換成 info.write(str); 調(diào)用時就像下面這樣
public static void main(String[] args) {
StringBuilder str = new StringBuilder();
String data1 = "0056b587dfb4901371a09a59a05f10c1";
String data2 = "j23434sdfjjur3247834jhk9eqdf574e";
String data3 = "erigueugd23948924njhsjahf958345j";
str.append(data1);
str.append(System.getProperty("line.separator")); // 效果相當(dāng)于換行
str.append(data2);
str.append(System.getProperty("line.separator"));
str.append(data3);
str.append(System.getProperty("line.separator"));
WriteFileCon("F:/img/test3_w.txt", str.toString());
}總結(jié)
到此這篇關(guān)于Java讀寫txt文件的文章就介紹到這了,更多相關(guān)Java讀寫txt文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼
這篇文章主要介紹了mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
java實現(xiàn)ReadWriteLock讀寫鎖的示例
ReadWriteLock是Java并發(fā)包中的接口,定義了讀鎖和寫鎖,讀鎖允許多線程同時訪問共享資源,而寫鎖則要求獨占,這種機制適用于讀多寫少的場景,可以提高并發(fā)效率同時保證數(shù)據(jù)一致性,本文就來詳細(xì)的介紹一下如何實現(xiàn),感興趣的可以了解一下2024-09-09
Java并發(fā)編程之顯示鎖ReentrantLock和ReadWriteLock讀寫鎖
這篇文章主要介紹了Java并發(fā)編程之顯示鎖ReentrantLock和ReadWriteLock讀寫鎖,本文講解了ReentrantLock概況、Lock接口、Lock使用、輪詢鎖的和定時鎖、公平性、可中斷獲鎖獲取操作等內(nèi)容,需要的朋友可以參考下2015-04-04
Springboot 整合 Java DL4J 實現(xiàn)農(nóng)產(chǎn)品質(zhì)量檢測系統(tǒng)(推薦)
本文詳細(xì)介紹了系統(tǒng)的搭建過程,包括技術(shù)選型、數(shù)據(jù)處理、模型訓(xùn)練和評估等關(guān)鍵步驟,系統(tǒng)采用卷積神經(jīng)網(wǎng)絡(luò),對水果成熟度和缺陷進行識別,有效解決了傳統(tǒng)方法成本高、效率低的問題,有助于提升農(nóng)產(chǎn)品檢測的科技含量和自動化水平2024-10-10

