Java 分离解析法
我正在创建一个处理SKU的程序。目前,我的程序中有两个类,Java 分离解析法,java,parsing,file-io,Java,Parsing,File Io,我正在创建一个处理SKU的程序。目前,我的程序中有两个类,SKU类是主类,Store类初始化ArrayList,并将SKU对象存储在数组中。我目前在SKU类中有一个方法,该方法从文件中获取输入,解析数据,并在objects变量中使用String标记器存储数据,并将对象添加到Store类中的数组中。我面临的问题是,我想分离SKU类中的解析方法,这样它就可以简单地从一行中读取,然后有一个单独的方法,该方法接受解析器的文件输入,最后更新我的Store类,这样它就可以用解析后的数据初始化产品。请问,你能
SKU
类是主类,Store
类初始化ArrayList
,并将SKU
对象存储在数组中。我目前在SKU
类中有一个方法,该方法从文件中获取输入,解析数据,并在objects变量中使用String
标记器存储数据,并将对象添加到Store
类中的数组中。我面临的问题是,我想分离SKU
类中的解析方法,这样它就可以简单地从一行中读取,然后有一个单独的方法,该方法接受解析器的文件输入,最后更新我的Store
类,这样它就可以用解析后的数据初始化产品。请问,你能在这方面帮助我吗
我在SKU
类中的解析方法当前如下:
public void parser() {
try {
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
// read from first line from the text file
String fileRead = br.readLine();
// skip first line from sample file as it contains headings
int lineNumber = 0;
// loop until all lines are read
while (fileRead != null)
{
if(lineNumber == 0) {
lineNumber++;
continue;
}
lineNumber++;
// use string.split to load a string array with the values from each line of
// the file, using a tab as the delimiter
String[] tokenize = fileRead.split("\t");
// assume file is made correctly
// and make temporary variables for the three types of data
String tempProductCode = tokenize[0];
String tempDescription = tokenize[1];
BigDecimal tempPrice = new BigDecimal(tokenize[2]);
// create temporary instance of SKU object
// and load with three data values
SKU tempObj = new SKU();
tempObj.setProductCode(tempProductCode);
tempObj.setDescription(tempDescription);
tempObj.setPrice(tempPrice);
// add to array list
Store.mySkuArrayList.add(tempObj);
// read next line before looping
// if end of file reached
fileRead = br.readLine();
}
// close file stream
br.close();
}
// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public class Store {
public static ArrayList<SKU> mySkuArrayList = new ArrayList<SKU>();
public void addSKU(SKU sku) {
mySkuArrayList.add(sku);
}
我的Store
课程如下:
public void parser() {
try {
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
// read from first line from the text file
String fileRead = br.readLine();
// skip first line from sample file as it contains headings
int lineNumber = 0;
// loop until all lines are read
while (fileRead != null)
{
if(lineNumber == 0) {
lineNumber++;
continue;
}
lineNumber++;
// use string.split to load a string array with the values from each line of
// the file, using a tab as the delimiter
String[] tokenize = fileRead.split("\t");
// assume file is made correctly
// and make temporary variables for the three types of data
String tempProductCode = tokenize[0];
String tempDescription = tokenize[1];
BigDecimal tempPrice = new BigDecimal(tokenize[2]);
// create temporary instance of SKU object
// and load with three data values
SKU tempObj = new SKU();
tempObj.setProductCode(tempProductCode);
tempObj.setDescription(tempDescription);
tempObj.setPrice(tempPrice);
// add to array list
Store.mySkuArrayList.add(tempObj);
// read next line before looping
// if end of file reached
fileRead = br.readLine();
}
// close file stream
br.close();
}
// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public class Store {
public static ArrayList<SKU> mySkuArrayList = new ArrayList<SKU>();
public void addSKU(SKU sku) {
mySkuArrayList.add(sku);
}
公共类存储{
public static ArrayList mySkuArrayList=new ArrayList();
公共无效添加SKU(SKU SKU){
mySkuArrayList.add(sku);
}
处理这一问题的一种方法是让parse方法返回一个标记化器列表(例如list tokenizeList),第二种方法将该列表作为输入并填充skuararylist
解析器方法的可能实现
public List<String[]> parser() {
List<String[]> tokenizeList = new ArrayList<>();
try {
... /*file opening logic*/
while (fileRead != null)
{
.../*line counting logic*/
String[] tokenize = fileRead.split("\t");
tokenizeList.add(tokenize);
fileRead = br.readLine();
}
// close file stream
br.close();
}// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return tokenizeList;
}
将代码拆分为三个单独的类。sku文件类表示存储sku代码的文本文件,此类知道如何存储每个sku条目并能够对其进行解析。sku类包含数据。Store类包含 Sku列表及其构造函数中的接受Sku文件
class SkuFile {
private String path;
SkuFile(String path) {
this.path = path;
}
List<Sku> readAllSku() {
ArrayList<Sku> result = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(new File(path).toPath());
for(String skuLine : lines) {
result.add(parseFrom(skuLine));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}
private Sku parseFrom(String data){
String[] tokenize = data.split("\t");
productCode = tokenize[0];
description = tokenize[1];
price = new BigDecimal(tokenize[2]);
return new Sku(productCode, description, price);
}
}
class Sku {
private String code;
private String description;
private BigDecimal price;
Sku(String code, String description, BigDecimal price) {
this.code = code;
this.description = description;
this.price = price;
}
//getters setters methods
}
class Store {
private List<Sku> skus;
Store(SkuFile file) {
skus = file.readAllSku();
}
}
class Test {
public static void main(String[] args) {
Store store = new Store(new SkuFile("products.txt"));
}
}
class文件{
私有字符串路径;
SKU文件(字符串路径){
this.path=path;
}
列出readAllSku(){
ArrayList结果=新建ArrayList();
试一试{
List line=Files.readAllLines(新文件(path.toPath());
用于(字符串行:行){
添加(parseFrom(skuLine));
}
}捕获(IOE异常){
抛出新的运行时异常(e);
}
返回结果;
}
专用Sku解析自(字符串数据){
字符串[]标记化=data.split(“\t”);
productCode=tokenize[0];
description=标记化[1];
价格=新的BigDecimal(标记化[2]);
返回新Sku(产品代码、说明、价格);
}
}
类别Sku{
私有字符串码;
私有字符串描述;
私人价格;
Sku(字符串代码、字符串描述、BigDecimal价格){
this.code=代码;
this.description=描述;
这个价格=价格;
}
//getters setters方法
}
班级商店{
私人清单SKU;
存储(SKU文件){
skus=file.readAllSku();
}
}
课堂测试{
公共静态void main(字符串[]args){
Store Store=新商店(新SKU文件(“products.txt”);
}
}
这听起来更像是一种重构代码的方法。谢谢,你是想在main方法下的调用中将'parser'方法传递给populateStore方法的参数吗?也许main不是该方法的最佳名称(我将其重命名为foo)。是的,我想将结果从解析器方法传递给populateStore方法。另一种方法是将解析器的结果存储在一个变量中,然后将该变量传递给另一个方法。