標籤:
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讀寫