package shangguigu.com.day16;
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.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
public class Test01 {
/**
*
* 实现非文本文件的复制
*/
@Test
public void test05() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(
new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\1.wmv")));
bos = new BufferedOutputStream(
new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\3.wmv")));
byte [] b = new byte[20];
int len;
while((len=bis.read(b))!=-1) {
bos.write(b,0,len);
bos.flush();
}
}catch(Exception ex) {
ex.printStackTrace();
}finally {
try {
if(bos!=null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if(bis!=null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* 实现文本文件的复制
*/
@Test
public void test04() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("test01.txt")));
bw = new BufferedWriter(new FileWriter(new File("test03.txt")));
char [] c =new char[20];
int len;
while((len=br.read(c))!=-1) {
bw.write(c, 0, len);
bw.flush();
}
}catch(Exception ex) {
ex.printStackTrace();
}finally {
if(bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* 使用字节流实现内容的输入
*/
@Test
public void test03() {
BufferedReader br = null;
try {
br = new BufferedReader(
new FileReader(new File("test01.txt")));
String str;
while((str=br.readLine())!=null) {
System.out.println(str);
}
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(br!=null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
*
* 使用字符流实现内容的输出
*/
@Test
public void test02() {
BufferedWriter bw=null;
try {
bw = new BufferedWriter(
new FileWriter(new File("test02.txt")));
String str="asssjhuhuhsuhhudbjkaguksdkj\n"
+ "ahgduihdjaksbukhcuosbcuhduos\n"
+ "ahdoiahdasidhiqhdoiwahdoiawh\n"
+ "doiashdoisahdoiahdoaishdioas\n"
+ "hdoiashdoiasndioahdoiahdeipj\n"
+ "fhiernvoirehfoirwnfbioehfoiew\n"
+ "fiewhfoiewbfnoiewuhfoewiuhfio\n"
+ "ewhfoiuewhnfueiowhfoeiwhfboiew\n"
+ "hfoiewhbfiouewhfiewhfuioewhfie\n"
+ "whfioewhfiuoewhfweubnfuwerhfbw\n"
+ "euygfywerfywergfuiewbvfiuyewgf\n"
+ "iueywgfyuewtgf";
bw.write(str);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bw!=null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* **
*
* 使用字节流实现内容的输出
*/
@Test
public void test01() {
BufferedOutputStream bos=null;
try {
bos = new BufferedOutputStream(
new FileOutputStream(new File("test01.txt")));
String str="asssjhuhuhsuhhudbjkaguksdkj\n"
+ "ahgduihdjaksbukhcuosbcuhduos\n"
+ "ahdoiahdasidhiqhdoiwahdoiawh\n"
+ "doiashdoisahdoiahdoaishdioas\n"
+ "hdoiashdoiasndioahdoiahdeipj\n"
+ "fhiernvoirehfoirwnfbioehfoiew\n"
+ "fiewhfoiewbfnoiewuhfoewiuhfio\n"
+ "ewhfoiuewhnfueiowhfoeiwhfboiew\n"
+ "hfoiewhbfiouewhfiewhfuioewhfie\n"
+ "whfioewhfiuoewhfweubnfuwerhfbw\n"
+ "euygfywerfywergfuiewbvfiuyewgf\n"
+ "iueywgfyuewtgf";
bos.write(str.getBytes());
bos.flush();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bos!=null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
public class Test01 {
/**
*
* 实现非文本文件的复制
*/
@Test
public void test05() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(
new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\1.wmv")));
bos = new BufferedOutputStream(
new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\3.wmv")));
byte [] b = new byte[20];
int len;
while((len=bis.read(b))!=-1) {
bos.write(b,0,len);
bos.flush();
}
}catch(Exception ex) {
ex.printStackTrace();
}finally {
try {
if(bos!=null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if(bis!=null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* 实现文本文件的复制
*/
@Test
public void test04() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("test01.txt")));
bw = new BufferedWriter(new FileWriter(new File("test03.txt")));
char [] c =new char[20];
int len;
while((len=br.read(c))!=-1) {
bw.write(c, 0, len);
bw.flush();
}
}catch(Exception ex) {
ex.printStackTrace();
}finally {
if(bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* 使用字节流实现内容的输入
*/
@Test
public void test03() {
BufferedReader br = null;
try {
br = new BufferedReader(
new FileReader(new File("test01.txt")));
String str;
while((str=br.readLine())!=null) {
System.out.println(str);
}
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(br!=null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
*
* 使用字符流实现内容的输出
*/
@Test
public void test02() {
BufferedWriter bw=null;
try {
bw = new BufferedWriter(
new FileWriter(new File("test02.txt")));
String str="asssjhuhuhsuhhudbjkaguksdkj\n"
+ "ahgduihdjaksbukhcuosbcuhduos\n"
+ "ahdoiahdasidhiqhdoiwahdoiawh\n"
+ "doiashdoisahdoiahdoaishdioas\n"
+ "hdoiashdoiasndioahdoiahdeipj\n"
+ "fhiernvoirehfoirwnfbioehfoiew\n"
+ "fiewhfoiewbfnoiewuhfoewiuhfio\n"
+ "ewhfoiuewhnfueiowhfoeiwhfboiew\n"
+ "hfoiewhbfiouewhfiewhfuioewhfie\n"
+ "whfioewhfiuoewhfweubnfuwerhfbw\n"
+ "euygfywerfywergfuiewbvfiuyewgf\n"
+ "iueywgfyuewtgf";
bw.write(str);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bw!=null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* **
*
* 使用字节流实现内容的输出
*/
@Test
public void test01() {
BufferedOutputStream bos=null;
try {
bos = new BufferedOutputStream(
new FileOutputStream(new File("test01.txt")));
String str="asssjhuhuhsuhhudbjkaguksdkj\n"
+ "ahgduihdjaksbukhcuosbcuhduos\n"
+ "ahdoiahdasidhiqhdoiwahdoiawh\n"
+ "doiashdoisahdoiahdoaishdioas\n"
+ "hdoiashdoiasndioahdoiahdeipj\n"
+ "fhiernvoirehfoirwnfbioehfoiew\n"
+ "fiewhfoiewbfnoiewuhfoewiuhfio\n"
+ "ewhfoiuewhnfueiowhfoeiwhfboiew\n"
+ "hfoiewhbfiouewhfiewhfuioewhfie\n"
+ "whfioewhfiuoewhfweubnfuwerhfbw\n"
+ "euygfywerfywergfuiewbvfiuyewgf\n"
+ "iueywgfyuewtgf";
bos.write(str.getBytes());
bos.flush();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bos!=null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}