私はテストプログラムを書いています、そしてその中で私は結果を2つの異なるファイルに書きます。特定のテスト用に1つのファイルと、すべての結果の現在の合計を保持する1つのファイル。私が持っている20のテストのそれぞれについてロジックを書き出すのではなく、の配列を返すメソッドを作成したいと思いますBufferedWriters
。これは可能ですか?
これが私が扱ってきたコードです:
public BufferedWriter[] createTestFile(String fileName, String fileName2) throws IOException{
File directory = null;
File photoDirectory = null;
BufferedWriter bufWrite=null;
BufferedWriter bw2 = null;
BufferedWriter[] bw[]=null;
SimpleDateFormat photoFormat = new SimpleDateFormat("ddMMyy-hhmmss");
new SimpleDateFormat("MMMddyy-hhmmss");
/*
* This sections checks the phone to see if there is a SD card. if
* there is an SD card, a directory is created on the SD card to
* store the test log results. If there is not a SD card, then the
* directory is created on the phones internal hard drive
*/
// if there is no SD card
if (Environment.getExternalStorageState() == null) {
directory = new File(Environment.getDataDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(Environment.getDataDirectory()
+ "/Robotium-Screenshots/");
// if no directory exists, create new directory
if (!directory.exists()) {
directory.mkdir();
}
// if phone DOES have sd card
} else if (Environment.getExternalStorageState() != null) {
// search for directory on SD card
directory = new File(Environment.getExternalStorageDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(
Environment.getExternalStorageDirectory()
+ "/Robotium-Screenshots/");
// if no directory exists, create new directory to store test
// results
if (!directory.exists()) {
directory.mkdir();
}
}// end of SD card checking
/*
* Checks for existing test logs, and if they exist, they are
* deleted, creating a new test log for each testing method
*/
File logResults = new File(directory, fileName);
File totLogRes = new File(directory, fileName2);
if (logResults.exists()) {
logResults.delete();
}
if (!logResults.exists()) {
logResults.createNewFile();
}
//check total Log
if (!totLogRes.exists()) {
totLogRes.createNewFile();
}
/*
* This creates the writing stream to log the test results This
* stream MUST be closed, using bw.close(), for the test results to
* show on the log. If the stream is not closed, when you open the
* text file that the results are stored in, the page will be blank.
*/
//This is where I have a problem. I'm not sure how to return an array of bufferedwriters
bufWrite = new BufferedWriter(new FileWriter(logResults, true));
bw2= new BufferedWriter(new FileWriter(totLogRes, true));
bw[0]=bufWrite; <---- get an error here saying I can't go from array to bw.
return bw[];
いくつかの方法を試しましたが、エラーが発生します。誰かが私を正しい方向に向けることができるかどうか疑問に思いました。