Java IO复习(一)

时间:2023-03-24 19:45:20
package com.zyw.file;

import java.io.*;

/**
* Created by zyw on 2016/3/10.
*/
public class IOTest {
public static void main(String args[]){
File newFile=new File("G:","mywork.txt");//File newFile=new File("G:\\mywork.txt");
if(newFile.exists()){
newFile.delete();
System.out.println(newFile.getName()+"is delete");
}else {
try {
newFile.createNewFile();
System.out.println(newFile.getName()+"is created");
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream os=null;
try {
os=new FileOutputStream(newFile);
try {
os.write("zyw is a smart boy".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
//注意一定要将流关闭!!!
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}