java讀寫

來源:互聯網
上載者:User

標籤:

IO流下分為位元組流與字元流,每個流又分為輸入輸出以及讀寫。

位元組流的兩個基類為InputStream與OutputStream。

字元流為Reader和Writer

一般的緩衝流讀寫字元:

BufferedReader br = new BufferedReader(new FileReader("b.txt"));

如果有編碼格式的話使用:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\test\\xiong.txt"), "gbk"));

 

讀取一個文本裡面的內容那個,並隨機輸出一行內容。

public static void main(String[] args) throws IOException {        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\test\\xiong.txt"), "gbk"));        ArrayList<String> list = new ArrayList<String>();        String line = null;        while ((line=br.readLine())!=null){            list.add(line);        }        int random = new Random().nextInt(list.size());        System.out.println(list.get(random));    }

 複製檔案夾下的檔案,檔案裡有相片,視頻所以使用位元組流:

package cn;import java.io.*;public class FileDemo2 {    public static void main(String[] args) throws IOException {        File srcFolder = new File("d:\\demo");        File destFolder = new File("d:\\test");        if (!destFolder.exists()){            destFolder.mkdir();        }        File[] files = srcFolder.listFiles();        for (File file:files){            File newFile = new File(destFolder, file.getName());            copy(file, newFile);        }    }    private static void copy(File file, File newFile) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));        int len = 0;        byte[] bys = new byte[1024];        while ((len=bis.read(bys))!=-1){            bos.write(bys, 0 , len);        }
     bis.close()
    bos.close()
}}

 將一個檔案夾下的以.java結尾的文字檔複製到另一個檔案夾並改為以.jad結尾

package cn;import java.io.*;import java.util.ArrayList;import java.util.Random;/** * Created by Administrator on 2015/3/11. */public class FileDemo {    public static void main(String[] args) throws IOException {        File srcFolder = new File("c:\\java");        File destFolder = new File("c:\\jad");        if(!(destFolder.exists())){            destFolder.mkdir();        }        File[] fileArray = srcFolder.listFiles(new FilenameFilter() {            @Override            public boolean accept(File dir, String name) {                return new File(dir, name).isFile()&&name.endsWith(".java");            }        });        for(File file:fileArray){            String name = file.getName();            File newFile = new File(destFolder, name);            copy(file, newFile);        }        File[] destFileArray = destFolder.listFiles();        for (File destFile:destFileArray){            System.out.println(destFile);            String name = destFile.getName();            String newName = name.replace(".java", ".jad");            File newFile = new File(destFolder, newName);            destFile.renameTo(newFile);        }    }    private static void copy(File file, File newFile) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));        int len = 0;        byte[] bys = new byte[1024];        while ((len=bis.read(bys))!=-1){            bos.write(bys, 0 , len);        }        bis.close();        bos.close();    }}

 複製一個檔案夾下的所有內容到指定目錄

package cn;import java.io.*;import java.util.ArrayList;import java.util.Random;/** * Created by Administrator on 2015/3/11. */public class FileDemo {    public static void main(String[] args) throws IOException {        File srcFile = new File("d:\\demos");        File destFile = new File("c:\\");        copyFolder(srcFile, destFile);    }    private static void copyFolder(File srcFile, File destFile) throws IOException{        if(srcFile.isDirectory()){            File newFolder = new File(destFile, srcFile.getName());            newFolder.mkdir();            File[] fileArray = srcFile.listFiles();            for (File file:fileArray){                copyFolder(file, newFolder);            }        }else{            File newFile = new File(destFile, srcFile.getName());            copyFile(srcFile, newFile);        }    }    private static void copyFile(File srcFile, File newFile) throws IOException{        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));        int len = 0;        byte[] bys = new byte[1024];        while ((len=bis.read(bys))!=-1){            bos.write(bys, 0, len);        }        bis.close();        bos.close();    }}

 

java讀寫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.