读取和删除文件 [英] read and delete a file

查看:122
本文介绍了读取和删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下类似方法来写入文件,读取文件并删除. 这是最好的方法吗?

i have tried something like below to write a file, read it and delete. is this a best way ?

public class WriteFileExample {

private void createFile(String filename){
        FileOutputStream fop = null;
        File file;
        String content = "This is the text content";

        try {

            file = new File(filename);
            fop = new FileOutputStream(file);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}

private void readAndDeleteFile(String filename){
   try {
            DeleteOnCloseFileInputStream  fis = new DeleteOnCloseFileInputStream(new File(filename));

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        WriteFileExample  exe = new WriteFileExample ();

        exe.createFile("c:/mytestfile.txt");
        readAndDeleteFile("c:/mytestfile.txt");
    }
}

第二个类扩展FileInput流

Second class extending FileInput stream

public class DeleteOnCloseFileInputStream extends FileInputStream {
   private File file;
   public DeleteOnCloseFileInputStream(String fileName) throws FileNotFoundException{
      this(new File(fileName));
   }
   public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException{
      super(file);
      this.file = file;
   }

   public void close() throws IOException {
       try {
          super.close();
       } finally {
          if(file != null) {
             file.delete();
             file = null;
         }
       }
   }
}

推荐答案

读取文件时,请使用缓冲区来提高效率.

when you read the file, use buffer to get higher efficiency.

InputStream is = BufferedInputStream(new FileInputStream(new File(fileName)));

希望能为您提供帮助.

这篇关于读取和删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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