创建一个文件,然后在java中删除

时间:2011-08-17 10:09:00

标签: java javascript hibernate

我的要求是有一个导出按钮,onclick数据库中的数据被加载然后转换成.csv,.doc或.html文件,可以打开或保存到某个地方,这需要转换成一个文件,将其保存在本地路径中并上传到SFTP服务器并从loacl路径中删除。我的代码就像这样......

public String a(String tableName, String format,String jsondataAsString,String jsonKeyName,String userName,
            String password, String hostName,String remotePath) throws IOException{
        userName="a";
        password="a";
        hostName="10.100.10.100";
        remotePath="/tmp";
        System.out.println("TableName-->" +tableName);
        System.out.println("Format-->" +format);
        System.out.println("JsonData-->" +jsondataAsString);
        System.out.println("userName-->" +userName);
        System.out.println("password-->" +password);
        System.out.println("hostname-->" +hostName);
        System.out.println("RemotePath-->" +remotePath);
        String mimeType = null;
        String fileName = null;
        String seperator = null;        
        String lineSeperator = null;
        boolean isFileTransferComplete=true;
        OutputStream f1 =null;

        if (format.equalsIgnoreCase("CSV")) {
            fileName = tableName + ".csv";
            mimeType = "application/CSV";
            seperator = ",";
            lineSeperator = "\n";



        } else if (format.equalsIgnoreCase("Word")) {
            fileName = tableName + ".doc";
            mimeType = "application/msword";
            seperator = " ";
            lineSeperator = "\n\n";
        } else {

            fileName = tableName + ".html";
            mimeType = "application/html";
            seperator = "  ";
            lineSeperator = "<br><br>";
        }
        String localfilePath="D:/aaa/" +fileName;
        String error="";
    try{
        String data = convertJsonToString(jsondataAsString, seperator, jsonKeyName,lineSeperator,format);
        if (data == null || data.length() == 0) {
            data = "[BLANK]";
        }
       boolean isFileTobeDeleted=true;
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        System.out.println("Buffer-->" +buffer);

        buffer.flush();
        buffer.write(data.getBytes());

        f1 = new FileOutputStream(localfilePath);
        buffer.writeTo(f1);

        isFileTransferComplete = new SFTPHandler(hostName,
                PicoEmsGuiConstants.SFTP_Port, userName, password)
                .uploadFile(remotePath,localfilePath);
        System.out.println("FileTransfer" +isFileTransferComplete);

          File target = new File(localfilePath);  

             target.delete();


         }
         System.out.println("isFileTobeDeleted" +isFileTobeDeleted);



        }catch(Exception e){
         System.out.println("Exception :::>" +e.getMessage());

        }finally{
            f1.close();
        }

        return isFileTransferComplete+"--"+ remotePath;

    }

我能够创建文件但是在完成上传后无法删除形式的loacl路径...任何人都可以告诉我我在哪里做错了

1 个答案:

答案 0 :(得分:4)

你不必关闭流然后尝试删除它吗?

finally {
    f1.close();
    if(file != null && file.exists()) {
        file.delete();
    }
}