电商网站建设需求分析引言,移动互联网开发试卷,自做网站好做吗,十大网站通过BufferedReader读取TXT文件window系统默认的编码是GBK#xff0c;而IDE的编码多数为UTF-8#xff0c;如果没有规定new InputStreamReader(new FileInputStream(file),“GBK”)为GBK会出现读取内容乱码。//文件路径String filePathC:/Users/Admin/Desktop/products.…通过BufferedReader读取TXT文件window系统默认的编码是GBK而IDE的编码多数为UTF-8如果没有规定new InputStreamReader(new FileInputStream(file),“GBK”)为GBK会出现读取内容乱码。//文件路径String filePathC:/Users/Admin/Desktop/products.txt;File filenew 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();}}}123456789101112131415161718192021222324252627读取TXT文件并保持在List集合TXT文件内容商品 价格 类型 数量创建Product对象public class Product{private int id;private String name;private int price;private char type;private int count;123456//将txt文件中的产品对象读取出来并且封装成集合对象private static List getProductFromTxt(){List listnew ArrayList();String filePathC:/Users/Admin/Desktop/products.txt;File filenew 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[] arracontent.split(,);Product productnew Product();product.setId(line);product.setName(arra[0]);product.setPrice(Integer.parseInt(arra[1]));char[] charra[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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(reader ! null){try {reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return list;}12345678910111213141516171819202122232425262728293031323334353637383940414243其中包含了String转charchar[] chstr.toCharArray();product.setType(ch[0]);————————————————版权声明本文为CSDN博主「IManiy」的原创文章遵循 CC 4.0 BY-SA 版权协议转载请附上原文出处链接及本声明。原文链接https://blog.csdn.net/IManiy/article/details/83834360