重要博客:
http://blog.csdn.net/hguisu/article/details/7418161
- File dir = new File("\\root"); 目录
- File f1 = new File(dir, "fileOne.txt"); 文件
dir.mkdir();//创建目录
f1.createNewFile();//创建文件
java.io包中包含了流式I/O所需要的所有类。在java.io包中有四个基本类:InputStream、OutputStream及Reader、Writer类,它们分别处理字节流和字符流:
其中常用的是BufferedInputStream和BufferedOutputStream(缓冲);FileReader和FileWriter。
5. FileInputStream流和FileOutputStream的应用
利用程序将文件file1.txt 拷贝到file2.txt中。
[java] view plain copy print?
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class TestFile {
public static void main(String args[]) throws IOException {
try {
File inFile = new File("copy.java");
File outFile = new File("copy2.java");
FileInputStream finS = new FileInputStream(inFile);
FileOutputStream foutS = new FileOutputStream(outFile);
int c;
while ((c = finS.read()) != -1) {
foutS.write(c);
}
finS.close();
foutS.close();
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
}
}
}
3)键盘输入流读到内存
将BufferedReader与标准的数据流相接
InputStreamReader sin=new InputStreamReader (System.in) ;
BufferedReader bin=new BufferedReader(sin);
[java] view plain copy
- import java.io.*;
- public class ReadWriteToFile {
- public static void main(String args[]) throws IOException {
- InputStreamReader sin = new InputStreamReader(System.in);
- BufferedReader bin = new BufferedReader(sin);
- FileWriter out = new FileWriter("myfile.txt");
- BufferedWriter bout = new BufferedWriter(out);
- String s;
- 10. while ((s = bin.readLine()).length() > 0) {
- 11. bout.write(s, 0, s.length());
- 12. }
- 13.
- 14. }
15. }
程序说明:
从键盘读入字符,并写入到文件中BufferedReader类的方法:String readLine()
作用:读一行字符串,以回车符为结束。
BufferedWriter类的方法:bout.write(String
s,offset,len)
作用:从缓冲区将字符串s从offset开始,len长度的字符串写到某处。