黑马程序员-day19综合练习

---------------------- android培训java培训、期待与您交流! ----------------------


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;


public class day19
{
public static void main(String[] args) throws Exception
{
//使用缓冲区循环输出Hello到一文件中
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\JAVA\\Test\\a.txt",true));
bw.write("Hello\r\n");
bw.close();
//使用缓冲区打印文件到控制台
BufferedReader br = new BufferedReader(new FileReader("F:\\JAVA\\Test\\a.txt"));
String buffer = null;
while((buffer = br.readLine()) != null)
{
System.out.println(buffer);
}
//使用缓冲区copy文本文件
br = new BufferedReader(new FileReader("F:\\JAVA\\Test\\a.txt"));
bw = new BufferedWriter(new FileWriter("F:\\JAVA\\Test\\a_copy.txt"));
while((buffer = br.readLine()) != null)
{
bw.write(buffer);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
//使用缓冲区copy图片(三种方法)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\JAVA\\Test\\b.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\JAVA\\Test\\b_copy.jpg"));
byte[] byteBuffer = new byte[1024];
int length = 0;
while ((length = bis.read(byteBuffer)) != -1)
{
bos.write(byteBuffer,0,length);
bos.flush();
}
BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("F:\\JAVA\\Test\\b.jpg"));
BufferedOutputStream bos2 = new BufferedOutputStream(new FileOutputStream("F:\\JAVA\\Test\\b_copy2.jpg"));
int intBuffer = 0;
while((intBuffer = bis2.read()) != -1)
{
bos2.write(intBuffer);
bos2.flush();
}
BufferedInputStream bis3 = new BufferedInputStream(new FileInputStream("F:\\JAVA\\Test\\b.jpg"));
BufferedOutputStream bos3 = new BufferedOutputStream(new FileOutputStream("F:\\JAVA\\Test\\b_copy3.jpg"));
byte[] byteBuffer2 = new byte[bis3.available()];
bis3.read(byteBuffer2);
bos3.write(byteBuffer2);
bos3.close();
bis3.close();
bos2.close();
bis2.close();
bis.close();
bos.close();
//copy文本,要求目标文件中带行号,从100开始
BufferedWriter bw2 = new BufferedWriter(new FileWriter("F:\\JAVA\\Test\\aWithLineNumber.txt"));
LineNumberReader lnr = new LineNumberReader(new FileReader("F:\\JAVA\\Test\\a.txt"));
lnr.setLineNumber(100);
String strBuffer = null;
while((strBuffer = lnr.readLine()) != null)
{
System.out.println(lnr.getLineNumber());//文件中输出是ASCII
bw2.write(strBuffer);
}
lnr.close();
bw2.close();
//键盘录入的内容转为大写分别输出到文件和屏幕(实时输出)
BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw3 = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedWriter bw4 = new BufferedWriter(new FileWriter("F:\\JAVA\\Test\\KeyBord.txt"));
String buf = null;
while(!("over".equals(buf = br3.readLine())))
{
bw4.write(buf.toUpperCase());
bw4.newLine();
bw4.flush();
bw3.write(buf.toUpperCase());
bw3.newLine();
bw3.flush();
}
bw3.close();
bw4.close();

}
}


---------------------- android培训java培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值