文件流,操作字节文件
FileInputStream
FileOutputStream
package IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.plaf.synth.SynthScrollBarUI;
import org.junit.Test;
public class fileInputStream {
@Test
public void testFileInputStream() throws IOException{
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
byte[] by = new byte[5];
int len;
while((len=fis.read(by))!=-1){
String string = new String(by,0,len);
System.out.print(string);
}
fis.close();
}
@Test
public void testFileInputStream1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File file1 = new File("1.png");
File file2 = new File("1_1.png");
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] by = new byte[5];
int len;
while ((len = fis.read(by)) != -1) {
fos.write(by);
}
} catch (Exception e) {
}finally{
if(fis != null){
try {
fis.close();
} catch (Exception e2) {
}
}
if(fos!=null){
try {
fos.close();
} catch (Exception e2) {
}
}
}
}
public void copyFile(String inputPath,String outputPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File file1 = new File(inputPath);
File file2 = new File(outputPath);
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] by = new byte[1024];
int len;
while ((len = fis.read(by)) != -1) {
fos.write(by);
}
} catch (Exception e) {
}finally{
if(fis != null){
try {
fis.close();
} catch (Exception e2) {
}
}
if(fos!=null){
try {
fos.close();
} catch (Exception e2) {
}
}
}
}
@Test
public void testCopy(){
long start = System.currentTimeMillis();
copyFile("D:\\桌面文件\\1.mp4","D:\\桌面文件\\2.mp4" );
long end = System.currentTimeMillis();
System.out.println("复制操作花费的时间:"+(end-start));
}
}