javaIO讀和寫和創建文件夾的基礎簡單操作
來源:文萃谷 5.55K
導語:Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++裏難以理解的`多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。下面我們來看看javaIO讀和寫和創建文件夾的基礎簡單操作,希望對大家有所幫助。
1234567 | package e16; import ; import NotFoundException; import Reader; import Writer; import ception; |
1 | public class FileUtil { public static final String PATH=roperty( "" ); public void cj(String path) { File file=new File(PATH+path); int index =xOf( "." ); if( index ==-1) { if(!ts()) { rs(); } } else { int index2=IndexOf( "/" ); String string=path. substring (0, index2); if(!ts()) { File file2=new File(PATH+string); rs(); } } try { teNewFile(); } catch (IOException e) { // TODO Auto-generated catch block tStackTrace(); } } } |
public class TestFileUtil {public static void main(String[] args) {
1 | //讀和寫的初始化 FileReader fileReader= null ; FileWriter fileWriter= null ; try { |
1234567891011 | //讀和寫的初始化 fileReader=new FileReader(file); ( "/src/com/lanou/day18/" ); fileWriter=new FileWriter(fileName); //將讀出來的數據寫入 int lenght=0; char [] context=new char [1024]; while((lenght=fileReader. read (context))!=-1) { e(context,0,lenght); } |
1234567891011 | //完成後刷新 h(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block tStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block tStackTrace(); }finally { if(fileWriter!= null ) { try { |
123456789101112 | //關閉寫入流 fileWriter. close (); } catch (IOException e) { // TODO Auto-generated catch block tStackTrace(); } } } } |