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

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

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

    神秘的 J2ee 殿堂

    ·古之學者必有師·做學者亦要做師者·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] 
    = "網(計算機軟硬件,通訊) "+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{

        
    /**
         * 
         * 文件的寫入
         * 
         * 
    @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();
        }

        
    /**
         * 
         * 文件的寫入
         * 
         * 
    @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();

        }

        
    /**
         * 
         * 創建與刪除文件
         * 
         * 
    @param filePath
         * 
         * 
    @param fileName
         * 
         * 
    @return 創建成功返回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(
    "文件已經刪除!");
            } 
    else {
                file.createNewFile();
                result 
    = true;
                System.out.println(
    "文件已經創建!");
            }
            
    return result;
        }

        
    /**
         * 
         * 創建和刪除目錄
         * 
         * 
    @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(
    "目錄已經存在,已刪除!");
                    result 
    = true;
                } 
    else {
                    file.mkdir();
                    System.out.println(
    "目錄不存在,已經建立!");
                    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());
                }
            }
        }

        
    /**
         * 
         * 檢查文件中是否為一個空
         * 
         * 
    @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 
    + " 文件中沒有數據!");
            } 
    else {
                System.out.println(fileName 
    + " 文件中有數據!");
            }
            fr.close();
            
    return result;
        }

        /**
         * 
         * 一行一行的讀取文件中的數據
         * 
         * 
    @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 月芽兒 閱讀(296) 評論(0)  編輯  收藏 所屬分類: J2EE學習摘錄

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    相冊

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费在线观看黄网站| 亚洲电影中文字幕| 1000部拍拍拍18免费网站| 亚洲av女电影网| 亚洲产国偷V产偷V自拍色戒| 国产JIZZ中国JIZZ免费看| 国产成人精品亚洲精品| 午夜小视频免费观看| 狼人大香伊蕉国产WWW亚洲| 亚洲成人福利在线观看| 国产亚洲高清不卡在线观看| 免费在线观看日韩| 免费在线一级毛片| 久久精品国产亚洲5555| 久久久久亚洲爆乳少妇无| 亚洲伊人久久综合中文成人网| 国产a级特黄的片子视频免费| 色播在线永久免费视频| 又粗又黄又猛又爽大片免费| 亚洲国产一区二区视频网站| 精品久久8x国产免费观看| 国产成人精品免费视频网页大全| 99久久国产免费-99久久国产免费| 亚洲日韩一区二区一无码| 国产亚洲3p无码一区二区| 一区二区三区亚洲| 国产亚洲日韩在线三区| 午夜亚洲国产理论秋霞| 亚洲午夜在线播放| 免费视频成人国产精品网站| 成年女人A毛片免费视频| 大片免费观看92在线视频线视频| 亚洲男人的天堂网站| a毛片成人免费全部播放| 最近最新MV在线观看免费高清| 精品久久久久久无码免费| 无码一区二区三区免费视频| 青青草原亚洲视频| 亚洲精品欧美综合四区| 99在线观看视频免费| 男人的天堂亚洲一区二区三区 |