- 相關(guān)推薦
javaIO讀和寫和創(chuàng)建文件夾的基礎(chǔ)簡單操作
導(dǎo)語:Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。下面我們來看看javaIO讀和寫和創(chuàng)建文件夾的基礎(chǔ)簡單操作,希望對大家有所幫助。
1 2 3 4 5 6 7 | package com.zuoye16; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; |
1 | public class FileUtil { public static final String PATH=System.getProperty( "user.dir" ); public void cj(String path) { File file=new File(PATH+path); int index =path.indexOf( "." ); if( index ==-1) { if(!file.exists()) { file.mkdirs(); } } else { int index2=path.lastIndexOf( "/" ); String string=path. substring (0, index2); if(!file.exists()) { File file2=new File(PATH+string); file2.mkdirs(); } } try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
public class TestFileUtil {public static void main(String[] args) {
1 | //讀和寫的初始化 FileReader fileReader= null ; FileWriter fileWriter= null ; try { |
1 2 3 4 5 6 7 8 9 10 11 | //讀和寫的初始化 fileReader=new FileReader(file); fileUtil.cj( "/src/com/lanou/day18/Text.txt" ); fileWriter=new FileWriter(fileName); //將讀出來的數(shù)據(jù)寫入 int lenght=0; char [] context=new char [1024]; while((lenght=fileReader. read (context))!=-1) { fileWriter.write(context,0,lenght); } |
1 2 3 4 5 6 7 8 9 10 11 | //完成后刷新 fileWriter.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fileWriter!= null ) { try { |
1 2 3 4 5 6 7 8 9 10 11 12 | //關(guān)閉寫入流 fileWriter. close (); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
【javaIO讀和寫和創(chuàng)建文件夾的基礎(chǔ)簡單操作】相關(guān)文章:
坡道定點停車和起步操作技巧和口訣04-10
Java網(wǎng)絡(luò)基礎(chǔ)和Socket通信基礎(chǔ)04-27
Photoshop基礎(chǔ)操作05-04
科目二坡道定點停車和起步操作要領(lǐng)和技巧05-31
PHP時間和日期函數(shù)怎么操作08-06
坡道定點停車和起步操作技巧08-12
科目二定點和起步的操作要領(lǐng)01-19
雅思聽力基礎(chǔ):詞匯和語法08-07