Java读取文件(以后继续添加)
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")));
}
}
相关文档:
枚举类型是JDK5.0的新特征。Sun引进了一个全新的关键字enum来定义一个枚举类。下面就是一个典型枚举类型的定义:
Java代码
public enum Color{
RED,BLUE,BLACK,YELLOW,GREEN
}
public enum Color{
RED,BLUE,BLACK,YELLOW,GREEN
}
显 ......
比如applet文件是AppletTest.class
1)
在AppletTest.java的代码中
使用默认包,即不用package语句
在html页中的代码是
<applet code="AppletTest.class" width="400" height="300">
</applet>
AppletTest.class文件和html页放在一个文件夹中
2)
在AppletTest.java的代码中
package xx.yy;
在html页 ......
java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。
新建目录<%@ page contentType="text/html;charset=gb2312"%>
<%
//String URL = request.getRequestURI();
String filePath="C:\\测试\\";
filePath=filePath.toString(); ......