package cf.java.study.java.io; import java.io.File; import java.io.FileInputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; public class FileTests { @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Before public void startTest() { System.out.println(" Test Start " + StringUtils.repeat("-", 10)); } @After public void endTest() { System.out.println('/n' + StringUtils.repeat("-", 10) + " Test End /n"); } @Test public void readDir() throws Exception { File file = new File("/"); System.out.println("file: /n" + file); // list the items in the directory for (String str : file.list()) { System.out.println(str); } } @Test public void readFile() throws Exception { File file = new File("./test"); // make a FileInputStream with the file instance FileInputStream fis = new FileInputStream(file); // read the bytes from fis // it is the end of file when read() returns -1 for (int re = 0; re >= 0; re = fis.read()) { System.out.print((char)re); } fis.close(); } @Test public void readFileByApache() throws Exception { // are you crazy?? can you be more simplier? System.out.print(FileUtils.readFileToString(new File("./test"))); } }