Java中的IO 数据流分为: •处理流 对以存在的封装和链接。 •字节流 与文件链接 Input 和 output Stream代表任何产出数据或者接收数据的数据源。 BufferFile 通过缓存减少Io磁盘操作 File类:构造文件对象,对象中的常用属性。 File file = new File (); File.isFile(); //是否为文件? File.isDirectory(); //是否为盘符 File.listfiles(); //返回一个list数组,显示当前级别的下一级目录 字节导向: 8位字节 Byte[] : 字节在使用时的失误。如果指定的字节为2,在读取如:[aaa测试]一行,那么,当读取到字符时即会出现乱码问题。 可以读取图片。 •inputStream(); package demo; import java.io.FileInputStream; public class FileInputStreamTest { /** * 以字节流的方式读取文件中的数据 * @param args */ public static void main(String[] args) throws Exception { FileInputStream inputStream = new FileInputStream("D://0809//io1.txt"); byte[] data = new byte[12]; int count; while((count = inputStream.read(data))!=-1){ System.out.print(new String(data,0,count)); } inputStream.close(); } } •inputStream(); package demo; import java.io.FileOutputStream; public class FileOutputStreamTest { /** * 以字节流的方式写入文件中的数据 * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub FileOutputStream outputStream = new FileOutputStream("D://0809//io1.txt"); outputStream.write("aa".getBytes()); outputStream.close(); } } 字符导向: 16位字节 Char[] : 如果确定读取的是文本文件,则使用FileRead对象去读取。 FileRead read = new FilRead(); •Read(); package demo; import java.io.FileReader; public class FileReaderTest { /** * 以字符流的方式读取指定文件 * @param args */ public static void main(String[] args) throws Exception { FileReader reader = new FileReader("D://0809//io1.txt"); char[] data = new char[2]; int count; while((count = reader.read(data))!=-1){ System.out.print(new String(data,0,count)); } reader.close(); } } •Write(); package demo; import java.io.FileInputStream; import java.io.FileOutputStream; public class InputStream_and_OutputStream { /** * 从一个文件读取数据并将数据写入到另一个文件中 * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub FileInputStream inputStream = new FileInputStream("d://io//y.txt"); FileOutputStream outputStream = new FileOutputStream("d://io//yyyyy.txt"); byte[] data = new byte[2]; int count; while((count = inputStream.read(data))!=-1){ // System.out.print(new String(data,0,count)); outputStream.write(data,0,count); } inputStream.close(); outputStream.close(); } } 垃圾只负责回收对象,不回收其他资源,所以必须关闭数据流。 使用递归算法获取某个文件夹的文件名 import java.io.*; import java.util.*; @SuppressWarnings("unchecked") public class Read_Copy { public static void main(String[] args) throws Exception { File c=new File("D://Dawn"); getfile(c); System.out.println("---------------------"); getDire(c); } public static void getfile(File f) { File[] files=f.listFiles(); for(File f1:files) { //如果为文件 if(f1.isFile()) System.out.println(f1); //如果为目录 if(f1.isDirectory()) //调用自身的函数或方法[递归算法] getfile(f1); } } } //打印目录 public static void getDire(File f) { File[] files=f.listFiles(); for(File f1:files) { if(f1.isDirectory()) { System.out.println(f1); //调用自身的函数或方法[递归算法] getDire(f1); } } } } 复制文件夹中的文件到指定文件夹中 方式一: package demo; import java.io.*; import java.util.*; public class CopyFile { String intPathOut; //初始输出路径 int count = 0; //计数器,用于判定是否首次调用copyFiles方法 public void copyFiles(String pathIn,String pathOut) throws Exception { File file = new File(pathIn); if(!file.exists()) { System.out.println("源文件路径不存在!!"); System.exit(5); } if(count==0) { File out = new File(this.intPathOut); out.mkdir(); // 首次调用执行,创建初始输出目录 } if(file.isFile()) { this.copyFile(file, pathOut); } else { this.copyDir(file, pathOut); } count++; } private void copyDir(File file, String pathOut) throws Exception { System.out.println(file.getPath()); pathOut = this.intPathOut+file.getPath().substring(2); //修改输出路径字符串,将子文件夹相对路径追加至初始输出路径,substring用于去掉路径中的驱动器盘符 File dir = new File(pathOut); dir.mkdir(); File temp[] = file.listFiles(); for (int i = 0; i < temp.length; i++) { this.copyFiles(temp[i].toString(), pathOut); //递归调用此方法 } } private void copyFile(File file, String pathOut) throws Exception { System.out.println(file.getPath()); DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file.getPath()))); byte[] date = new byte[in.available()]; in.read(date); DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(pathOut+"/"+file.getName()))); out.write(date); in.close(); out.close(); } public static void main(String[] args) throws Exception { CopyFile copy = new CopyFile(); copy.intPathOut="C:/22"; copy.copyFiles("D:/Y2", "C:/22"); System.out.println("复制完毕"); } } 方式二: import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class test { public static final String subPath = "//"; public static void main(String[] args) { test t = new test(); t.copeFile("C:/CorelDRAW 12", "D:/aaa"); } private File inFile = null; private File outFile = null; private InputStream input = null; private OutputStream output = null; public void copeFile(String in, String out) { this.inFile = new File(in); this.outFile = new File(out); if(inFile.exists() && outFile.exists()) this.outFile(inFile, outFile); } private int num = 0; public void outFile(File inFile, File outFile) { num++; String inName = ""; if(num == 1) inName = inFile.getName() + test.subPath; String out = outFile.getPath(); for(File f : inFile.listFiles()) { if(f.isFile()) { try { input = new FileInputStream(f); output = new FileOutputStream(out + test.subPath + inName + f.getName()); byte[] b = new byte[(int)f.length()]; input.read(b); output.write(b); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { input.close(); output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { System.out.println(out + test.subPath + inName + f.getName()); File ff = new File(out + test.subPath + inName + f.getName()); ff.mkdir(); this.outFile(f,ff); } } } }