//使用BufferedInputStream和BufferedOutputStream实现非文本文件
@Test
public void test1(){
long start = System.currentTimeMillis();
String from = "D:\\流1\\Pe.java";
String to = "D:\\流1\\Pe1.java";
copy(from,to);
long end = System.currentTimeMillis();
System.out.println("花费的时间:"+(end-start));
}
public void copy(String from,String to){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入,写出的文件
File file1 = new File(from);
File file2 = new File(to);
//2.先创建相应的节点流:FileInputStream,FileOutoutStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b))!=-1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void test3(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("D:\\流1\\Pe.java");
File file1 = new File("D:\\流1\\Pe1.java");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(br);
bw = new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while ((len = br.read(c))!=-1) {
// String str = new String(c,0,len);
// System.out.println(str);
// }
String str = null;
while ((str = br.readLine())!=null) {
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void test4(){
BufferedReader br = null;
try {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while (true) {
System.out.println("请输入字符串:");
str = br.readLine();
//忽略大小写
if(str.equalsIgnoreCase("e")||str.equalsIgnoreCase("e")){
break;
}
//转成大写
String str1 = str.toUpperCase();
System.out.println(str1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
@Test
public void test1(){
long start = System.currentTimeMillis();
String from = "D:\\流1\\Pe.java";
String to = "D:\\流1\\Pe1.java";
copy(from,to);
long end = System.currentTimeMillis();
System.out.println("花费的时间:"+(end-start));
}
public void copy(String from,String to){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入,写出的文件
File file1 = new File(from);
File file2 = new File(to);
//2.先创建相应的节点流:FileInputStream,FileOutoutStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b))!=-1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void test3(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("D:\\流1\\Pe.java");
File file1 = new File("D:\\流1\\Pe1.java");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(br);
bw = new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while ((len = br.read(c))!=-1) {
// String str = new String(c,0,len);
// System.out.println(str);
// }
String str = null;
while ((str = br.readLine())!=null) {
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void test4(){
BufferedReader br = null;
try {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while (true) {
System.out.println("请输入字符串:");
str = br.readLine();
//忽略大小写
if(str.equalsIgnoreCase("e")||str.equalsIgnoreCase("e")){
break;
}
//转成大写
String str1 = str.toUpperCase();
System.out.println(str1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}