bj java 初學 2015-07-23

目錄

程序1:讀取E盤下的所有的.zip 文件夾
程序2:列出E:\BaiduYunDownload下的所有的.zip 文件夾的絕對路徑(FilenameFilter)
程序3:讀取E:\ccg.txtE盤ccg.txt文件中的內容在控制檯顯示。(FileInputStream)
程序4:向E:\123.txt寫入數據(FileOutputStream)
程序5:複製E:\ccg.txt中內容到E:\q.txt(FileInputStream和FileOutputStream)
程序6:用BufferedReader讀取E:\123.txt中的內容
程序7:用BufferedWriter向E:\test_g.txt中寫入數據
程序8:打印流PrintStream的使用,
程序9:解析E:\Test_i_XML.txt,顯示這個文件中的信息
程序10:解析E:\Test_j_SAX.txt這個文件,並交由Test_j_MySAXHander類對象mySAXHander來處理

程序1:

/*讀取E盤下的所有的.zip 文件夾的名稱
 * */
package com.day8_2015_07_23;
import java.io.File;
public class Test_a_File {
public static void main(String[] args) {
    ergodicFile("E://");

}
    public static void  ergodicFile(String filePath)
    {
        File file = new File(filePath);
        //調用.listFiles()方法,返回值是文件的絕對路徑
        File[] abstractFilePath = file.listFiles();
        for (int i = 0; i < abstractFilePath.length; i++) {
            //得到E盤下所有文件的目錄
            //System.out.println(abstractFilePath[i]);
            if(abstractFilePath[i].isDirectory())//判斷該抽象路徑是否是文件夾
            {
                ergodicFile(abstractFilePath[i].getAbsolutePath());
            }
            else
            {
                if(abstractFilePath[i].getName().endsWith(".zip"))
                {    //abstractFilePath得到.zip文件的路徑。。。。
                    //abstractFilePath[i].getName()得到得到這個路徑中的.zip文件的文件名。
                    System.out.println(abstractFilePath[i].getName());
                }
            }

        }
    }
}

運行結果:
這裏寫圖片描述

程序2:

/*列出E:\\BaiduYunDownload下的所有的.zip 文件夾的絕對路徑
 * */
package com.day8_2015_07_23;
import java.io.File;
public class Test_b_main {
    public static void main(String[] args) {
        Test_b_fileFilter filter = new Test_b_fileFilter();
        File file = new File("E:\\BaiduYunDownload");
        File[] absulateFilePath = file.listFiles(filter);
        for (int i = 0; i < absulateFilePath.length; i++) {
            System.out.println(absulateFilePath[i]);
        }
    }

}
/*得到E盤根目錄下的.zip 文件
 * */
package com.day8_2015_07_23;
import java.io.File;
import java.io.FilenameFilter;
public class Test_b_fileFilter implements FilenameFilter{
    @Override
    public boolean accept(File dir, String name) {
        // TODO Auto-generated method stub
        return name.endsWith(".zip");
    }
}

運行結果:
這裏寫圖片描述

程序3

/*讀取E:\\ccg.txtE盤ccg.txt文件中的內容在控制檯顯示。
 * */
package com.day8_2015_07_23;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test_c_FileRead {
    public static void main(String[] args) {
        File file = new File("E:\\ccg.txt");
        FileInputStream in =null;
        try 
        {
            in = new FileInputStream(file);
            byte[] array = new byte[2048];
            int i = in.read(array);

            while(i!=-1)
            {
                System.out.println("讀取的字節數:"+i);
                System.out.println(new String(array,0,i));
                i = in.read(array);
            }
        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

運行結果:
這裏寫圖片描述

程序4:

/*向E:\\123.txt寫入數據
 * */
package com.day8_2015_07_23;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test_d_FileWrite {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        File file = new File("E:\\123.txt");
        if(!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            out.write(s.getBytes());
            out.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }

}

運行結果:
這裏寫圖片描述

程序5:

/*複製E:\\ccg.txt中內容到E:\\q.txt
 * */
package com.day8_2015_07_23;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test_e_CopyFile {
    public static void main(String[] args) {
        copy("E:\\ccg.txt","E:\\q.txt");

    }
    public static void copy(String source,String destination)
    {
        File SouFile = new File(source);
        File desFile = new File(destination);
        if(!SouFile.exists())
        {
            System.out.println("源文件不存在");
            return;
        }
        if(!desFile.exists())
        {
            try {
                desFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            FileInputStream in = new FileInputStream(SouFile);
            FileOutputStream out = new FileOutputStream(desFile);
            byte[] array = new byte[16];
            int i = 0;
            while(i!=-1)
            {
                i = in.read(array);
                System.out.println("此時i是:"+i);
                if(i!=-1)
                {
                    out.write(array, 0, i);
                }

            }
            in.close();
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }*/

    }

}

運行結果:
這裏寫圖片描述

程序6:

/*用BufferedReader讀取E:\\123.txt中的內容
 * */
package com.day8_2015_07_23;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test_f_BufferedReader {
    public static void main(String[] args) {
        File file = new File("E:\\123.txt");
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))) ;
            String s = br.readLine();
            while(s!=null)
            {
                System.out.println(s);
                s = br.readLine();
            }
            br.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

運行結果:
這裏寫圖片描述


程序7:

/*用BufferedWriter向E:\\test_g.txt中寫入數據
 * */
package com.day8_2015_07_23;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test_g_BufferedWriter {
    public static void main(String[] args) {
        File file = new File("E:\\test_g.txt");
        char[] arr = {'a','b','c','d'};
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bw.write("這是我通過BufferedWriter寫進去的。");
            bw.newLine();
            bw.write(arr);
            bw.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

運行結果:
這裏寫圖片描述

程序8

/*打印流PrintStream的使用,
 * */
package com.day8_2015_07_23;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class Test_h_PrintStream {
    public static void main(String[] args) {
        File file = new File("E:\\Test_h_PrintStream.txt");
        try {
            //創建具有指定文件且不帶自動行刷新的新打印流
            PrintStream ps = new PrintStream(file);
            System.setOut(ps);
            System.out.println("常燦光");
            System.out.println("Angelababy");
            System.out.println("Alin");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

運行結果:
這裏寫圖片描述

程序9

/*解析E:\\Test_i_XML.txt,顯示這個文件中的信息
 * */
package com.day8_2015_07_23;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test_i_XML {
    public static void main(String[] args) {
        File file = new File("E:\\Test_i_XML.txt");
        //1得到DOM解析器的編碼實例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            //2從DOM工廠獲得DOM解析器
            DocumentBuilder db = dbf.newDocumentBuilder();
            //3解析XML文檔,得到一個Document,即DOM樹
            Document doc =  db.parse(file);
            //4得到所有Weather節點列表信息
            NodeList weathers = doc.getElementsByTagName("Weather");
            for (int i = 0; i < weathers.getLength(); i++) {
                Node weather = weathers.item(i);
                //System.out.println(weather);
                String s = weather.getAttributes().getNamedItem("name").getNodeValue();
                System.out.println(s);
                for (Node node = weather.getFirstChild(); node!=null; node = node.getNextSibling()) {
                    if(node.getNodeType()==Node.ELEMENT_NODE)
                    {
                        //System.out.println(node.getNodeName());
                        if(node.getFirstChild()!=null)
                        {
                            //System.out.println(node.getFirstChild().getNodeValue() );
                        }
                    }
                }

            }

        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

運行結果:
這裏寫圖片描述
這裏寫圖片描述

程序10:

/*解析E:\\Test_j_SAX.txt這個文件,並交由Test_j_MySAXHander類對象mySAXHander來處理
 * */
package com.day8_2015_07_23; 
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class Test_j_SAX {
    public static void main(String[] args) {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        File file = new File("E:\\Test_j_SAX.txt");
        SAXParser parser;
        try {
            parser = factory.newSAXParser();
            Test_j_MySAXHander mySAXHander = new Test_j_MySAXHander();
            parser.parse(file, mySAXHander);
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
package com.day8_2015_07_23;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Test_j_MySAXHander extends DefaultHandler  {
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub
        super.characters(ch, start, length);
        System.out.println("標籤內容"+new String(ch, start, length));
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
        System.out.println("結束解析");
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        super.endElement(uri, localName, qName);
        System.out.println("結束標籤");
    }
    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.startDocument();
        System.out.println("開始解析");
    }
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        super.startElement(uri, localName, qName, attributes);
        System.out.println("開始標籤"+qName);
        if(qName.equals("Weather"))
        {
            System.out.println("Attributes"+attributes.getValue("name"));
        }
    }
}

運行結果:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章