程序使用文件

时间:2014-06-06 07:28:02

标签: java file class

我在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)));

我怎么能把它写成更具分析性的线条?

2 个答案:

答案 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);