一、Reader类(字符读取)输入流
FileReader类 是InputStreamReader类子类,无法指定字符编码格式,方法通用,单个字符读取
1.FileReader(File file);
fr=new FileReader("F:/好好.txt");
} catch (Exception e) {
e.printStackTrace();
}
int length=0;
try {
while ((length=fr.read())!=-1) { //循环读取文本里每一个字符转换成了ASCll码
System.out.println((char)length); //读取接收到的数值强转为单个字符
}
} catch (IOException e) {
e.printStackTrace();
}
1.InputStreamReader(InputStream in); //传一个字节流,
InputStreamReader isr=null;
BufferedReader br=null;
try {
is=new FileInputStream("F:/好好.txt"); //将文件读取成字节流
isr=new InputStreamReader(is,"GBK");//将字节流包装成字符流,后面指定编码格式,可为utf-8
br=new BufferedReader(isr); //把读到的字符流放到缓冲区
String line;
while ((line=br.readLine())!=null) { //.readLine方法是逐行读取缓冲区字符串
System.out.println(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
isr.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
四、BufferedReader类(缓冲流)
实例:
BufferedReader br=null;
reader=new FileReader("F:/好好.txt"); //引入文件 单个字符读取文件
br=new BufferedReader(reader); //读取每个字符到缓冲区
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
while ((line=br.readLine())!=null ) {//readLine()读取一行字符串
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if (br!=null&&reader!=null) {
try {
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Writer fw=null;
fw=new FileWriter("F:/陈京.txt",true); //后面带布尔true,写入到该文件是追加字符串
String info="带你飞";
fw.write(info);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Writer w=null;
BufferedWriter bw=null;
//将字符串写入文件
try {
w=new FileWriter("E:/浪.txt"); //指定写入目标文件
bw=new BufferedWriter(w); //包装成缓冲流,写入到文件
String line="带你飞"; //赋值字符串,line接收
bw.write(line);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (w!=null) {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
InputStreamReader ir=null; //字符流读取
BufferedReader br =null; //将读到的字符串包装成缓冲区
FileWriter fw=null; //写入到目标文件
BufferedWriter bw=null; //将缓冲区的字符串写入到 bw
try {
is=new FileInputStream("E:/pet.template");
ir=new InputStreamReader(is,"utf-8");
br=new BufferedReader(ir);
//字符串更改,不创建和回收,比String更高效
StringBuffer sbf=new StringBuffer();//StringBuffer 字符串更改
String line=null;
while ((line=br.readLine())!=null) {
sbf.append(line); //.append 追加括号里的值
}
System.out.println("替换前:"+sbf);
String newstr1=sbf.toString().replace("{name}", "欧欧"); //.replace 替换括号里的值,把前面的换位后面的
String newstr2=newstr1.replace("{type}", "狗狗");
String newstr3=newstr2.replace("{master}", "李伟");
System.out.println("替换后:"+newstr3);
fw=new FileWriter("F:/pet.txt");
bw =new BufferedWriter(fw);
bw.write(newstr3);
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (bw!=null&&bw!=null&&br!=null&&ir!=null&&is!=null) {
bw.close();
fw.close();
br.close();
ir.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
读写二进制文件实现步骤:(1)引入相关类 (2)创建流对象
(3)调用DataInputStream对象的read()方法读取数据
(4)调用DataOutputStream对象的write()方法写数据
(5)读取写入的数据 (6)关闭流对象