导语: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(); } } } } |