package hanqi.test; import java.io.FileInputStream; import java.io.FileOutputStream; public class Test06wenjianfuzhi {
public static void main(String[] args){
//文件复制 //1、边读边写 //读 输入流 try{
FileInputStream in = new FileInputStream("d:/picasa39_3.9.141.259_setup.1447731330.exe"); byte[] b = new byte[1024]; int i=-1; //写 输出流
FileOutputStream out = new FileOutputStream("e:/0.exe"); //边读边写
while((i=in.read(b))>0)
{
//写
out.write(b, 0, i);
} out.close();
in.close(); System.out.println("复制完成");
}
catch(Exception e){ e.printStackTrace(); } }
}
package hanqi.test; import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Test7 { public static void main(String[] args) {
// TODO 自动生成的方法存根 //字符流 //输出流
FileWriter fw;
try {
fw = new FileWriter("d:/test/test03.txt",true); fw.write("字符流\r\n"); fw.close(); System.out.println("写入成功"); //输入流 FileReader fr = new FileReader("d:/test03.txt"); char[] cbuf = new char[1024]; int i = -1; StringBuilder str = new StringBuilder(); while((i=fr.read(cbuf))>0)
{
str.append(new String(cbuf,0,i));
} fr.close();
System.out.println(str); } catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} } }
package hanqi.test; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Test08daihuancun { public static void main(String[] args) { // 字符流
try {
FileWriter fw = new FileWriter("d:/test07.txt"); // 带缓存的字符流
//需要先构造字符流对象,然后在家上缓存
BufferedWriter bw =new BufferedWriter(fw); bw.write("带缓存的字符流\r\n"); bw.close(); fw.close(); System.out.println("成功"); //读取
FileReader fr= new FileReader("d:/test08.txt");
BufferedReader br= new BufferedReader(fr); //带缓存的读取
//按行读取
//需要循环
String str;
StringBuilder sb1 = new StringBuilder(); //当前返回的字符串为Null,那么循环结束
while((str=br.readLine())!=null)
{
sb1.append(str+"\n");
}
//String str=br.readLine();
System.out.println(sb1); fr.close();
br.close(); } catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} } }