Java IO学习笔记+代码(1)
文件对象的生成和文件的创建 /* * ProcessFileName.java * * Created on 2006年8月22日, 下午3:10 * * 文件对象的生成和文件的创建 */ package study.iostudy; import java.io.*; public class GenerateFile { public static void main(String[] args) { File dirObject = new File("d:\\mydir"); File fileObject1 = new File("oneFirst.txt"); File fileObject2 = new File("d:\\mydir", "firstFile.txt"); System.out.println(fileObject2); try { dirObject.mkdir(); }catch(SecurityException e) { e.printStackTrace(); } try { fileObject2.createNewFile(); fileObject1.createNewFile(); }catch(IOException e) { e.printStackTrace(); } } } 文件名的处理 /* * ProcessFileName.java * * Created on 2006年8月22日, 下午3:29 * * 文件名的处理 */ package study.iostudy; import java.io.*; /* * 文件名的处理 * String getName(); 获得文件的名称,不包含文件所在的路径。 * String getPath(); 获得文件的路径。 * String getAbsolutePath(); 获得文件的绝对路径。 * String getParent(); 获得文件的上一级目录的名称。 * String renameTo(File newName); 按参数中给定的完整路径更改当前的文件名。 * int compareTo(File pathName); 按照字典顺序比较两个文件对象的路径。 * boolean isAbsolute(); 测试文件对象的路径是不是绝对路径。 */ public class ProcesserFileName { public static void main(String[] args) { File fileObject1 = new File("d:\\mydir\\firstFile.txt"); File fileObject2 = new File("d:\\firstFile.txt"); boolean pathAbsolute = fileObject1.isAbsolute(); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * "); System.out.println("There are some information of fileObject1's file name:"); System.out.println("fileObject1: " + fileObject1); System.out.println("fileObject2: " + fileObject2); System.out.println("file name: " + fileObject1.getName()); System.out.println("file path: " + fileObject1.getPath()); System.out.println("file absolute path: " + fileObject1.getAbsolutePath()); System.out.println("file's parent directory: " + fileObject1.getParent()); System.out.println("file's absoulte path: " + pathAbsolute); int sameName = fileObject1.compareTo(fileObject2); System.out.println("fileObject1 compare to fileObject2: " + sameName); fileObject1.renameTo(fileObject2); System.out.println("file's new name: " + fileObject1.getName()); } } 测试和设置文件属性 /* * SetterFileAttribute.java * * Created on 2006年8月22日, 下午3:51 * * 测试和设置文件属性 */ package study.iostudy; import java.io.*; public class SetterFileAttribute { /* * File类中提供的有关文件属性测试方面的方法有以下几种: * boolean exists(); 测试当前文件对象指示的文件是否存在。 * boolean isFile(); 测试当前文件对象是不是文件。 * boolean isDirectory(); 测试当前文件对象是不是目录。 * boolean canRead(); 测试当前文件对象是否可读。 * boolean canWrite(); 测试当前文件对象是否可写。 * boolean setReadOnly(); 将当前文件对象设置为只读。 * long length(); 获得当前文件对象的长度。 */ public static void main(String[] args) { File dirObject = new File("d:\\mydir"); File fileObject = new File("d:\\mydir\\firstFile.txt"); try { dirObject.mkdir(); fileObject.createNewFile(); }catch(IOException e) { e.printStackTrace(); } System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * "); System.out.println("there are some information about property of file object:"); System.out.println("file object : " + fileObject); System.out.println("file exist? " + fileObject.exists()); System.out.println("Is a file? " + fileObject.isFile()); System.out.println("Is a directory?" + fileObject.isDirectory()); System.out.println("Can read this file? " + fileObject.canRead()); System.out.println("Can write this fie? " + fileObject.canWrite()); long fileLen = fileObject.length(); System.out.println("file length: " +fileLen); boolean fileRead = fileObject.setReadOnly(); System.out.println(fileRead); System.out.println("Can read this file? " + fileObject.canRead()); System.out.println("Can write this fie? " + fileObject.canWrite()); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * "); } }本文出自 51CTO.COM技术博客 |


zc4530
博客统计信息
热门文章
最新评论
友情链接