java how to acces fiels code example
Example 1: Write an application that reads the content of the current directory and prints it to the screen. java
package com.vogella.java.files;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
public class FilesUtil {
public static String readTextFile(String fileName) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(fileName)));
return content;
}
public static List<String> readTextFileByLines(String fileName) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(fileName));
return lines;
}
public static void writeToTextFile(String fileName, String content) throws IOException {
Files.write(Paths.get(fileName), content.getBytes(), StandardOpenOption.CREATE);
}
}
Example 2: java file reader construct input
Reader fileReader = new FileReader("c:\\data\\input-text.txt");