06-Java 本地文件操作
1、File類簡介
? 創建好:File file=new File("hello.txt"); 后,按住Ctrl鍵、單擊File。會出現File的源代碼。
在視圖左下角雙擊“outline”大綱最大化后會出現文件所具有的方法,帶有綠色的點,表明方法是公開的,即public 。
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("hello.txt");if (file.exists()) {System.out.println(file.isFile());//文件可以是文件System.out.println(file.isDirectory());//也可以是文件夾(路徑)}else {System.out.println("文件不存在"); //結果是不存在的!?
2、文件的創建、刪除、重命名。
(1)創建和刪除:?
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("hello.txt");if (file.exists()) {// System.out.println(file.isFile());//文件可以是文件 // System.out.println(file.isDirectory());//也可以是文件夾(路徑)file.delete();//文件刪除System.out.println("文件刪除成功");}else {System.out.println("文件不存在");try {file.createNewFile();System.out.println("文件已經成功創建");} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println("文件無法創建");?
(2)重命名:(注意:文件夾結構必須處于同一個分區,文件處于不同的分區,需要使用的是文件的拷貝而不是重命名)
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("hello.txt");if (file.exists()) {File nameto=new File("new hello.txt");//重命名 file.renameTo(nameto); // System.out.println(file.isFile());//文件可以是文件 // System.out.println(file.isDirectory());//也可以是文件夾(路徑)//file.delete();//文件刪除//System.out.println("文件刪除成功");}else {System.out.println("文件不存在");try {file.createNewFile();System.out.println("文件已經成功創建");} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println("文件無法創建");}?
3、文件夾的創建、刪除、重命名。
1、
public static void main(String[] args) {File folder =new File("my new folder");//文件夾的創建if (folder.mkdirs()) {System.out.println("文件夾創建完成");}else {if (folder.exists()) {System.out.println("文件夾已經存在不用創建");}else {System.out.println("文件夾創建失敗");?
2、
File folder =new File("my new folder");File newfolder=new File("my new folder-new");//重命名if (folder.renameTo(newfolder)) {System.out.println("重命名成功");}else {System.out.println("重命名失敗");?
3、
File folder =new File("my new folder");if (folder.delete()) {//刪除(只能刪空文件夾啊!)System.out.println("delete suceeded");}else {System.out.println("delete failed");?
4、文件屬性的讀取
(1)創建一個文件:在右擊工程、new、選untitled text file 、輸入內容后、另存為(選中所屬工程名)、重命名~
public class ReadFileProperty {public static void main(String[] args) {File file=new File("text.txt"); // 判斷文件是否存在System.out.println("判斷文件是否存在"+file.exists()); // 讀取文件名稱System.out.println("讀取文件名稱"+file.getName()); // 讀取文件(相對)路徑System.out.println("讀取文件路徑"+file.getPath()); // 讀取文件絕對路徑System.out.println("讀取文件絕對路徑"+file.getAbsolutePath()); // 獲取文件父級路徑System.out.println("獲取文件父級路徑"+new File(file.getAbsolutePath()).getParent()); // 讀取文件大小System.out.println("讀取文件大小"+file.length()+"byte");System.out.println("讀取文件大小"+(float)file.length()/1000+"KB"); // 判斷文件是否被隱藏System.out.println("判斷文件是否被隱藏"+file.isHidden()); // 判斷文件是否可讀System.out.println("判斷文件是否可讀"+file.canRead()); // 判斷文件是否可寫System.out.println("判斷文件是否可寫"+file.canWrite()); // 判斷文件是否為文件夾System.out.println("判斷文件是否為文件夾"+file.isDirectory());?
結果:
判斷文件是否存在true
讀取文件名稱text.txt
讀取文件路徑text.txt
讀取文件絕對路徑C:\Documents and Settings\Owner.LENOVO-F94A111E\workspace\ReadFileProperty\text.txt
獲取文件父級路徑C:\Documents and Settings\Owner.LENOVO-F94A111E\workspace\ReadFileProperty
讀取文件大小14byte
讀取文件大小0.014KB
判斷文件是否被隱藏false
判斷文件是否可讀true
判斷文件是否可寫true(如果將文件設置為只讀的話 ,那么就是false !)
判斷文件是否為文件夾false
?
?
1、? 文件屬性的設置
File file=new File("test.file"); //也是要手動新建一個文件的!if (file.exists()) {//將文件設定為可寫(前提需要先將它設置為不可寫!)file.setWritable(true);//or false//將文件設定為可讀file.setReadable(true);//將文件設定為只讀(運行一下語句時需要將上面兩個語句注釋掉~)file.setReadOnly();}2、? 遍歷文件夾:
public static void main(String[] args) {// TODO Auto-generated method stubprintFiles(new File("../FileScanner"),1);}public static void printFiles(File dir,int tab) {//tab為使層次更清楚if (dir.isDirectory()) {File next[]=dir.listFiles();for (int i = 0; i < next.length; i++) {for (int j = 0; j < tab; j++) {System.out.print("|---");//去掉ln。 }System.out.println(next[i].getName());if (next[i].isDirectory()) {printFiles(next[i],tab+1);?
結果:
|---.classpath
|---.project
|---.settings
|---|---org.eclipse.jdt.core.prefs
|---bin
|---|---com
|---|---|---jikexueyuan
|---|---|---|---filescan
|---|---|---|---|---main
|---|---|---|---|---|---Scanner.class
|---src
|---|---com
|---|---|---jikexueyuan
|---|---|---|---filescan
|---|---|---|---|---main
|---|---|---|---|---|---Scanner.java
?
3、? 文件的簡單讀寫:
(1)讀::
???
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("text.txt");if (file.exists()) {System.err.println("exist");try {FileInputStream fis =new FileInputStream(file);InputStreamReader isr=new InputStreamReader(fis, "UTF-8");BufferedReader br=new BufferedReader(isr);String line;while ((line=br.readLine())!=null) {System.out.println(line);}br.close();isr.close();fis.close();//先打開的后關閉,后打開的先關閉} catch (FileNotFoundException e){e.printStackTrace();}catch ( UnsupportedEncodingException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();(2)寫:
??
File newfile =new File("newtext.txt");FileOutputStream fos =new FileOutputStream(newfile);OutputStreamWriter osw =new OutputStreamWriter(fos,"UTF-8");BufferedWriter bw=new BufferedWriter(osw);try {bw.write("長歌行 漢樂府\n");bw.write("青青園中葵,朝露待日晞。\n");bw.write("陽春布德澤,萬物生光輝。\n");bw.write("常恐秋節至,焜黃華葉衰。\n");bw.write("百川東到海,何時復西歸?\n");bw.write("少壯不努力,老大徒傷悲。\n");bw.close();osw.close();fos.close();System.out.println("寫入完成");} catch (FileNotFoundException e){e.printStackTrace();}catch ( UnsupportedEncodingException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();?
總結
以上是生活随笔為你收集整理的06-Java 本地文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图的色数
- 下一篇: spring aop 环绕通知aroun