java IO文件操作

一些范例:

0. 目录File

遍历目录下的文件

import java.io.File;  
import java.io.IOException;  
  
public class DirErgodic {  
  
    private static int depth=1;  
      
    public static void find(String pathName,int depth) throws IOException{  
        int filecount=0;  
        //获取pathName的File对象  
        File dirFile = new File(pathName);  
        //判断该文件或目录是否存在,不存在时在控制台输出提醒  
        if (!dirFile.exists()) {  
            System.out.println("do not exit");  
            return ;  
        }  
        //判断如果不是一个目录,就判断是不是一个文件,时文件则输出文件路径  
        if (!dirFile.isDirectory()) {  
            if (dirFile.isFile()) {  
                System.out.println(dirFile.getCanonicalFile());  
            }  
            return ;  
        }  
          
        for (int j = 0; j < depth; j++) {  
            System.out.print("  ");  
        }  
        System.out.print("|--");  
        System.out.println(dirFile.getName());  
        //获取此目录下的所有文件名与目录名  
        String[] fileList = dirFile.list();  
        int currentDepth=depth+1;  
        for (int i = 0; i < fileList.length; i++) {  
            //遍历文件目录  
            String string = fileList[i];  
            //File("documentName","fileName")是File的另一个构造器  
            File file = new File(dirFile.getPath(),string);  
            String name = file.getName();  
            //如果是一个目录,搜索深度depth++,输出目录名后,进行递归  
            if (file.isDirectory()) {  
                //递归  
                find(file.getCanonicalPath(),currentDepth);  
            }else{  
                //如果是文件,则直接输出文件名  
                for (int j = 0; j < currentDepth; j++) {  
                    System.out.print("   ");  
                }  
                System.out.print("|--");  
                System.out.println(name);  
                  
            }  
        }  
    }  
      
    public static void main(String[] args) throws IOException{  
        find("D:\\MongoDB", depth);  
    }  
}  

1.读写

try{  
    FileInputStream in=new FileInputStream("count.txt");  
    DataInputStream dataIn=new DataInputStream(in);  
    number=dataIn.readInt();  
    number++;  
    in.close();  
    dataIn.close();  
}  
catch(FileNotFoundException e){   
[html] view plain copy
    number++;  
[html] view plain copy
    try {FileOutputStream out=new  
    FileOutputStream("count.txt");  
    DataOutputStream dataOut=new  
    DataOutputStream(out);  
    dataOut.writeInt(number);  
    out.close();dataOut.close();  
}  
catch(IOException ee){}  
}  
catch(IOException ee)  
{  
}  
}  
else{  
    number++;  
try{  
    FileOutputStream out=new FileOutputStream("count.txt");  
    DataOutputStream dataOut=new DataOutputStream(out);  
    dataOut.writeInt(number);  
    out.close();dataOut.close();  
}  
    catch(FileNotFoundException e){}  
    catch(IOException e){}  
 }  
}  


2 按行读

 try{
File f=new File("D:/test","A.txt");
FileReader in=new FileReader(f);
BufferedReader buffer=new BufferedReader(in);
String s=null;
while((s=buffer.readLine())!=null)
{ out.print(s+"<BR>");
}
in.close();buffer.close();
}
catch(IOException ee)
{out.print(" 文件不存在");}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值