我在java中初学者,我发现了一个从txt文件输入的例子。
public class MyCollection {
private String mInputFilePath = null;
private String mOutputFilePath = null;
private ArrayList<Document> mDocuments = null;
private HashMap<String, Integer> mDocumentFrequencies = null;
public MyCollection(String inputFile, String outputFile) {
mInputFilePath = inputFile;
mOutputFilePath = outputFile;
mDocuments = new ArrayList<Document>();
mDocumentFrequencies = new HashMap<String, Integer>();
}
public void process(){
BufferedReader inputBufferedReader = null;
BufferedWriter outputBufferedWriter = null;
try {
inputBufferedReader = new BufferedReader(new FileReader(new File(mInputFilePath)));
outputBufferedWriter = new BufferedWriter(new FileWriter(new File(mOutputFilePath)));
}
我在这里无法理解这段代码:
inputBufferedReader = new BufferedReader(new FileReader(new File(mInputFilePath)));
outputBufferedWriter = new BufferedWriter(new FileWriter(newFile(mOutputFilePath)));
我怎么能把它写成更具分析性的线条?
答案 0 :(得分:1)
这inputBufferedReader = new BufferedReader(new FileReader(new File(mInputFilePath)));
与
相同File file = new File(mInputFilePath);
FileReader fr = new FileReader(file);
inputBufferedReader = new BufferedReader(fr);
编译器使用哪种方式
没有任何优点或缺点答案 1 :(得分:0)
有些代码相互放在一起,无需创建实例。
BufferedReader inputBufferedReader = null;
BufferedWriter outputBufferedWriter = null;
File fi = new File(mInputFilePath);
FileReader fri = new FileReader(fi);
inputBufferedReader = new BufferedReader(fri);
File fo = new File(mOutputFilePath);
FileReader fro = new FileReader(fro);
outputBufferedWriter = new BufferedWriter(fro);