- 相关推荐
javaIO读和写和创建文件夹的基础简单操作
导语:Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。下面我们来看看javaIO读和写和创建文件夹的基础简单操作,希望对大家有所帮助。
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); //将读出来的数据写入 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 | //关闭写入流 fileWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
【javaIO读和写和创建文件夹的基础简单操作】相关文章:
坡道定点停车和起步操作技巧和口诀04-10
Java网络基础和Socket通信基础04-27
Photoshop基础操作05-04
科目二坡道定点停车和起步操作要领和技巧05-31
PHP时间和日期函数怎么操作08-06
坡道定点停车和起步操作技巧08-12
科目二定点和起步的操作要领01-19
雅思听力基础:词汇和语法08-07
PHOTOSHOP的基础操作原理05-22