日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java io流读取txt文件_Java使用IO流读取TXT文件

發布時間:2025/3/8 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java io流读取txt文件_Java使用IO流读取TXT文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過BufferedReader讀取TXT文件

window系統默認的編碼是GBK,而IDE的編碼多數為UTF-8,如果沒有規定new InputStreamReader(new FileInputStream(file),“GBK”)為GBK會出現讀取內容亂碼。

//文件路徑

String filePath="C:/Users/Admin/Desktop/products.txt";

File file=new File(filePath);

BufferedReader reader = null;

String tempString = null;

int line =1;

try {

// System.out.println("以行為單位讀取文件內容,一次讀一整行:");

reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));

while ((tempString = reader.readLine()) != null) {

System.out.println("Line"+ line + ":" +tempString);

line ++ ;

}

reader.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

if(reader != null){

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

讀取TXT文件并保持在List集合

TXT文件內容

商品 價格 類型 數量

創建Product對象

public class Product{

private int id;

private String name;

private int price;

private char type;

private int count;

1

2

3

4

5

6

//將txt文件中的產品對象讀取出來并且封裝成集合對象

private static List getProductFromTxt(){

List list=new ArrayList<>();

String filePath="C:/Users/Admin/Desktop/products.txt";

File file=new File(filePath);

BufferedReader reader = null;

String content = null;

int line =1;

try {

// System.out.println("以行為單位讀取文件內容,一次讀一整行:");

reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));

while ((content = reader.readLine()) != null) {

//System.out.println("Line"+ line + ":" +tempString);

String[] arra=content.split(",");

Product product=new Product();

product.setId(line);

product.setName(arra[0]);

product.setPrice(Integer.parseInt(arra[1]));

char[] ch=arra[2].toCharArray();

product.setType(ch[0]);

product.setCount(Integer.parseInt(arra[3]));

list.add(product);

line ++ ;

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(reader != null){

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return list;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

其中包含了String轉char

char[] ch=str.toCharArray();

product.setType(ch[0]);

————————————————

版權聲明:本文為CSDN博主「IManiy」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/IManiy/article/details/83834360

總結

以上是生活随笔為你收集整理的java io流读取txt文件_Java使用IO流读取TXT文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。