import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class ReadFileExample { public static void main(String[] args) { try { File file = new File("example.txt"); String fileContent = FileUtils.readFileToString(file, "UTF-8"); System.out.println(fileContent); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class WriteFileExample { public static void main(String[] args) { try { String content = "This is some example text."; File file = new File("example.txt"); FileWriter writer = new FileWriter(file); IOUtils.write(content, writer); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }This example writes a string to a file using a FileWriter object and the IOUtils class in the org.apache.commons.io package.