Java Path Delete nio readAndDelete(final Path p)

Here you can find the source of readAndDelete(final Path p)

Description

Read and delete file and return the content.

License

Open Source License

Parameter

Parameter Description
p the file path

Exception

Parameter Description
IOException an exception

Return

the content

Declaration

public static String readAndDelete(final Path p) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w  w  w .  j  a va2s.co  m
 * Copyright (C) 2013 Fabien Vauchelles (fabien_AT_vauchelles_DOT_com).
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3, 29 June 2007, of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;

import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    /**
     * Read and delete file and return the content.
     *
     * @param p the file path
     * @return the content
     * @throws IOException
     */
    public static String readAndDelete(final Path p) throws IOException {
        final StringBuilder sb = new StringBuilder();

        try (BufferedReader bfr = Files.newBufferedReader(p, Charset.forName("UTF-8"))) {
            String line = bfr.readLine();
            while (line != null) {
                sb.append(line);

                line = bfr.readLine();
            }
        }

        Files.delete(p);

        return sb.toString();
    }
}

Related

  1. deleteTmpDir(Path path)
  2. deleteTreeBelowPath(Path startHerePath)
  3. doDelete(@Nonnull Path path)
  4. forceDelete(Path path)
  5. optimisticDelete(Path path)
  6. recursiveDelete(Path directory)
  7. recursiveDelete(Path path)
  8. recursiveDelete(Path pathToBeDeleted)
  9. recursiveDeleteDirectory(Path dir)