JavaIO-創(chuàng)新互聯(lián)

這里寫目錄標(biāo)題
  • Java IO
    • 1.創(chuàng)建文件的3種方式
    • 2.獲取文件的信息
    • 3.目錄操作

成都創(chuàng)新互聯(lián)公司10多年企業(yè)網(wǎng)站建設(shè)服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及高端網(wǎng)站定制服務(wù),企業(yè)網(wǎng)站建設(shè)及推廣,對成都圍欄護(hù)欄等多個領(lǐng)域擁有豐富的網(wǎng)站營銷經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。Java IO 1.創(chuàng)建文件的3種方式
package javaio.file;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class FileCreate {//方式1 new File(String pathname)
    @Test
    public void create01(){String filePath = "E:\\文件下載\\news1.txt";

        File file = new File(filePath);

        try {file.createNewFile();
            System.out.println("文件創(chuàng)建成功");
        } catch (IOException e) {e.printStackTrace();
        }

    }

    //方式2 new File(File parent, String child)  //根據(jù)父目錄文件 + 子路勁構(gòu)建
    //E:\文件下載\news2.txt
    @Test
    public  void  create02(){File parentFile = new File("E:\\文件下載");
        String fileName = "news2.txt";
        //這里的file對象,在java程序中,只是一個對象
        //只有執(zhí)行了createNewFile 方法,才會真正的,在磁盤創(chuàng)建該文件
        File file = new File(parentFile,fileName);
        try {file.createNewFile();
            System.out.println("文件創(chuàng)建成功");
        } catch (IOException e) {e.printStackTrace();
        }

    }


    //方法3 new File(String parent, String child)  //根據(jù)父目錄文件 + 子路勁構(gòu)建
    @Test
    public  void  create03(){String parentFile = "E:\\文件下載";
        String fileName = "news3.txt";
       
        File file = new File(parentFile,fileName);
        try {file.createNewFile();
            System.out.println("文件創(chuàng)建成功");
        } catch (IOException e) {e.printStackTrace();
        }

    }

}
2.獲取文件的信息
//獲取文件的信息
    @Test
    public void ionfo(){File file = new File("E:\\文件下載\\news1.txt");
        //通用相應(yīng)的方法,得到對應(yīng)信息
        System.out.println("文件名字=" + file.getName());

        System.out.println("文件絕對路勁=" + file.getAbsolutePath());
        System.out.println("文件父級目錄=" +file.getParent());
        System.out.println("文件大小(字節(jié))=" + file.length());
        System.out.println("文件是否存在=" +file.exists());//T
        System.out.println("是不是一個文件=" + file.isFile());//T
        System.out.println("是不是一個目錄="+ file.isDirectory());//F
    }
3.目錄操作
//判斷 E:\文件下載\news1.txt 是否存在,如果存在就刪除
    @Test
    public void m1(){String filePath = "E:\\文件下載\\news1.txt";
        File file = new File(filePath);
        if(file.exists()){if(file.delete()){System.out.println(filePath + "刪除成功");
            }else {System.out.println(filePath + "刪除失敗");
            }

        }else {System.out.println(filePath +"該文件不存在...");
        }
    }

    //判斷 E:\新建文件夾  是否存在,存在就刪除,否則提示不存在
    //這里我們需要體會到,在java編程中,目錄也被當(dāng)做文件
    @Test
    public void m2(){String filePath = "E:\\新建文件夾";
        File file = new File(filePath);
        if(file.exists()){if(file.delete()){System.out.println(filePath + "刪除成功");
            }else {System.out.println(filePath + "刪除失敗");
            }

        }else {System.out.println(filePath +"該目錄不存在...");
        }
    }

    //判斷 E:\文件下載\a\b\c  目錄是否存在,如果存在就提示已經(jīng)存在,否則就創(chuàng)建
    @Test
    public void m3() {String diectoryPath = "E:\\文件下載\\a\\b\\c";
        File file = new File(diectoryPath);
        if (file.exists()) {System.out.println(diectoryPath + "存在..");

        } else {if(file.mkdirs()) {System.out.println(diectoryPath + "創(chuàng)建成功...");
            } else {System.out.println(diectoryPath + "創(chuàng)建失敗...");
            }

        }
    }

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

當(dāng)前名稱:JavaIO-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://muchs.cn/article40/eipho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、域名注冊企業(yè)建站、軟件開發(fā)網(wǎng)站導(dǎo)航、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)