1.輸入輸出流
io流.PNG
1.代碼:
package Test;
import org.junit.Test;
import java.io.*;
public class JavaIO {
/*
* 字節(jié)型輸入流
* */
@Test
public void fileinputstream(){
File file=new File("D:/test/aaa.txt");
System.out.println(file.isFile());
FileInputStream fis=null;
try {
fis=new FileInputStream(file);
byte[] bit= new byte[5];
int code=fis.read(bit);
while (code!=-1){
String value=new String(bit,0,code);
System.out.print(value);
code=fis.read(bit);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fis!=null) {//這個判斷是問了防止file找不到文件報錯導(dǎo)致FileinputStream沒有創(chuàng)建然后空指針
fis.close();//關(guān)閉通道
}
}catch (IOException e){
e.printStackTrace();
}
}
}
/**
* 字符型輸出流
*/
@Test
public void fileoutputstream(){
File file=new File("D:/test/bbb.txt");
FileOutputStream outputStream=null;
try {
outputStream=new FileOutputStream(file,true);
// byte[] bytes=new byte[5];
// bytes[0]=97;
String str="a+b+c";
byte[] bytes=str.getBytes();
outputStream.write(bytes);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
/*
* 文件的復(fù)制,bug沒有判斷是否是文件還是文件夾
* */
public void fileCopy(String fileName,String copyPath){
File file=new File(fileName);
FileInputStream fis=null;
FileOutputStream fot=null;
try {
fis=new FileInputStream(file);
byte[] bit= new byte[1024];
int code=fis.read(bit);
while (code!=-1){
fot=new FileOutputStream(copyPath,true);
//String value=new String(bit,0,code);
fot.write(bit,0,code);
code=fis.read(bit);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fis!=null) {//這個判斷是問了防止file找不到文件報錯導(dǎo)致FileinputStream沒有創(chuàng)建然后空指針
fis.close();//關(guān)閉通道
fot.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
@Test
public void copyTest(){
// this.fileCopy("D:/test/bbb.txt","D:/test/ccc.txt");
File file =new File("D:\\test");
JavaIO javaIO=new JavaIO();
javaIO.superCopy(file,"D:\\test");
//this.superCopy(file,"D:/test1");
}
public void superCopy(File file,String newPath){
File[] files=file.listFiles();
String oldPath=file.getAbsolutePath();
String newFilePath=newPath+oldPath.split(":")[1];
File newFile=new File(newFilePath);
if(files!=null){
//file是文件夾
newFile.mkdir();
System.out.println(newFilePath+"文件夾創(chuàng)建");
if(files!=null&&files.length!=0){
for (File f:files) {
this.superCopy(f,newPath);
}
}
}else {
//file是文件
FileInputStream fis=null;
FileOutputStream fot=null;
try {
fis=new FileInputStream(file);
fot=new FileOutputStream(newFile);
byte[] bit= new byte[1024];
int code=fis.read(bit);
while (code!=-1){
//String value=new String(bit,0,code);
fot.write(bit,0,code);
fot.flush();
code=fis.read(bit);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fis!=null) {//這個判斷是問了防止file找不到文件報錯導(dǎo)致FileinputStream沒有創(chuàng)建然后空指針
fis.close();//關(guān)閉通道
fot.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
/*
* 對象流,使用前需要將對象序列化
* */
@Test
public void objeceInputStream()throws Exception{
//將對象直接記錄在文件中
// Person p=new Person("長啥","10010",12);
// FileOutputStream fos=new FileOutputStream("D:/test/ddd.txt");
// ObjectOutputStream obs=new ObjectOutputStream(fos);
// obs.writeObject(p);
// obs.flush();
// obs.close();
FileInputStream ins=new FileInputStream("D:/test/ddd.txt");
ObjectInputStream ons=new ObjectInputStream(ins);
Person person=(Person) ons.readObject();
System.out.println(person.getName());
person.eat();
}
}
2.總結(jié)
io總結(jié).PNG