创建一个文件,然后在java中删除 [英] Create a file and then delete in java

查看:174
本文介绍了创建一个文件,然后在java中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是有一个导出按钮,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 =\\\
;


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

fileName = tableName +.html;
mimeType =application / html;
seperator =& nbsp;& nbsp;;
lineSeperator =< br>< br>;
}
字符串localfilePath =D:/ aaa /+ fileName;
String error =;
尝试{
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);


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

} finally {
f1.close();
}

return isFileTransferComplete + - + remotePath;

}

我可以创建文件,但上传完成后无法删除形式的loacl路径...任何人都可以告诉我,我在哪里布莱恩错误

解决方案

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

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


My requirement is there is an export button,onclick the data in the database is loaded and then converted into a .csv ,.doc or .html file ,which can be opened or saved somehere, this needs to be converted into a file , save it in the local path and uploaded to the SFTP server and deleted from the loacl path. My code goes like this .....

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 = "&nbsp;&nbsp;";
            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;

    }

I am able to create file but after the completion of uploading unable to delete form the loacl path...Can anyone tell me where i am goin wrong

解决方案

Don't you have to close the stream and then attempt to delete it?

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

这篇关于创建一个文件,然后在java中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆