---------------------- 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培训、期待与您交流! ----------------------