JAVA文件和管道流处理

    技术2026-04-21  8

    文件

    java.io.File File:既可以表示文件也可以表示目录

    通过File我们可以访问文件和目录以及子项的相关信息,也可以创建和删除文件和目录 通过File不能读取文件的数据

    1.文件的相关属性获取

    文件名:getName

    文件的长度:length

    是否可读可写:canRead/canWrite

    是否隐藏:isHidden

    是否是文件:isFile

    是否是目录:isDirectory

    import java.io.File; public class demo01 { public static void main(String[] args) { // TODO Auto-generated method stub File file=new File("demo1"); System.out.println("name:"+file.getName()); System.out.println("length:"+file.length()); System.out.println("可读:"+file.canRead()); System.out.println("可写:"+file.canWrite()); System.out.println("隐藏:"+file.isHidden()); System.out.println("是文件:"+file.isFile()); System.out.println("是目录"+file.isDirectory()); } }

    文件的创建和删除

    exists:判断文件是否存在 createNewFile:创建文件 delete:删除文件

    import java.io.File; import java.io.IOException; public class demo02 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file =new File("demo02.txt"); if(!file.exists()) { file.createNewFile(); System.out.println("创建文件成功"); }else { file.delete(); System.out.println("删除文件成功"); } } }

    一级目录的创建和删除

    mkdir():创建目录

    import java.io.File; public class demo03 { public static void main(String[] args) { // TODO Auto-generated method stub File file=new File("aa"); if(!file.exists()) { file.mkdir(); System.out.println("创建文件夹成功"); }else { file.delete(); System.out.println("删除文件夹成功"); } } }

    多级目录的创建和删除

    mkdirs():创建多级目录 分隔:File.separator 多级目录的删除:递归 1.递归:方法调用自身的过程

    2.listFiles:获取子项

    import java.io.File; public class demo05 { public static void main(String[] args) { // TODO Auto-generated method stub File file=new File("ab"); del(file); System.out.println("删除多级目录成功"); } //删除多级目录 public static void del(File file) { if(file.isDirectory()) { File[] files=file.listFiles(); for(File file2:files){ del(file2); } } file.delete(); } }

    通过过滤器获取部分子项

    FileFilter:文件过滤器

    import java.io.File; import java.io.FileFilter; public class demo06 { public static void main(String[] args) { // TODO Auto-generated method stub File file=new File("."); File[]subs=file.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { // TODO Auto-generated method stub System.out.println(file.getName()); return file.getName().startsWith("."); } }); for(File file2:subs) { System.out.println(file2); } } }

    IO

    RandomAccessFile

    基于指针随机访问文件,总是在指针位置读写文件,读取完成过后,指针自动后移

    两种模式:要求我们在创建的时候指定 读写:rw 只读:r

    两种方法:write() read() 1.getFilePointer:获取当前指针的位置 2.seek():设置指针的位置 3.读写一组字节

    import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; public class demo01 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub RandomAccessFile raf=new RandomAccessFile("raf.txt","rw"); System.out.println(raf.getFilePointer()); raf.write('a'); System.out.println(raf.getFilePointer()); raf.write(1); System.out.println(raf.getFilePointer()); raf.seek(0); System.out.println(raf.getFilePointer()); // raf.write(1); System.out.println("写出成功"); raf.close(); } } ---------------------------- 0 1 2 0 写出成功 --------------------

    标准的IO操作

    1.区别RAF: *RAF只能读写文件,IO可以读取网络 *RAF可以进行读写,IO流是单向的 2.流的分类

    流向:输入流(外界到程序) 输出流(程序到外界)

    流是否与特定的某个节点相连: 节点流(低级流) 处理流(高级流)

    低级流:读写操作必须要有低级流,数据源明确

    高级流:高级流不能独立存在,用来处理其他流的 3.字节输出输出流 输出流:OutputStream 是所有的字节输出流的顶级父类 定义了写的方法write 输入流:InputStream 是所有字节输入流的顶级父类 定义了读的方法 read 文件字节输出输入流 文件字节输出流:FileOutputStream 文件字节输入流:FileInputStream

    高效的流处理方法

    import java.io.IOException; import java.io.RandomAccessFile; public class demo05_2 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub RandomAccessFile src=new RandomAccessFile("read.txt","rw"); RandomAccessFile desc=new RandomAccessFile("copy.txt","rw"); byte[]data=new byte[1024]; int len=-1; while((len=src.read(data))!=-1) { desc.write(data,0,len); } System.out.println("复制成功"); desc.close(); } } import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class demo03 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub FileOutputStream fos=new FileOutputStream("copy1.txt"); FileInputStream fis=new FileInputStream("read1.txt"); byte[]data=new byte[1024]; int len=-1; while((len=fis.read(data))!=-1) { fos.write(data,0,len); } System.out.println("success"); fis.close(); fos.close(); } }
    Processed: 0.010, SQL: 9