package LyfPractice;
import org.junit.Test;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Scanner;
/**
* Created by fangjiejie on 2016/12/6.
*/
public class File {
public File(String s) {
}
@Test
public void read(){
try(FileInputStream f1=new FileInputStream("D://lyf.txt")){
byte[] buffer=new byte[1024*5];
int n=-1;
while((n=f1.read(buffer))!=-1)
{
System.out.println(new String(buffer));
}
}catch (Exception e){
}finally {
}
}
@Test
public void write(){
try (FileOutputStream f2=new FileOutputStream("D://lyf.txt",true)){
f2.write("啦啦啦".getBytes());
}catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void copy(){
try (FileInputStream f3=new FileInputStream("d://liu.wmv");
FileOutputStream f4=new FileOutputStream("e://liu.wmv");
){
int n=1;
long start=System.currentTimeMillis();
byte[] buffer=new byte[1024*1024*10];
while((n=f3.read(buffer))!=-1){
f4.write(buffer,0,n);
}
long last=System.currentTimeMillis();
SimpleDateFormat h=new SimpleDateFormat("mm:ss");
System.out.println(h.format((last-start)));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void copy1(){
try (Reader f1=new FileReader("d://lyf.txt");
Writer f2=new FileWriter("e://lyf2.txt");
){
char []buffer=new char[1024];
int n=-1;
while((n=f1.read(buffer))!=-1){
f2.write(buffer,0,n);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void copy2(){
try (BufferedInputStream f5=new BufferedInputStream(new FileInputStream("D://liu.wmv"),1024*1024*10);
BufferedOutputStream f6=new BufferedOutputStream(new FileOutputStream("E://u.wmv"),1024*1024*10);
){
byte []buffer=new byte[1024*1024*10];
int n=-1;
while((n=f5.read(buffer))!=-1){
f6.write(buffer,0,n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}