IO流

package my03.io;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
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.OutputStreamWriter;


public class IO {
private static String FILE = "C:/IO.java";
private static String _FILE = "C:/_IO.txt";


public static void main(String[] args) {
File f = new File(_FILE);
if (f.exists()) {
f.delete();
}
xx7();
}


/**
* 字符缓冲流

*/
public static void xx1() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
FileReader fr = new FileReader(FILE);
FileWriter fw = new FileWriter(_FILE);


br = new BufferedReader(fr);
bw = new BufferedWriter(fw);


while (br.ready()) {
bw.write(br.readLine());
bw.newLine();
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}
}


/**
* 字节缓冲流
*/
public static void xx2() {


BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {


FileInputStream fis = new FileInputStream(FILE);
FileOutputStream fos = new FileOutputStream(_FILE);


bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);


byte[] b = new byte[2048];
while (bis.read(b) != -1) {
bos.write(b);
}
bos.flush();


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}
}


/**
* 字符流

*/
public static void xx3() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(FILE);
fw = new FileWriter(_FILE);


char[] c = new char[2048];
while (fr.read(c) != -1) {
fw.write(c);
}
fw.flush();


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}
}


/**
* 字节流
*/
public static void xx4() {


FileInputStream fis = null;
FileOutputStream fos = null;
try {


fis = new FileInputStream(FILE);
fos = new FileOutputStream(_FILE);


byte[] b = new byte[2048];
while (fis.read(b) != -1) {
fos.write(b);
}
fos.flush();


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}
}


/**
* 转换流
*/
public static void xx5() {


InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {


FileInputStream fis = new FileInputStream(FILE);
FileOutputStream fos = new FileOutputStream(_FILE);


isr = new InputStreamReader(fis, "utf-8");
osw = new OutputStreamWriter(fos, "gbk");


char[] c = new char[2048];
while (isr.read(c) != -1) {
osw.write(c);
}
fos.flush();


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (isr != null) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}
}


/**
*从控制台输入,然后写入文件

*/
public static void xx6() {
BufferedReader br = null;
BufferedWriter bw = null;


try {
InputStreamReader isr = new InputStreamReader(System.in);


FileWriter fw = new FileWriter(_FILE);


br = new BufferedReader(isr);
bw = new BufferedWriter(fw);
String readLine = null;
while ((readLine = br.readLine()) != null) {
bw.write(readLine);
bw.newLine();
bw.flush();
if (readLine.equals("a")) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}
}


/**
*复制图片

*/
public static void xx7() {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(new File("c:/a.jpg"));
fos = new FileOutputStream(new File("c:/_a.jpg"));
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[2048];
while (bis.read(b) != -1) {
bos.write(b);
bos.flush();
}
} catch (Exception e) {


e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}
}


}

你可能感兴趣的:(java)