Java,
分享于 点击 36056 次 点评:131
Java,
废话:一个人拿着钱去买车,但那辆车需要10万元,但他只有99998,于是他走到一个乞丐面前说,你先借我2元,等我买了车之后还你。乞丐很潇洒的拿出4元给他,说给我也买一辆。每个人的成功都不是一蹴而就,都是经过沉淀,累积,慢慢的从量变变成质变。坚持。
走着:
当指定绝对路径时,定义目录分隔符有两种方式:
1,反斜线但是一定要写两个。new File("d:\\test\\testFile\\test.txt");
2,斜线/ 写一个即可。new File("d:/test/testFile/test.txt");
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class FileDemo01 { /*--------File.separator相当于/--------*/ /** * 创建文件夹 * @param filePath="d:/test/testFile"这是一个文件夹的路径 * 如果filePath="d:/test/testFile/a.txt"时会创建一个名叫a.txt的文件夹 */ public void newFolder(String filePath){ File file=new File(filePath); if(!file.exists()){//查看文件是否存在 file.mkdir(); System.out.println("创建成功"); } } /** * 改文件夹的名称 * @param filePath="D:/test"这是一个文件夹的路径 * 把name改成reName */ public void renameFile(String filePath,String name,String reName){ File file = new File(filePath); String[] fileNames=file.list();//得到该文件夹下所有文件夹,文件的名称包括后缀名 File f=null; for(String fileName:fileNames){ if(file.getPath().endsWith("/")){ f=new File(file.getPath()+fileName); }else{ f=new File(file.getPath()+File.separator+fileName); } if(fileName.equals(name)){ fileName=reName; f.renameTo(new File(file.getPath()+File.separator+fileName)); } } } /** * 创建文件 * @param filePath="d:/test/testFile/test.txt"这是一个文件的路径 * 如果filePath="d:/test/testFile/test"会创建一个test文件但是没有后缀名 */ public void newFile(String filePath){ File file=new File(filePath); if(!file.exists()){ try { file.createNewFile(); System.out.println("创建成功"); } catch (IOException e) { System.out.println("创建文件失败!"); } } } /** * 写文件 * @param filePath="d://test//testFile//test.txt"这是一个文件的路径 * @throws IOException */ public void writeFile(String filePath) throws IOException{ String content="我们的关系多像积木啊,\r\n不堪一击却又千变万化,\r\n用尽了心思盖得多像家,\r\n下一秒钟也可能倒塌,\r\n幸福的期待真像积木啊,\r\n多会幻想就能堆多漂亮,\r\n可惜感情从来就不听话,\r\n从爱出发却通往复杂"; File file=new File(filePath); if(!file.exists()){ try { file.createNewFile(); System.out.println("创建文件"); } catch (IOException e) { System.out.println("创建文件失败"); } } if(file.isFile()){ FileWriter fWrite=new FileWriter(file);//会覆盖之前里面的内容,默认的是false // FileWriter fWrite=new FileWriter(file,true);//不会覆盖之前里面的内容,默认的是false PrintWriter pWrite=new PrintWriter(fWrite); pWrite.write(content); pWrite.close(); System.out.println("写入成功"); }else{ System.out.println("该路径不是一个文件路径"); } } /** * 读文件 * @param filePath="d:/test/testFile/test.txt" * @throws IOException */ public void readFile(String filePath) throws IOException{ File file=new File(filePath); StringBuffer sb=new StringBuffer(); if(file.exists()){ if(file.isFile()){ BufferedReader br=new BufferedReader(new FileReader(file)); String s=br.readLine(); while(null!=s){ sb.append(s); sb.append("\n"); s=br.readLine(); } }else{ System.out.println("该路径不是一个文件路径"); } } System.out.println(sb.toString()); } /** * 读文件 * @param filePath="d:/test/testFile/test.txt" * @throws IOException */ public void readFile1(String filePath) throws IOException{ FileReader fr = new FileReader(filePath); char[] buf = new char[1024];//该长度通常都是1024 的整数倍。 int len = 0; String str=null; while((len=fr.read(buf))!=-1) { str=new String(buf,0,len); } System.out.println(str); } /** * 删除文件 * @param filePath="d:/test/testFile/test.txt"这是一个文件的路径 */ public void deleteFile(String filePath){ File file=new File(filePath); if(file.exists()){ file.delete(); //删除文件的时候可以直接删除,但是如果删除的是文件夹,必须确保文件夹下没有文件 System.out.println("删除成功"); } } /** * 删除文件夹 * @param filePath="d:/test/testFile" */ public void deleteFolder(String filePath){ File file=new File(filePath); if(file.exists()){ if(file.isDirectory()){ String nameArray[]=file.list(); if(nameArray.length>0){ File nFile=null; for(int i=0;i<nameArray.length;i++){ if(!filePath.endsWith(File.separator)){ nFile=new File(filePath+File.separator+nameArray[i]); }else{ nFile=new File(filePath+nameArray[i]); } if(nFile.isFile()){ nFile.delete(); System.out.println("删除成功"); } if(nFile.isDirectory()){//不能区别文件夹路径下是否有/。d://test,d://test//返回的都是true; this.deleteFolder(nFile.getPath()); } } }else{ System.out.println("该文件夹下没有文件"); } }else{ System.out.println("文件不是一个文件夹"); } file.delete();//如果文件夹里面是文件的话是不能删除的、需要把文件夹里面的内容都删完。 }else{ System.out.println("目录不存在"); } } /** * 复制文件 * @param oldPath="d:/test//testFile/test.txt" 都是文件夹的路径 * @param newPath="d:/test//testFile/test1.txt" */ public void copyFile(String oldPath,String newPath){ try { int bytesum=0; int byteread=0; File oldFile=new File(oldPath); File newFile=new File(newPath); if(!newFile.exists()){ newFile.createNewFile(); } FileInputStream inputStream=new FileInputStream(oldFile);//字节流 FileOutputStream outStream=new FileOutputStream(newFile); byte[] buffer=new byte[5000]; while((byteread=inputStream.read(buffer))!=-1){ bytesum+=byteread; outStream.write(buffer,0,bytesum); } outStream.close(); System.out.println("复制成功"); } catch (IOException e) { e.printStackTrace(); System.out.println("复制单个文件时出错"); } } /** * 复制文件夹 * @param oldPath="d:/test/testFile" * @param newPath="d:/test/testFile1" */ public void copyFolder(String oldPath,String newPath){ File oldFile=new File(oldPath); File newFile=new File(newPath); if(!newFile.exists()){ newFile.mkdir(); } String [] fileNames=oldFile.list(); if(fileNames.length>0){ for(String name:fileNames){ File oFile=null; if(oldFile.getPath().endsWith(File.separator)){ oFile=new File(oldFile.getPath()+name); }else{ oFile=new File(oldFile.getPath()+File.separator+name); } File nFile=null; if(newFile.getPath().endsWith(File.separator)){ nFile=new File(newFile.getPath()+name); }else{ nFile=new File(newFile.getPath()+File.separator+name); } if(oFile.isFile()){ this.copyFile(oFile.getPath(), nFile.getPath()); } else{ copyFolder(oFile.getPath(),nFile.getPath()); } } } } public static void main(String args[]) throws Exception { new FileDemo01().newFolder("d:/test/testFile"); new FileDemo01().renameFile("D:/test","test","testFile"); new FileDemo01().newFile("d:/test/testFile/test.txt"); new FileDemo01().writeFile("d:/test/testFile/test.txt"); new FileDemo01().readFile("d:/test/testFile/test.txt"); new FileDemo01().readFile1("d:/test/testFile/test.txt"); new FileDemo01().deleteFile("d:/test/testFile/test.txt"); new FileDemo01().deleteFolder("d:/test/testFile1"); new FileDemo01().copyFile("d:/test/testFile/A.txt","d:/test/testFile/B.txt"); new FileDemo01().copyFolder("d:/test/testFile","d:/test/testFile1"); } }
结束:原文出处:http://www.cnblogs.com/yaowukonga/archive/2013/03/14/2959359.html,学习从模仿开始,加入自己的理解,加深记忆
相关文章
- 暂无相关文章
用户点评