All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.brambolt.nio.file.Files Maven / Gradle / Ivy

There is a newer version: 2022.05.01-7057
Show newest version
/*
 * Copyright 2017-2021 Brambolt.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.brambolt.nio.file;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;

/**
 * Small utility functions, porcelain and mnemonics for file-handling.
 *
 * 

These functions are easy to remember and use, and useful for simple use * cases. If speed, robustness in boundary cases, precise error handling or * other detailed requirements are more important than ease of use, then it is * best to use the underlying APIs directly.

*/ public class Files { /** * Returns the file type extension for the parameter file. * *

If the file parameter is null, the return value is null.

* *

If the file name does not contain a dot, the returned extension is * the empty string.

* *

The extension is returned without the dot: csv

* * @param file The file to get the extension for * @return The extension, like "csv" or "txt" */ public static String getExtension(File file) { if (null == file) return null; String fileName = file.getName(); int offset = fileName.lastIndexOf("."); if (-1 == offset) return ""; // No extension return fileName.substring(offset + 1); } /** *

Reads an entire file into a string.

* *

With Java 11+, use java.nio.file.Files.readString.

* * @param path The path to read from * @param encoding The encoding to read with * @return The content at the parameter path as a string in the encoding * @throws IOException If unable to read the string content */ public static String readString(String path, Charset encoding) throws IOException { return readString(Paths.get(path), encoding); } /** *

Reads an entire file into a string.

* *

With Java 11+, use java.nio.file.Files.readString.

* * @param path The path to read from * @param encoding The encoding to read with * @return The content at the parameter path as a string in the encoding * @throws IOException If unable to read the string content */ public static String readString(Path path, Charset encoding) throws IOException { return new String(java.nio.file.Files.readAllBytes(path), encoding); } /** *

Reads an entire file into a string.

* *

With Java 11+, use java.nio.file.Files.readString.

* * @param file The file to read from * @param encoding The encoding to read with * @return The content at the parameter path as a string in the encoding * @throws IOException If unable to read the string content */ public static String readString(File file, Charset encoding) throws IOException { return readString(file.toPath(), encoding); } /** * Creates a copy of the content at the parameter path, in the temporary * files directory (normally /tmp, C:\Temp or a * user-specific location under the home directory). * * @param path The path to the content to copy * @return The path to the newly created copy * @throws IOException If unable to copy the content */ public static Path createTempCopy(Path path) throws IOException { return createTempCopy(path, false); } /** * Creates a copy of the content at the parameter path, in the temporary * files directory (normally /tmp, C:\Temp or a * user-specific location under the home directory). * *

If the deleteOnExit parameter is true then * the copy will be removed when the JVM exits.

* * @param path The path to the content to copy * @param deleteOnExit The copy will be deleted at JVM exit iff true * @return The path to the newly created copy * @throws IOException If unable to copy the content */ public static Path createTempCopy(Path path, boolean deleteOnExit) throws IOException { FileNames.Split fileName = FileNames.Split.apply(path); Path tmpPath = java.nio.file.Files.createTempFile(fileName.prefix, fileName.suffix); java.nio.file.Files.copy(path, tmpPath, StandardCopyOption.REPLACE_EXISTING); if (deleteOnExit) tmpPath.toFile().deleteOnExit(); return tmpPath; } /** * Writes the parameter content to the parameter file (and erases whatever * content the file held before, or creates the file if necessary). * *

The default character set will be used.

* * @param file The file to write to * @param content The content to write * @throws IOException If unable to write to the file */ public static void writeString(File file, String content) throws IOException { writeString(file, content, Charset.defaultCharset()); } /** * Writes the parameter content to the parameter file (and erases whatever * content the file held before, or creates the file if necessary). * * @param file The file to write to. * @param content The content to write. * @param encoding The character set to use. * @throws IOException If unable to write to the file. */ public static void writeString(File file, String content, Charset encoding) throws IOException { java.nio.file.Files.write(file.toPath(), content.getBytes(encoding)); } /** * Writes the parameter content to the parameter file (and erases whatever * content the file held before, or creates the file if necessary). * * @param file The file to write to. * @param content The content to write. * @param openOption The options for opening the destination file. * @throws IOException If unable to write to the file. */ @SuppressWarnings("unused") public static void writeString(File file, String content, StandardOpenOption openOption) throws IOException { writeString(file, content, Charset.defaultCharset(), openOption); } /** * Writes the parameter content to the parameter file (and erases whatever * content the file held before, or creates the file if necessary). * * @param file The file to write to. * @param content The content to write. * @param encoding The character set to use. * @param openOption The options for opening the destination file. * @throws IOException If unable to write to the file. */ public static void writeString(File file, String content, Charset encoding, StandardOpenOption openOption) throws IOException { java.nio.file.Files.write(file.toPath(), content.getBytes(encoding), openOption); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy