我是 java8 的新手,我正在尝试编写模板方法设计模式,我为此目的使用 Consumer,但我不知道我在哪里做错了。
package org.java.series.ds.cleancode;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.function.Consumer;
public class Sample1 {
public static void main(String[] args) throws Exception {
OrderExporter exporter=new OrderExporter();
Exporter exporter2=new Exporter();
exporter2.exportFiles("abc.csv", exporter::writeToCSV);
}
}
class Exporter{
public File exportFiles(String fileName,Consumer<Writer> consumer)throws Exception {
File file=new File(fileName);
try(FileWriter fileWriter=new FileWriter(file)){
//writeContent(fileWriter);
consumer.accept(fileWriter);
return file;
}catch (Exception e) {
System.err.println(e);
throw e;
}
}
}
class OrderExporter{
public void writeToCSV(FileWriter fileWriter) throws IOException {
fileWriter.write(String.format("%s,%d,%s", "kishan",29,"vns"));
}
}
任何帮助都是不言而喻的。