IO流:输入、输出常见的流
在项目中创建aaa.txt和bbb.txt文件
*import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Stream {
public static void main(String[] args) throws IOException {
}
private static void demo10() throws UnsupportedEncodingException, FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("aaa.txt"), "uTf-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("bbb.txt"), "utf-8"));
int c = 0;
while((c = br.read()) != -1){
bw.write(c);
}
br.close();
bw.close();
}
private static void demo9() throws UnsupportedEncodingException, FileNotFoundException, IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("aaa.txt"),"uTf-8");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("bbb.txt"),"gbk");
int c = 0;
while((c = isr.read()) != -1){
osw.write(c);
}
isr.close();
osw.close();
}
private static void demo8() throws FileNotFoundException, IOException {
LineNumberReader lnr = new LineNumberReader(new FileReader("aaa.txt"));
String line = "";
lnr.setLineNumber(100);
while((line = lnr.readLine()) != null){
System.out.println(lnr.getLineNumber() + ":" + line);
}
lnr.close();
}
private static void demo7() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("aaa.txt");
fos = new FileOutputStream("bbb.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}finally {
try{
if(fis != null)
fis.close();
}finally {
if(fos != null)
fos.close();
}
}
}
private static void demo6() throws IOException, FileNotFoundException {
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aaa.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bbb.txt"));
) {
byte[] arr = new byte[3];
int len;
while ((len = bis.read(arr)) != -1) {
bos.write(arr);
}
}
}
private static void demo5() throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt"));
char[] arr = new char[3];
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
}
private static void demo4() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("aaa.txt");
FileWriter fw = new FileWriter("bbb.txt");
char[] arr = new char[3];
int line;
while ((line = fr.read(arr)) != -1) {
fw.write(arr);
System.out.println(new String(arr, 0, line));
}
fr.close();
fw.close();
}
private static void demo3() throws FileNotFoundException, IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aaa.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bbb.txt"));
int len;
while ((len = bis.read()) != -1) {
bos.write(len);
bos.flush();
}
bis.close();
bos.close();
}
private static void demo2() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");
byte[] arr = new byte[3];
int len;
while ((len = fis.read(arr)) != -1) {
fos.write(len);
System.out.print(new String(arr,0,len));
}
fis.close();
fos.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");
int len;
while ((len = fis.read()) != -1) {
fos.write(len);
}
fis.close();
}
}*