【问题标题】:can I create a method that returns an array of bufferedwriters我可以创建一个返回缓冲区写入器数组的方法吗
【发布时间】:2012-07-26 18:17:41
【问题描述】:

我正在编写一个测试程序,并在其中将结果写入 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[]; 

我尝试了几种不同的方法,但我得到了错误。我想知道是否有人可以指出我正确的方向。

【问题讨论】:

  • 您可能想看看使用 JUnit,它为 Java 应用程序提供了一个测试框架。我只将它用于桌面开发。可能有适用于 Android 的版本,但我不能 100% 确定。

标签: java android eclipse testing robotium


【解决方案1】:

您应该将 bw 的声明更改为:

BufferedWriter[] bw=new BufferedWriter[2]; 

现在,您正在声明 BufferedWriter 的数组和数组。而且你没有分配它......

另外,return 语句应该是

return bw;

【讨论】:

    【解决方案2】:

    几个错误:

    BufferedWriter[] bw[]=null;
    

    定义一个数组,就像int a[];,所以

    BufferedWritter bw[] = null;
    

    另外,数组是一个对象,你需要用new创建它

    BufferedWriter bw[] = new BufferedWriter[2];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-23
      • 2011-08-02
      • 2017-01-20
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多