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")));
}
}
相关文档:
为什么会有Java,为什么会有.NET
有人说,Java是为了跨Windows和UNIX而产生的。是这样吗?
Sun有自己的操作系统solaris,并且打的是高端市场,而Java发展早期阶段,Windows还主
要定位在中小型企业,并没有打算与Sun一争高端客户。
而且Sun的用户大部分都是大型企业级,而Windows定位在家庭消费用户, ......
Windows下JAVA用到的环境变量主要有3个,JAVA_HOME、CLASSPATH、PATH。下面逐个分析。 JAVA_HOME指向的是JDK的安装路径,如x:\JDK_1.4.2,在这路径下你应该能够找到bin、lib等目录。值得一提的是,JDK的安装路径可以选择任意磁盘目录,不过建议你放的目录层次浅一点,如果你放的目录很深,比如x:\XXXXXX\xxxxx\XXXX\xxxx\X ......
1.Java的编译运行
Java中的package概念相当于C++中的namespace的概念。但是java会把package的名字和文件系统中目录结构对应起来,也即如果你申明了某个package的名字为com.tij.everythingisobj,那么你的文件系统中必须创建com\tij\everythingisobj这样一个 ......
java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。
新建目录<%@ page contentType="text/html;charset=gb2312"%>
<%
//String URL = request.getRequestURI();
String filePath="C:\\测试\\";
filePath=filePath.toString(); ......
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ShuffleTest {
public static void main(String args[]) {
String simpsons[ ......