国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

JAVA基礎(chǔ)學(xué)習(xí)day21--IO流三-File、Properties、PrintWriter與合并、分割流

2019-11-14 15:29:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、File

1.1、File概述

 

文件和目錄路徑名的抽象表示形式。

用戶界面和操作系統(tǒng)使用與系統(tǒng)相關(guān)的路徑名字符串 來(lái)命名文件和目錄。此類呈現(xiàn)分層路徑名的一個(gè)抽象的、與系統(tǒng)無(wú)關(guān)的視圖。抽象路徑名 有兩個(gè)組件

  1. 一個(gè)可選的與系統(tǒng)有關(guān)的前綴 字符串,比如盤符,"/" 表示 UNIX 中的根目錄,"http:////" 表示 Microsoft Windows UNC 路徑名。
  2. 零個(gè)或更多字符串名稱 的序列。 

字段摘要
static StringpathSeparator
與系統(tǒng)有關(guān)的路徑分隔符,為了方便,它被表示為一個(gè)字符串。
static charpathSeparatorChar
與系統(tǒng)有關(guān)的路徑分隔符。
static Stringseparator
與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符,為了方便,它被表示為一個(gè)字符串。
static charseparatorChar
與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符。
構(gòu)造方法摘要
File(File parent, String child)
根據(jù) parent 抽象路徑名和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例。
File(String pathname)
通過(guò)將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來(lái)創(chuàng)建一個(gè)新 File 實(shí)例。
File(String parent, String child)
根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例。
File(URI uri)
通過(guò)將給定的 file: URI 轉(zhuǎn)換為一個(gè)抽象路徑名來(lái)創(chuàng)建一個(gè)新的 File 實(shí)例。

 

方法摘要
booleancanExecute()
測(cè)試應(yīng)用程序是否可以執(zhí)行此抽象路徑名表示的文件。
booleancanRead()
測(cè)試應(yīng)用程序是否可以讀取此抽象路徑名表示的文件。
booleancanWrite()
測(cè)試應(yīng)用程序是否可以修改此抽象路徑名表示的文件。
intcompareTo(File pathname)
按字母順序比較兩個(gè)抽象路徑名。
booleancreateNewFile()
當(dāng)且僅當(dāng)不存在具有此抽象路徑名指定名稱的文件時(shí),不可分地創(chuàng)建一個(gè)新的空文件。
static FilecreateTempFile(String PRefix, String suffix)
在默認(rèn)臨時(shí)文件目錄中創(chuàng)建一個(gè)空文件,使用給定前綴和后綴生成其名稱。
static FilecreateTempFile(String prefix, String suffix, File directory)
在指定目錄中創(chuàng)建一個(gè)新的空文件,使用給定的前綴和后綴字符串生成其名稱。
booleandelete()
刪除此抽象路徑名表示的文件或目錄。
voiddeleteOnExit()
在虛擬機(jī)終止時(shí),請(qǐng)求刪除此抽象路徑名表示的文件或目錄。
booleanequals(Object obj)
測(cè)試此抽象路徑名與給定對(duì)象是否相等。
booleanexists()
測(cè)試此抽象路徑名表示的文件或目錄是否存在。
FilegetAbsoluteFile()
返回此抽象路徑名的絕對(duì)路徑名形式。
StringgetAbsolutePath()
返回此抽象路徑名的絕對(duì)路徑名字符串。
FilegetCanonicalFile()
返回此抽象路徑名的規(guī)范形式。
StringgetCanonicalPath()
返回此抽象路徑名的規(guī)范路徑名字符串。
longgetFreeSpace()
返回此抽象路徑名指定的分區(qū)中未分配的字節(jié)數(shù)。
StringgetName()
返回由此抽象路徑名表示的文件或目錄的名稱。
StringgetParent()
返回此抽象路徑名父目錄的路徑名字符串;如果此路徑名沒(méi)有指定父目錄,則返回 null
FilegetParentFile()
返回此抽象路徑名父目錄的抽象路徑名;如果此路徑名沒(méi)有指定父目錄,則返回 null
StringgetPath()
將此抽象路徑名轉(zhuǎn)換為一個(gè)路徑名字符串。
longgetTotalSpace()
返回此抽象路徑名指定的分區(qū)大小。
longgetUsableSpace()
返回此抽象路徑名指定的分區(qū)上可用于此虛擬機(jī)的字節(jié)數(shù)。
inthashCode()
計(jì)算此抽象路徑名的哈希碼。
booleanisAbsolute()
測(cè)試此抽象路徑名是否為絕對(duì)路徑名。
booleanisDirectory()
測(cè)試此抽象路徑名表示的文件是否是一個(gè)目錄。
booleanisFile()
測(cè)試此抽象路徑名表示的文件是否是一個(gè)標(biāo)準(zhǔn)文件。
booleanisHidden()
測(cè)試此抽象路徑名指定的文件是否是一個(gè)隱藏文件。
longlastModified()
返回此抽象路徑名表示的文件最后一次被修改的時(shí)間。
longlength()
返回由此抽象路徑名表示的文件的長(zhǎng)度。
String[]list()
返回一個(gè)字符串?dāng)?shù)組,這些字符串指定此抽象路徑名表示的目錄中的文件和目錄。
String[]list(FilenameFilter filter)
返回一個(gè)字符串?dāng)?shù)組,這些字符串指定此抽象路徑名表示的目錄中滿足指定過(guò)濾器的文件和目錄。
File[]listFiles()
返回一個(gè)抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名表示的目錄中的文件。
File[]listFiles(FileFilter filter)
返回抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過(guò)濾器的文件和目錄。
File[]listFiles(FilenameFilter filter)
返回抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過(guò)濾器的文件和目錄。
static File[]listRoots()
列出可用的文件系統(tǒng)根。
booleanmkdir()
創(chuàng)建此抽象路徑名指定的目錄。
booleanmkdirs()
創(chuàng)建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。
booleanrenameTo(File dest)
重新命名此抽象路徑名表示的文件。
booleansetExecutable(boolean executable)
設(shè)置此抽象路徑名所有者執(zhí)行權(quán)限的一個(gè)便捷方法。
booleansetExecutable(boolean executable, boolean ownerOnly)
設(shè)置此抽象路徑名的所有者或所有用戶的執(zhí)行權(quán)限。
booleansetLastModified(long time)
設(shè)置此抽象路徑名指定的文件或目錄的最后一次修改時(shí)間。
booleansetReadable(boolean readable)
設(shè)置此抽象路徑名所有者讀權(quán)限的一個(gè)便捷方法。
booleansetReadable(boolean readable, boolean ownerOnly)
設(shè)置此抽象路徑名的所有者或所有用戶的讀權(quán)限。
booleansetReadOnly()
標(biāo)記此抽象路徑名指定的文件或目錄,從而只能對(duì)其進(jìn)行讀操作。
booleansetWritable(boolean writable)
設(shè)置此抽象路徑名所有者寫權(quán)限的一個(gè)便捷方法。
booleansetWritable(boolean writable, boolean ownerOnly)
設(shè)置此抽象路徑名的所有者或所有用戶的寫權(quán)限。
StringtoString()
返回此抽象路徑名的路徑名字符串。
URItoURI()
構(gòu)造一個(gè)表示此抽象路徑名的 file: URI。
URLtoURL()
已過(guò)時(shí)。 此方法不會(huì)自動(dòng)轉(zhuǎn)義 URL 中的非法字符。建議新的代碼使用以下方式將抽象路徑名轉(zhuǎn)換為 URL:首先通過(guò) toURI 方法將其轉(zhuǎn)換為 URI,然后通過(guò) URI.toURL 方法將 URI 裝換為 URL。

1.2、File創(chuàng)建與刪除

 

import java.io.*;class FileDemo1 {    public static void main(String[] args)     {        method1();    }    public static void method1()    {        //創(chuàng)建File對(duì)象        File f1=new File("a.txt");        //第二種        File f2=new File("d://abc","b.txt");        //第三種        File dir=new File("d://bcd");        File f3=new File(dir,"c.txt");        //使用分隔符        File f4=new File("d:"+File.separator+"abc"+File.separator+"d.txt");        //輸出        System.out.println("f1="+f1);//a.txt        System.out.println("f2="+f2);//d:/abc/b.txt        System.out.println("f3="+f3);//d:/bcd/c.txt        System.out.println("f4="+f4);//d:/abc/d.txt    }}

 

創(chuàng)建文件

 

 

//創(chuàng)建文件,在指定位置創(chuàng)建文件,如果該文件已經(jīng)存在,則不創(chuàng)建,返回 false    public static void method1() throws IOException    {        File f=new File("d://file.txt");        //創(chuàng)建        sop("create: "+f.createNewFile());        //刪除,刪除失敗返回false        //sop("delete:"+f.delete());        //在退出時(shí)刪除指定文件        f.deleteOnExit();    }

 

1.3、File判斷 File獲取

 

package com.pb.day20.io.file;import java.io.File;import java.io.IOException;public class FileDemo2 {    public static void main(String[] args) throws IOException {        File f1=new File("d://a.txt");        File f2=new File("e://c.txt");        File dir1=new File("d://dbc");        File dir2=new File("d://dbc//dd//aa//d//f//dsf//fsd");        //判斷文件是否存在,不存在就創(chuàng)建        if(!f1.exists()){            f1.createNewFile();        }        //判斷文件夾是否存在        if(!dir1.exists()){            dir1.mkdir();//創(chuàng)建一層文件夾        }        //創(chuàng)建多層文件夾        if(!dir2.exists()){            dir2.mkdirs();        }        //判斷        sop("f1是文件嗎?"+f1.isFile());        sop("dir1是文件夾嗎?"+dir1.isDirectory());        sop("f1是可執(zhí)行文件嗎?"+f1.canExecute());        sop("f1可讀嗎?"+f1.canRead());        sop("f1可寫嗎?"+f1.canWrite());        sop("f1是隱藏文件嗎?"+f1.isHidden());        //獲取文件信息        sop(f1.getName());//文件名        sop(f1.getPath());//相對(duì)路徑        sop(f1.getAbsolutePath());//獲取絕對(duì)路徑,返回值是字符串        sop(f1.getAbsoluteFile());//獲取絕對(duì)路徑,返回值是File類對(duì)象        sop(f1.lastModified());//最后修改時(shí)間 長(zhǎng)整型        sop(f1.length());//文件字節(jié)大小,長(zhǎng)整型        //重命名        sop(f1.renameTo(f2));//剪切的作用            }    public static void sop(Object obj){        System.out.println(obj);    }}

 

 

1.4、File文件列表

 

package com.pb.day20.io.file;import java.io.File;import java.io.FilenameFilter;public class FileDemo3 {    public static void main(String[] args) {        method3();    }    //列出當(dāng)前目錄下的指定文件    public static void method3(){        File f=new File("d://");        File [] files=f.listFiles(new FilenameFilter() {                        @Override            public boolean accept(File dir, String name) {                //返回txt文件                return  name.endsWith(".txt");            }        });        for(File ff:files){            System.out.println(ff.getName()+"..."+ff.length());        }    }                //列表當(dāng)前目錄下的所有文件包含文件夾名字    public static void method2(){        File f=new File("c://");        String [] names=f.list();//必須是目錄,否則就是空指針異常        for(String s:names){            System.out.println(s);        }    }            //列出當(dāng)前系統(tǒng)的所有盤符    public static void method1(){        File [] files=File.listRoots();        for(File f:files){            System.out.println(f);        }            }}

 

1.6、File列出所有遞歸

 

package com.pb.day20.io.file;/** * 遞歸調(diào)用, * 列表指定目錄下的,所有文件包括子目錄中的文件 */import java.io.File;public class FileDemo4 {    public static void main(String[] args) {        File dir=new File("d://");        getFiles(dir);    }        //遞歸    public static void getFiles(File dir){        System.out.println(dir);        //獲取文件數(shù)組,列表        File[] files=dir.listFiles();        //遍歷        for(File file:files){            //判斷是不是文件夾        if(file.isDirectory()){            //是文件夾就再次調(diào)用            getFiles(file);        }else{            //不是就列表            System.out.println(file);        }        }    }}

 

遞歸:

限定條件

注意遞歸的次數(shù),盡量避免內(nèi)存溢出

 

1.7、File刪除帶文件的目錄

 

package com.pb.day20.io.file;import java.io.File;public class FileDemo5 {    public static void main(String[] args) {        File dir=new File("d://kk");        removeDir(dir);    }    //刪除,帶有文件的多層文件夾    public static void removeDir(File dir){        File [] files =dir.listFiles();        for(int x=0;x<files.length;x++){            if(files[x].isDirectory()){                removeDir(files[x]);            }else{                //刪除文件            System.out.println(files[x]+"----file----"+files[x].delete());            }        }        //刪除空文件夾        System.out.println(dir+"----dir----"+dir.delete());            }}

 

1.8、將一個(gè)文件夾的的所有文件,包含子目錄都復(fù)制要另一個(gè)目錄下

 

/*將一個(gè)文件夾的的所有文件,包含子目錄都復(fù)制要另一個(gè)目錄下*/import java.io.*;class IOCopy {    public static void main(String[] args)     {        //源文件        File src=new File("D://music");        //目的文件        File des=new File("e://test");        copyDir(src,des);        System.out.println("復(fù)制完成!");    }        /*    遞歸復(fù)制    */    public static void copyDir(File src,File des)    {            //獲取列表        File[] files=src.listFiles();        //判斷目標(biāo)文件是否存在        if(!des.exists()){            //創(chuàng)建目錄            des.mkdir();        }        //遍歷        for(File f:files)        {                //判斷是不是目錄            if(f.isDirectory())            {                //獲取路徑,拼接文件名,再次調(diào)用自己            copyDir(new File(f.getPath()),new File(des+"http://"+f.getName()));            //是不是文件            }else if (f.isFile())            {                //復(fù)制                copyFile(new File(f.getPath()),new File(des+"http://"+f.getName()));            }                }    }    //使用IO流復(fù)制文件    public static void copyFile(File src,File des)    {        BufferedInputStream bis=null;        BufferedOutputStream bos=null;        try        {            bis=new BufferedInputStream(new FileInputStream(src));            bos=new BufferedOutputStream(new FileOutputStream(des));            //聲明緩沖區(qū)            int len=0;            while((len=bis.read())!=-1)            {                bos.write(len);            }        }        catch (IOException e)        {             e.printStackTrace();          }finally        {            try        {            if(bos!=null)                bos.close();        }        catch (IOException e)        {             e.printStackTrace();          }        try        {            if(bis!=null)                bis.close();        }        catch (IOException e)        {             e.printStackTrace();          }                    }    }}

 

 

二、Properties

2.1、Properties概述

 

構(gòu)造方法摘要
Properties()
創(chuàng)建一個(gè)無(wú)默認(rèn)值的空屬性列表。
Properties(Properties defaults)
創(chuàng)建一個(gè)帶有指定默認(rèn)值的空屬性列表。

方法摘要
StringgetProperty(String key)
用指定的鍵在此屬性列表中搜索屬性。
StringgetProperty(String key, String defaultValue)
用指定的鍵在屬性列表中搜索屬性。
voidlist(PrintStream out)
將屬性列表輸出到指定的輸出流。
voidlist(PrintWriter out)
將屬性列表輸出到指定的輸出流。
voidload(InputStream inStream)
從輸入流中讀取屬性列表(鍵和元素對(duì))。
voidload(Reader reader)
按簡(jiǎn)單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對(duì))。
voidloadFromxml(InputStream in)
將指定輸入流中由 XML 文檔所表示的所有屬性加載到此屬性表中。
Enumeration<?>propertyNames()
返回屬性列表中所有鍵的枚舉,如果在主屬性列表中未找到同名的鍵,則包括默認(rèn)屬性列表中不同的鍵。
voidsave(OutputStream out, String comments)
已過(guò)時(shí)。 如果在保存屬性列表時(shí)發(fā)生 I/O 錯(cuò)誤,則此方法不拋出 IOException。保存屬性列表的首選方法是通過(guò) store(OutputStream out, String comments) 方法或 storeToXML(OutputStream os, String comment) 方法來(lái)進(jìn)行。
ObjectsetProperty(String key, String value)
調(diào)用 Hashtable 的方法 put
voidstore(OutputStream out, String comments)
以適合使用 load(InputStream) 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對(duì))寫入輸出流。
voidstore(Writer writer, String comments)
以適合使用 load(Reader) 方法的格式,將此 Properties 表中的屬性列表(鍵和元素對(duì))寫入輸出字符。
voidstoreToXML(OutputStream os, String comment)
發(fā)出一個(gè)表示此表中包含的所有屬性的 XML 文檔。
voidstoreToXML(OutputStream os, String comment, String encoding)
使用指定的編碼發(fā)出一個(gè)表示此表中包含的所有屬性的 XML 文檔。
Set<String>stringPropertyNames()
返回此屬性列表中的鍵集,其中該鍵及其對(duì)應(yīng)值是字符串,如果在主屬性列表中未找到同名的鍵,則還包括默認(rèn)屬性列表中不同的鍵。

Properties 類表示了一個(gè)持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。 

 

2.2、存取功能

 

public static void setAndGet(){        //聲明        Properties prop=new Properties();        //設(shè)置        prop.setProperty("username", "root");        prop.setProperty("passWord","root");        //獲取用戶名對(duì)應(yīng)的值        System.out.println(prop.getProperty("username"));        //修改密碼        prop.setProperty("password","root123");        //獲取全部key        Set<String> keys=prop.stringPropertyNames();        //遍歷        for(String s:keys){            System.out.println(s+": "+prop.getProperty(s));        }    }

 

2.3、存取配置文件

 

    public static void method2() throws IOException {        // 聲明Properties集合        Properties prop = new Properties();        FileInputStream fis = new FileInputStream("D:/info.txt");                FileOutputStream fos = new FileOutputStream("D://info.txt");            //加載數(shù)組到集合        prop.load(fis);        System.out.println(prop);        prop.list(System.out);                //修改        prop.setProperty("sex", "woman");        prop.store(fos, "sex is woman");        System.out.println(prop);        fos.close();        fis.close();    }    public static void method1() throws IOException {        // 文件流        BufferedReader br = new BufferedReader(new FileReader("D://info.txt"));        // 聲明Properties集合        Properties prop = new Properties();        String line = null;        // 遍歷        while ((line = br.readLine()) != null) {            // System.out.println(line);            String[] arr = line.split("=");            // System.out.println(arr[0]+"..."+arr[1]);            prop.setProperty(arr[0], arr[1]);        }        System.out.println(prop);        br.close();    }

 

 

 

示例

 

public static void method() throws IOException{        File file=new File("a.txt");                //判斷文件是否存在        if(!file.exists()){            file.createNewFile();        }        FileInputStream fis=new FileInputStream(file);        //聲明集合        Properties prop=new Properties();        prop.load(fis);        int count=0;        String value=prop.getProperty("times");        if(value!=null){            count=Integer.parseInt(value);            //判斷次數(shù)            if(count>=5){                System.out.println("使用次數(shù)已經(jīng)用完,請(qǐng)續(xù)費(fèi)!");                return;            }        }        //次數(shù)+1        count++;        //把KEY和VALUE放回去        prop.setProperty("times", count+"");        FileOutputStream fos=new FileOutputStream(file);        prop.store(fos, "次數(shù)+1");        fos.close();        fis.close();    }

 

 

 

三、PrintWriter與序列流

3.1、PrintWriter與PrintStream

 

package com.pb.io.properties.demo1;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;/** * 從鍵盤接收數(shù)據(jù),并輸出在控制臺(tái) * 使用PrintWriter * */public class PrintWriterDemo {    public static void main(String[] args) throws IOException {        //從鍵盤接收數(shù)據(jù),并輸出在控制臺(tái)        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));                //使用打印流輸出,并自動(dòng)刷新        PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)),true);                String line=null;        System.out.println("請(qǐng)輸入內(nèi)容,以over結(jié)束!");        while((line=br.readLine())!=null){            if("over".equals(line)){                break;            }            pw.println(line);        }        pw.close();        br.close();    }}

 

 

3.2、序列流--SequenceInputStream

SequenceInputStream 表示其他輸入流的邏輯串聯(lián)。它從輸入流的有序集合開始,并從第一個(gè)輸入流開始讀取,直到到達(dá)文件末尾,接著從第二個(gè)輸入流讀取,依次類推,直到到達(dá)包含的最后一個(gè)輸入流的文件末尾為止。

構(gòu)造方法摘要
SequenceInputStream(Enumeration<? extends InputStream> e)
通過(guò)記住參數(shù)來(lái)初始化新創(chuàng)建的 SequenceInputStream,該參數(shù)必須是生成運(yùn)行時(shí)類型為 InputStream 對(duì)象的 Enumeration 型參數(shù)。
SequenceInputStream(InputStream s1, InputStream s2)
通過(guò)記住這兩個(gè)參數(shù)來(lái)初始化新創(chuàng)建的 SequenceInputStream(將按順序讀取這兩個(gè)參數(shù),先讀取 s1,然后讀取 s2),以提供從此 SequenceInputStream 讀取的字節(jié)。

方法摘要
intavailable()
返回不受阻塞地從當(dāng)前底層輸入流讀取(或跳過(guò))的字節(jié)數(shù)的估計(jì)值,方法是通過(guò)下一次調(diào)用當(dāng)前底層輸入流的方法。
voidclose()
關(guān)閉此輸入流并釋放與此流關(guān)聯(lián)的所有系統(tǒng)資源。
intread()
從此輸入流中讀取下一個(gè)數(shù)據(jù)字節(jié)。
intread(byte[] b, int off, int len)
將最多 len 個(gè)數(shù)據(jù)字節(jié)從此輸入流讀入 byte 數(shù)組。

 

 

 

 

四、合并流

4.1、合并流

 

package com.pb.io.properties.demo1;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Enumeration;import java.util.Vector;public class SequenceDemo {    public static void main(String[] args) throws IOException {                Vector<FileInputStream> v=new Vector<FileInputStream>();        v.add(new FileInputStream("d://1.txt"));        v.add(new FileInputStream("d://2.txt"));        v.add(new FileInputStream("d://3.txt"));        //轉(zhuǎn)換為枚舉        Enumeration<FileInputStream> en=v.elements();        //合并流        SequenceInputStream sis=new SequenceInputStream(en);        //目標(biāo)文件        FileOutputStream fos=new FileOutputStream("d://4.txt");        //聲明緩沖區(qū)        byte[] buf=new byte[1024];        int len=0;        while((len=sis.read(buf))!=-1){            fos.write(buf,0,len);        }        fos.close();        sis.close();    }}

 

 

 

五、分割流

5.1、分割流

 

package com.pb.io.properties.demo1;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;/** * 將文件切割 * 再合并 * */public class SplitDemo {    public static void main(String[] args) throws IOException {            //split();        merge();    }    //合并    public static void merge() throws IOException{        File src=new File("D://split");        File [] files=src.listFiles();        ArrayList<FileInputStream> list=new ArrayList<FileInputStream>();        for(File f:files){            list.add(new FileInputStream(f));        }        Iterator<FileInputStream> it=list.iterator();        //創(chuàng)建枚舉        Enumeration<FileInputStream> en=new Enumeration<FileInputStream>() {                        @Override            public FileInputStream nextElement() {                                return it.next();            }            @Override            public boolean hasMoreElements() {                return it.hasNext();            }        };                //創(chuàng)建合并流        SequenceInputStream sis=new SequenceInputStream(en);        FileOutputStream fos=new FileOutputStream("D://split//t.mp3");        byte[] buf=new byte[1024];        int len=0;        while((len=sis.read(buf))!=-1){            fos.write(buf,0,len);        }        fos.close();        sis.close();            }                //切割    public static void split() throws IOException{        //源文件        File file=new File("D://gm.mp3");                FileInputStream fis=new FileInputStream(file);        //目標(biāo)        FileOutputStream fos=null;        //緩沖區(qū)        byte[] buf=new byte[1024*1024];        int len=0;        int count=1;        while((len=fis.read(buf))!=-1){            fos=new FileOutputStream("D://split//"+(count++)+".part");            fos.write(buf,0,len);        }        fos.close();        fis.close();    }}

 

 

 

 

 

 

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 文成县| 得荣县| 财经| 廉江市| 长宁区| 大荔县| 西充县| 曲水县| 淳安县| 四川省| 瑞安市| 甘南县| 石门县| 贡觉县| 德安县| 西乌珠穆沁旗| 项城市| 邵阳县| 邓州市| 呼和浩特市| 贵港市| 聂拉木县| 遵化市| 盐城市| 孟村| 康平县| 徐水县| 商都县| 津南区| 龙游县| 桦川县| 乌海市| 台北县| 临夏县| 天峨县| 临沂市| 威信县| 乌审旗| 长寿区| 湖南省| 通许县|