Here you can find the source of readAndDelete(final Path p)
Parameter | Description |
---|---|
p | the file path |
Parameter | Description |
---|---|
IOException | an exception |
public static String readAndDelete(final Path p) throws IOException
//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(); } }