io中的文件字符输入流和文件字符输出流
1,文件字符输入流
package cn.com.io;
import java.io.*;
public class TestIo05 {
public static void main(String[] args) {
//文件字符输入流
File file = new File("abc.txt");
Reader reader = null;
try {
reader = new FileReader(file);
char[] flush = new char[1024];
int tmp;
while((tmp = reader.read(flush)) != -1) {
String str = new String(flush,0,tmp);
System.out.println(str);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
if (reader != null) {
reader.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
2,文件字符输出流
package cn.com.io;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class TestIo06 {
public static void main(String[] args) {
//文件字符输出流
File file = new File("abc.txt");
Writer writer = null;
try {
writer = new FileWriter(file);
//第一种方法
// String str = "是个人";
// char[] flush = str.toCharArray();
// writer.write(flush,0,flush.length);
//第二种方法
String str = "美人鱼!!";
writer.write(str);
writer.write("小军");
writer.flush();
//第三种方法
// writer.append("小红").append("小明").append("小英");
// writer.flush();
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
if(writer != null) {
writer.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}