1. 读取文件字符流的时候,我们用FileReader,而FileInputStream类用于读字节流。
2.注意,若是函数头部已经声明可能抛出异常,那么一个已经声明可能会抛出异常的函数,调用时得用try-catch处理
3.如果你的函数可能抛出异常,就必须在函数头部加以声明,当然,你也可以声明一种并不会真的抛出的异常(就是说其实这种异常根本不会在这个方法里出现,但仍然可以声明),但注意要在引用的地方catch该异常,并处理
4.BufferedReader与BufferedWriter是什么:
你并不一定要使用这两个类,不用程序也是ok的,但
BufferedReader与BufferedWriter顾名思义就是缓冲读入与缓冲写入,它们的存在是为了提高读入和写入的效率的,当我们想要从文件读入或写入某些数据,如:BufferedReader在读取文件时,会先从文件中读入字符数据并放入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取,如果缓冲区数据不足,才会再从文件中读取,这样读取的效率会提高,BufferedWriter同理
5.StringTokenizer介绍如下:
StringTokenizer这个类可以实现让我们根据自己需要分割字符串
比如:StringTokenizer D=new StringTokenizer(line,"_");
意思就是对于line这个字符串,我们用_下划线分割,假如line=hello_world_love_you,
经过分割后就变成了4个小字符串hello,world,love,you,对于D,那是一个容器,存放所有分割好的小字符串,用D.nextToken()读取
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;public class FileCatalogLoader implements CatalogLoader {private Coffee readCoffee(String line)throws DataFormatException{try {StringTokenizer L=new StringTokenizer(line,"_");String CoffeeToken();String codeToken();String descriptionToken();double price=Double.Token());//字符串转成double型数据String originToken();String roastToken();String flavorToken();String aromaToken();String acidityToken();String bodyToken();return new Coffee(code, description, price, origin, roast, flavor, aroma, acidity, body);} catch (NoSuchElementException e) {throw e;}}private Product readProduct(String line)throws DataFormatException{try {StringTokenizer B=new StringTokenizer(line,"_");String productToken();String codeToken();String descriptionToken();double price=Double.Token());return new Product(code, description, price);}catch (NoSuchElementException e) {throw e;}}private CoffeeBrewer readCoffeeBrewer(String line)throws DataFormatException{try {StringTokenizer D=new StringTokenizer(line,"_");String BrewerToken();String descriptionToken();String codeToken();double price=Double.Token());String modelToken();String waterSupplyToken();int numberOfCups=Integer.Token());return new CoffeeBrewer(description, code, price, model, waterSupply, numberOfCups);} catch (NoSuchElementException e) {throw e;}}@Overridepublic Catalog loadCatalog(String fileName) throws FileNotFoundException, IOException, DataFormatException {Catalog catalog=new Catalog();String line;BufferedReader lbd=new BufferedReader(new FileReader(fileName)//FileReader用于读取字符流);try {while ((line = adLine()) != null) {if (line.startsWith("Coffee")) {catalog.addProduct(readCoffee(line));} else if (line.startsWith("Brewer")) {catalog.addProduct(readCoffeeBrewer(line));} else if (line.startsWith("Product")) {catalog.addProduct(readProduct(line));}}}catch (FileNotFoundException e) {throw e;}catch (IOException e) {throw e;}catch (DataFormatException e) {throw e;}lbd.close();return catalog;}}
还有一个是GourmetCoffee里的writeFile补全代码。
private void writeFile(String filename, String content)throws IOException {/* PLACE YOUR CODE HERE */try {BufferedWriter lbd=new BufferedWriter(new FileWriter(filename));lbd.write(content);lbd.close();} catch (IOException e) {throw e;}}
本文发布于:2024-01-31 21:24:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170670744531452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |