<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    神秘的 J2ee 殿堂

    ·古之學(xué)者必有師·做學(xué)者亦要做師者·FIGHTING·

    Java文件操作小記

    Java文件操作小記

    Test.java
    package com.hunau.liuyong;
    import com.hunau.liuyong.FileMethod;
    public class Test{
    public
     static void main(String[] args) throws IOException
    {
    FileMethod fileMethod = new FileMethod();
    String[] ss = new String[50];
    for(int i=0;i<ss.length;i++)
    {
    ss[i] 
    = "網(wǎng)(計(jì)算機(jī)軟硬件,通訊) "+i;
    }
    System.out.println(
    "f:\\dd\\");
    fileMethod.fileIsNull("f:\\ss\\","me.txt");
    }
    }
    FileMethod.java
    package com.hunau.liuyong;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    public class FileMethod{

        
    /**
         * 
         * 文件的寫(xiě)入
         * 
         * 
    @param filePath(文件路徑)
         * 
         * 
    @param fileName(文件名)
         * 
         * 
    @param args[]
         * 
         * 
    @throws IOException
         * 
         
    */

        
    public void writeFile(String filePath, String fileName, String[] args)
                
    throws IOException {
            //1
            FileWriter fw 
    = new FileWriter(filePath + fileName);
            //2   1處可以用2處代替
            //File fw = new File(filePath+fileName);
            //fw.createNewFile();
            PrintWriter out 
    = new PrintWriter(fw);
            
    for (int i = 0; i < args.length; i++) {
                out.write(args[i]);
                out.println();
                out.flush();
            }
            fw.close();
            out.close();
        }

        
    /**
         * 
         * 文件的寫(xiě)入
         * 
         * 
    @param filePath(文件路徑)
         * 
         * 
    @param fileName(文件名)
         * 
         * 
    @param args
         * 
         * 
    @throws IOException
         * 
         
    */

        
    public void writeFile(String filePath, String fileName, String args)
                
    throws IOException

        {
            FileWriter fw 
    = new FileWriter(filePath + fileName);
            fw.write(args);
            fw.close();

        }

        
    /**
         * 
         * 創(chuàng)建與刪除文件
         * 
         * 
    @param filePath
         * 
         * 
    @param fileName
         * 
         * 
    @return 創(chuàng)建成功返回true
         * 
         * 
    @throws IOException
         * 
         
    */

        
    public boolean createAndDeleteFile(String filePath, String fileName)
                
    throws IOException {
            
    boolean result = false;
            File file 
    = new File(filePath, fileName);
            
    if (file.exists()) {
                file.delete();
                result 
    = true;
                System.out.println(
    "文件已經(jīng)刪除!");
            } 
    else {
                file.createNewFile();
                result 
    = true;
                System.out.println(
    "文件已經(jīng)創(chuàng)建!");
            }
            
    return result;
        }

        
    /**
         * 
         * 創(chuàng)建和刪除目錄
         * 
         * 
    @param folderName
         * 
         * 
    @param filePath
         * 
         * 
    @return 刪除成功返回true
         * 
         
    */

        
    public boolean createAndDeleteFolder(String folderName, String filePath)

        {
            
    boolean result = false;
            
    try {
                File file 
    = new File(filePath + folderName);
                
    if (file.exists()) {
                    file.delete();
                    System.out.println(
    "目錄已經(jīng)存在,已刪除!");
                    result 
    = true;
                } 
    else {
                    file.mkdir();
                    System.out.println(
    "目錄不存在,已經(jīng)建立!");
                    result 
    = true;
                }
            } 
    catch (Exception ex) {
                result 
    = false;
                System.out.println(
    "CreateAndDeleteFolder is error:" + ex);
            }
            
    return result;
        }

        
    /**
         * 
         * 輸出目錄中的所有文件及目錄名字
         * 
         * 
    @param filePath
         * 
         
    */

        
    public void readFolderByFile(String filePath) {
            File file 
    = new File(filePath);
            File[] tempFile 
    = file.listFiles();
            
    for (int i = 0; i < tempFile.length; i++) {
                
    if (tempFile[i].isFile()) {
                    System.out.println(
    "File : " + tempFile[i].getName());
                }
                
    if (tempFile[i].isDirectory()) {
                    System.out.println(
    "Directory : " + tempFile[i].getName());
                }
            }
        }

        
    /**
         * 
         * 檢查文件中是否為一個(gè)空
         * 
         * 
    @param filePath
         * 
         * 
    @param fileName
         * 
         * 
    @return 為空返回true
         * 
         * 
    @throws IOException
         * 
         
    */

        
    public boolean fileIsNull(String filePath, String fileName)
                
    throws IOException {
            
    boolean result = false;
            FileReader fr 
    = new FileReader(filePath + fileName);
            
    if (fr.read() == -1) {
                result 
    = true;
                System.out.println(fileName 
    + " 文件中沒(méi)有數(shù)據(jù)!");
            } 
    else {
                System.out.println(fileName 
    + " 文件中有數(shù)據(jù)!");
            }
            fr.close();
            
    return result;
        }

        /**
         * 
         * 一行一行的讀取文件中的數(shù)據(jù)
         * 
         * 
    @param filePath
         * 
         * 
    @param fileName
         * 
         * 
    @throws IOException
         * 
         
    */

        
    public void readLineFile(String filePath, String fileName)
                
    throws IOException {
            FileReader fr 
    = new FileReader(filePath + fileName);
            BufferedReader br 
    = new BufferedReader(fr);
            String line 
    = br.readLine();
            
    while (line != null) {
                System.out.println(line);
                line 
    = br.readLine();
            }
            br.close();
            fr.close();
        }
    }


    posted on 2008-07-03 10:54 月芽?jī)?/a> 閱讀(291) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): J2EE學(xué)習(xí)摘錄

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(2)

    隨筆分類(lèi)

    隨筆檔案

    相冊(cè)

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲视频在线免费看| 美女羞羞免费视频网站| 成人片黄网站A毛片免费| 亚洲成av人片天堂网无码】| 亚洲国产精品一区二区三区久久| 日韩精品在线免费观看| 亚洲中文字幕久久精品蜜桃| 久久久青草青青国产亚洲免观| 69式互添免费视频| 无遮挡呻吟娇喘视频免费播放| 亚洲经典在线观看| 亚洲美日韩Av中文字幕无码久久久妻妇| 日韩免费无码一区二区三区 | 亚洲丝袜中文字幕| 免费a级毛片18以上观看精品| 一区二区免费视频| 日韩在线观看视频免费| 亚洲一区二区三区精品视频| 亚洲综合色在线观看亚洲| 97视频热人人精品免费| 你懂的在线免费观看| 亚洲免费网站观看视频| 亚洲毛片在线观看| 亚洲第一黄色网址| 四虎永久在线精品免费网址| 精品一卡2卡三卡4卡免费视频| 久久精品亚洲日本波多野结衣 | 亚洲网站在线免费观看| 亚洲精品国产精品乱码不卞| 成年轻人网站色免费看| 三年片在线观看免费观看大全动漫 | 精品免费久久久久久久| 中出五十路免费视频| 男女男精品网站免费观看| 亚洲国产精品综合久久2007| 久久精品国产亚洲麻豆| 2048亚洲精品国产| 免费大片在线观看网站| 成人免费无码大片A毛片抽搐色欲| 在线观看免费中文视频| 任你躁在线精品免费|