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();
}
}


}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值