JDK7 introduced a new I/O mechanism called NIO2 (New Input Output 2). It's possible to use NIO2 with streams or channels. This is a class I wrote to manipulate files using the new APIs with the streams:
package org.davis.main; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * <p>This class is used to work with files using NIO2</p> * @author Davis Fiore * */ public final class FileManager { private final static Charset ENCODING = StandardCharsets.UTF_8; /** * <p>Output a collection to a file as text</p> * @param fileName - output file name * @param lines - string collection to be written to a file * @throws IOException */ public void writeBufferedTextFromArray(String fileName, List<String> lines) throws IOException { Path path = Paths.get(fileName); BufferedWriter writer = Files.newBufferedWriter(path, ENCODING); for (String line : lines) { writer.write(line); writer.newLine(); } writer.close(); } /** * <p>Output a string to a file as text</p> * @param fileName - output file name * @param s - input sting * @throws IOException */ public void writeBufferedText(String fileName, String s) throws IOException { Path path = Paths.get(fileName); BufferedWriter writer = Files.newBufferedWriter(path, ENCODING); writer.write(s,0,s.length()); writer.close(); } /** * <p>Output a string to a file as byte stream</p> * @param fileName - output file name * @param lines - input string * @throws IOException */ public void writeBufferedBytes(String fileName, String s) throws IOException { Path path = Paths.get(fileName); byte[] bytes = s.getBytes(); OutputStream os = new BufferedOutputStream(Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.APPEND)); os.write(bytes,0,bytes.length); } /** * <p>Output a collection to a file in a single operation</p> * @param fileName - output file name * @param lines - input string * @throws IOException */ public void bulkWriteFromArray(List<String> lines, String fileName) throws IOException { Path path = Paths.get(fileName); Files.write(path, lines, ENCODING); } /** * <p>Read a file in a single operation (convenient for small files)</p> * @param fileName - input file name * @return List of lines * @throws IOException */ public List<String> bulkReadToArray(String fileName) throws IOException { Path path = Paths.get(fileName); return Files.readAllLines(path, ENCODING); } /** * <p>Read a file in a single operation (convenient for small files)</p> * @param fileName - input file name * @return List of lines * @throws IOException */ public List<String> readBufferedTextToArray(String fileName) throws IOException { Path path = Paths.get(fileName); List<String> res = new ArrayList<>(); BufferedReader reader = Files.newBufferedReader(path, ENCODING); String line = null; while ((line = reader.readLine()) != null) res.add(line); reader.close(); return res; } /** * <p>Read a file as text using Scanner and return a list of lines</p> * @param fileName - input file name * @return List of lines * @throws IOException */ public List<String> readTextWithScannerToArray(String fileName) throws IOException { Path path = Paths.get(fileName); List<String> res = new ArrayList<>(); Scanner sc = new Scanner(path, ENCODING.name()); while (sc.hasNextLine()) res.add(sc.nextLine()); sc.close(); return res; } /** * <p>Read a file as text and return a string</p> * @param fileName - input file name * @return String with the content of the file * @throws IOException */ public String readBufferedText(String fileName) throws IOException { Path path = Paths.get(fileName); String res = ""; String newLine = System.getProperty("line.separator"); BufferedReader reader = Files.newBufferedReader(path, ENCODING); String line = null; while ((line = reader.readLine()) != null) res += line + newLine; reader.close(); return res; } /** * <p>Read a file as byte stream and return a string</p> * @param fileName - input file name * @return String with the content of the file * @throws IOException */ public String readBufferedBytes(String fileName) throws IOException { Path path = Paths.get(fileName); String res = ""; InputStream is = Files.newInputStream(path); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = reader.readLine()) != null) res += line; reader.close(); is.close(); return res; } /** * <p>Create a file</p> * @param fileName - output file name * @throws IOException */ public void createFile(String fileName) throws IOException { Path path = Paths.get(fileName); Files.createFile(path); } /** * <p>Delete a file</p> * @param fileName - file name * @throws IOException */ public void deleteFile(String fileName) throws IOException { Path path = Paths.get(fileName); Files.delete(path); } /** * <p>Copy a file</p> * @param source - source file name * @param dest - destination file name * @throws IOException */ public void copyFile(String source, String dest) throws IOException { Path sourcePath = Paths.get(source); Path destPath = Paths.get(dest); Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING); } /** * <p>Move a file</p> * @param source - source file name * @param dest - destination file name * @throws IOException */ public void moveFile(String source, String dest) throws IOException { Path sourcePath = Paths.get(source); Path destPath = Paths.get(dest); Files.move(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING); } }
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.