package com.aptech.io;
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;
public class FileIO {
public void FileDemo() throws IOException { // java IO的核心对象
File f = new File("d:/abc.rmvb"); if (!f.exists()) { // 创建文件 f.createNewFile(); }
File f1 = new File("d:/def"); if (!f1.exists()) { // 创建文件夹 f1.mkdir(); } // 文件对象是否是目录 System.out.println(f1.isDirectory()); // 文件对象是否是文件 System.out.println(f.isFile()); // 获得文件夹下的所有文件对象 // f1.listFiles() }
public void readFileByByte() throws IOException { // Stream 字节流 InputStream is = new FileInputStream("d:\\abc.txt"); // 设置读写的缓冲大小 byte[] b = new byte[1024];
int len = -1; // 将流读入到字节数组中 while ((len = is.read(b)) != -1) { // String(字节数组,起始下标,读字节的长度) // String str=new String(b,0,len,"GBK"); String str = new String(b, 0, len); System.out.println(str); } is.close(); }
public void writeFileByByte() throws IOException { // Stream 字节流 // FileOutputStream(文件对象,是否追加) OutputStream os = new FileOutputStream("d:\\abc.txt", true); String str = "123123213\r\n324324324\r\n23432"; // 写字节数组 os.write(str.getBytes()); os.close(); }
/** * 复制文件 * *
@throws IOException * 作者:贺刚 Date:2011-7-29 */ public void copyFileByByte(String fromFile, String toPath) throws IOException { // 获得来源文件对象 File file = new File(fromFile); // 获得来源文件输入流 InputStream is = new FileInputStream(file); // 获得目标文件对象 String toFile = toPath + "/" + file.getName(); // 获得目标文件输出流 OutputStream os = new FileOutputStream(toFile); // 10k的缓冲 byte[] b = new byte[10240]; int len = -1; while ((len = is.read(b)) != -1) { os.write(b, 0, len); } os.close(); is.close();
}
// Reader writer public void readFileByChar() throws IOException { // FileReader fr=new FileReader("d:/abc.txt"); // char[] ch=new char[1024]; // int len=-1; // while((len=fr.read(ch))!=-1){ // String str=new String(ch,0,len); // System.out.println(str); // }
InputStreamReader isr = new InputStreamReader(new FileInputStream( "d:/abc.txt"), "GBK"); char[] ch = new char[1024]; int len = -1; while ((len = isr.read(ch)) != -1) { String str = new String(ch, 0, len); System.out.println(str); } }
public void copyDirectory( String fromPath, String toPath) throws IOException { File from = new File(fromPath); // 判断是否是文件夹 if (from.isDirectory()) { //创建目标文件夹 String toFilePath=toPath + "/" + from.getName(); File f=new File(toFilePath); f.mkdir(); //获得子文件对象 File[] files = from.listFiles(); if (files != null) { for (File file : files) { //是否是文件夹 if(file.isDirectory()){ //递归调用复制文件夹 copyDirectory(file.getPath(),f.getPath()); //删除目录 }else if(file.isFile()){ //是否是文件 copyFileByByte(file.getPath(),f.getPath()); //删除文件 } } }
}
}
public static void main(String[] args) throws IOException { FileIO f = new FileIO(); // f.FileDemo(); // f.readFileByByte(); // f.writeFileByByte(); // f.copyFileByByte("d:\\soft\\myeclipse-8.0.0-win32.exe", "d:/def");// f.readFileByChar(); f.copyDirectory("d:/def", "e:/test"); }}
希望能帮到你 231386375 java技术交流群 期待加入