java每日一学--IO

规则3.1 临时文件使用完毕应及时删除

说明:临时文件可能包含敏感数据,为防止可能的未授权访问,需保证临时文件在使用完毕前被删除。错误示例:在结束时没有删除创建的临时文件。

 

 

public class TempFile 
{ 
    public static void main(String[] args) throws IOException 
    { 
        File f = new File("tempnam.tmp"); 
        if (f.exists()) 
        { 
            System.out.println("This file already exists"); 
            return; 
        } 
        FileOutputStream fop = null; 
        try 
        { 
            fop = new FileOutputStream(f); 
            String str = "Data"; 
            fop.write(str.getBytes()); 
        } 
        finally 
        { 
            if (fop != null) 
            { 
                try 
                { 
                    fop.close(); 
                } 
                catch (IOException x) 
                { 
                    // handle error 
                } 
            } 
        } 
    } 
} 
推荐做法1: 
public class TempFile 
{ 
    public static void main(String[] args) 
    { 
        Path tempFile = null; 
        try 
        { 
            tempFile = Files.createTempFile("tempnam", ".tmp"); 
            try (BufferedWriter writer = Files.newBufferedWriter(tempFile, 
                    Charset.forName("UTF8"), StandardOpenOption.DELETE_ON_CLOSE)) 
            { 
                // write to file 
            } 
            System.out.println("Temporary file write done, file erased"); 
        } 
        catch (FileAlreadyExistsException x) 
        { 
            System.err.println("File exists: " + tempFile); 
        } 
        catch (IOException x) 
        { 
            // Some other sort of failure, such as permissions. 
            System.err.println("Error creating temporary file: " + x); 
        } 
    } 
} 

 

  该示例代码使用了 Java SE 7 NIO2 包中的几个方法创建了一个临时文件,使用的是 createTempFile()
方法,

它创建一个不可预知的名字。使用try-with-resources结构打开文件,不管是否有异常该结构都会自动关闭文件。最
后,文件是用 Java SE 7 DELETE_ON_CLOSE
选项打开的,这使得在文件关闭时自动删除。如果是jdk1.7之前的

版本,则需在临时文件使用之后主动删除。

推荐做法2

public class TempFile 
{ 
    public static void main(String[] args) throws IOException 
    { 
        File f = File.createTempFile("tempnam", ".tmp"); 
        FileOutputStream fop = null; 
        try 
        { 
            fop = new FileOutputStream(f); 
            String str = "Data"; 
            fop.write(str.getBytes()); 
            fop.flush(); 
        } 
        finally 
        { 
            if (fop != null) 
            { 
                try 
                { 
                    fop.close(); 
                    f.delete(); //delete file when finished 
                } 
                catch (IOException x) 
                { 
                    // handle error 
                } 
            } 
        } 
    }   
} 
如果使用的是JDK1.7之前的版本,可以自己编写代码实现临时文件在使用完之后对其进行删除。

  

 

转载于:https://www.cnblogs.com/tjw-nau/p/3382935.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值