文件复制 public static void main(String[] args)throws IOException{ String str=new String(); try{ BufferedReader in=new BufferedReader(new FileReader("c:\\7\\1.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("c:\\7\\8.txt")); while((str=in.readLine())!=null){ System.out.println(str); out.write(str); out.newLine(); } out.flush(); in.close(); out.close( ); }catch(IOException ex){ System.out.println("错误!"+ex); } } 对文件的随机访问 类 RandomAccessFile 构造函数: RandomAccessFile(String name,String mode) 参数name:确定一个文件名,给出流的源(目的地),参数mode取r(只读)、rw(可读写) RandomAccessFile(File name,String mode) 参数File:是一个File对象,给出流的源(目的地),提供一个方法: seek(long a) 定位RandomAccessFile流的读写位置。参数a为读写位置距离文件开头的字节个数。 getFilePoint( )方法获得流的当前读写位置。 public static void main(String[] args) throws IOException { try{ RandomAccessFile in; int data[]={1,2,3,4,5,6,7,8,9,10}; in=new RandomAccessFile("c:\\tom.out","rw"); for(int i=0;i<data.length;i++) in.writeInt(data[i]); for(long j=data.length-1;j>=0;j--){ in.seek(j*4); System.out.print(","+in.readInt()); } in.close( ); }catch(Exception ex){ } try{ RandomAccessFile in; in=new RandomAccessFile("c:\\FileInputStreamDemo.java","rw"); long filePoint=0; long fileLength=in.length(); while(filePoint<fileLength){ String s=in.readLine(); System.out.println(s); filePoint=in.getFilePointer(); } in.close( ); }catch(Exception ex){ } } DataInputStream 和 DataOutputStream 构造方法 DataInputStream(InputStream in) DataOutputStream(OutputStream out) Serialization串行化(序列化) 当对象保存到文件或从文件中读取对象时,对象必须实现Serialization序列化。 对象必须实现接口 java.io.Serializable public class Date extends Object implements Serializable, Cloneable, Comparable ObjectInputStream 和 ObjectOutputStream 可以传对象 构造方法 ObjectInputStream(InputStream in) ObjectOutputStream(OutputStream out) 方法:readObject()/writeObject() class Student implements Serializable{ String name; double height; Student(String name,double height){ this.name=name; this.height=height; } public void setHeight(double c){ this.height=c; } } public static void main(String[] args) throws IOException { Student stu=new Student("张三",1.65); try{ FileOutputStream out=new FileOutputStream("c:\\k.txt"); ObjectOutputStream object_out=new ObjectOutputStream(out); object_out.writeObject(stu); FileInputStream in=new FileInputStream("c:\\k.txt"); ObjectInputStream object_in=new ObjectInputStream(in); Student li=(Student)object_in.readObject(); li.setHeight(1.78); li.name="李四"; System.out.println(stu.name+stu.height); System.out.println(li.name+li.height); }catch(Exception ex){ } 对象的复制 public static void main(String[] args) throws IOException { Student stu=new Student("张三",1.65); try{ ByteArrayOutputStream outone=new ByteArrayOutputStream( ); ObjectOutputStream object_out=new ObjectOutputStream(outone); object_out.writeObject(stu); ByteArrayInputStream inone=new ByteArrayInputStream(outone.toByteArray()); ObjectInputStream intwo=new ObjectInputStream(inone); Student li=(Student)intwo.readObject(); System.out.println(stu.name+stu.height); System.out.println(li.name+li.height); }catch(Exception ex){ } } java文件的管理 创建文件夹:在指定的目录下创建文件夹temp File file2=new File("c:\\r","temp"); file2.mkdir( ); 在temp目录中创建文件 File file3=new File(file2,"temp1.txt"); file3.createNewFile( ); 删除文件 file3.delete( ); 获取文件夹中的所有文件和子文件夹 String [ ] filename = file2.list( ); 显示文件名和目录名通常使用getCanonicalPath( )和getName( ), 前者返回包含规范化路径名的字符串,后者返回文件名字符串 static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) { try{ String s=stdIn.readLine( ); File t=new File(s); listSubDir(t); }catch(IOException e){ } static void listSubDir( File currentPath ){ String [ ] filenames=currentPath.list( ); try{ for(int i=0;i<filenames.length;i++){ File f=new File(currentPath.getPath( ) ,filenames[i]); if(f.isDirectory( )){ System.out.println(f.getCanonicalPath()); listSubDir(f); //递归调用实现遍历目录中的所有文件和文件夹 }else{ System.out.println(f.getName()); } } }catch(IOException e){ System.out.println("IOException"); } } }
原文:http://www.cnblogs.com/lcpholdon/p/4166048.html