其余知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后)
对于文件内容的操做主要分为两大类
分别是:
字符流
字节流
其中,字符流有两个抽象类:Writer ,Reader
其对应子类FileWriter和FileReader可实现文件的读写操做
BufferedWriter和BufferedReader可以提供缓冲区功能,用以提升效率
一样,字节流也有两个抽象类:InputStream OutputStream
其对应子类有FileInputStream和FileOutputStream实现文件读写
BufferedInputStream和BufferedOutputStream提供缓冲区功能
俺当初学IO的时候犯了很多迷糊,网上有些代码也没法经过编译,甚至风格都很大不一样,因此新手请注意:
1.本文代码较长,不应省略的都没省略,主要是由于做为一个新手须要养成良好的代码编写习惯
2.本文在linux下编译,相似于File.pathSeparator和File.separator这种表示方法是出于跨平台性和健壮性考虑
3.代码中有些操做有多种执行方式,我采用了方式1...方式2...的表述,只需轻轻解开注释即可编译
4.代码中并无在主方法上抛出异常,而是分别捕捉,形成代码过长,若是仅是测试,或者不想有好的编程习惯,那你就随便抛吧……
5.功能相似的地方就没有重复写注释了,若是新手看不懂下面的代码,那确定是上面的没有理解清楚
字符流
实例1:字符流的写入
-
import java.io.File;
-
import java.io.FileWriter;
-
import java.io.IOException;
-
-
public class Demo {
-
public static void main(String[] args ) {
-
-
-
-
String path = File.separator + "home" + File.separator + "siu" +
-
File.separator + "work" + File.separator + "demo.txt";
-
-
-
FileWriter w = null;
-
try {
-
-
-
w = new FileWriter(path);
-
-
-
w.write("Nerxious is a good boy\r\n");
-
-
w.flush();
-
} catch (IOException e) {
-
e.printStackTrace();
-
} finally {
-
-
-
if(w != null) {
-
try {
-
-
w.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
}
编译以后,在目录下面生成文件,并写入字符串
实例2:字符流的读取
-
import java.io.File;
-
import java.io.FileReader;
-
import java.io.IOException;
-
-
public class Demo2 {
-
public static void main(String[] args ) {
-
String path = File.separator + "home" + File.separator + "siu" +
-
File.separator + "work" + File.separator + "demo.txt";
-
-
FileReader r = null;
-
try {
-
r = new FileReader(path);
-
-
-
-
int temp1 = r.read();
-
System.out.println((char)temp1);
-
int temp2 = r.read();
-
System.out.println((char)temp2);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
} catch (IOException e) {
-
e.printStackTrace();
-
} finally {
-
if(r != null) {
-
try {
-
r.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
}
编译以后的效果:
实例3:文本文件的复制
-
import java.io.File;
-
import java.io.FileReader;
-
import java.io.FileWriter;
-
import java.io.IOException;
-
-
public class Demo {
-
public static void main(String[] args ) {
-
-
String doc = File.separator + "home" + File.separator + "siu" +
-
File.separator + "work" + File.separator + "demo.txt";
-
-
String copy = File.separator + "home" + File.separator + "siu" +
-
File.separator + "life" + File.separator + "lrc.txt";
-
-
FileReader r = null;
-
FileWriter w = null;
-
try {
-
r = new FileReader(doc);
-
w = new FileWriter(copy);
-
-
-
int temp = 0;
-
while((temp = r.read()) != -1) {
-
w.write(temp);
-
}
-
-
-
-
-
-
-
-
-
-
-
} catch (IOException e) {
-
e.printStackTrace();
-
} finally {
-
-
if(r != null) {
-
try {
-
r.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
if(w != null) {
-
try {
-
w.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
}
实例4:利用字符流的缓冲区来进行文本文件的复制
-
import java.io.BufferedReader;
-
import java.io.BufferedWriter;
-
import java.io.File;
-
import java.io.FileReader;
-
import java.io.FileWriter;
-
import java.io.IOException;
-
-
public class Demo {
-
public static void main(String[] args ) {
-
-
String doc = File.separator + "home" + File.separator + "siu" +
-
File.separator + "work" + File.separator + "demo.txt";
-
-
String copy = File.separator + "home" + File.separator + "siu" +
-
File.separator + "life" + File.separator + "lrc.txt";
-
-
FileReader r = null;
-
FileWriter w = null;
-
-
BufferedReader br = null;
-
BufferedWriter bw = null;
-
try {
-
r = new FileReader(doc);
-
w = new FileWriter(copy);
-
-
-
-
br = new BufferedReader(r);
-
bw = new BufferedWriter(w);
-
-
String line = null;
-
-
-
while((line = br.readLine()) != null) {
-
-
bw.write(line);
-
-
-
-
-
bw.newLine();
-
}
-
-
} catch (IOException e) {
-
e.printStackTrace();
-
} finally {
-
-
-
if(br != null) {
-
try {
-
r.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
if(bw != null) {
-
try {
-
bw.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
}