<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 月芽兒 閱讀(291) 評論(0)  編輯  收藏 所屬分類: J2EE學習摘錄

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    相冊

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成人精品综合免费视频| 国产亚洲综合一区二区三区| 毛片基地看看成人免费| 亚洲AⅤ无码一区二区三区在线| 亚洲日本VA中文字幕久久道具| 91香蕉成人免费网站| 亚洲成在人线中文字幕| 亚洲视频免费观看| 亚洲一区二区三区免费观看| 999久久久免费精品国产| 亚洲成A人片在线播放器| 午夜寂寞在线一级观看免费| 豆国产96在线|亚洲| 亚洲精品无码av天堂| 三级黄色免费观看| 91亚洲va在线天线va天堂va国产| 亚洲一区免费在线观看| 亚洲日韩国产一区二区三区在线 | 午夜老司机永久免费看片| 亚洲视频在线一区二区三区| 国产精品久久永久免费| 亚洲AV综合色区无码一二三区 | 啦啦啦www免费视频| 老湿机一区午夜精品免费福利| 国产黄色一级毛片亚洲黄片大全| 最新久久免费视频| 亚洲人成7777影视在线观看| 国产免费av片在线播放| a毛片免费全部在线播放**| 亚洲欧洲自拍拍偷午夜色| 国产成人免费一区二区三区| 国产免费A∨在线播放| 亚洲精品美女久久久久9999| 女人18毛片免费观看| j8又粗又长又硬又爽免费视频| 亚洲精品自产拍在线观看动漫| 免费无码AV片在线观看软件| 亚欧国产一级在线免费| 亚洲国产美女精品久久| 亚洲国产日韩在线观频| 2021免费日韩视频网|