java 文件io的常见操作

本文演示了Java中文件创建、移动、读写的基本I/O操作,包括目录创建、文件创建、移动文件及文件读写方法。
import java.io.*;

public class IODemo {
 
  //创建目录
  public static void CreateDir(String path){
     File dir = new File(path);
     if(!dir.exists()){
        dir.mkdir();           
     }
  }
  //创建新文件(isDel为true删除原来存在的文件)
  public static void CreateFile (String path, boolean isDel) throws IOException{    
     File file = new File(path);
     if(file.exists()){
        if(isDel){
            file.delete();  
            file.createNewFile();       
        }
     }else{
        file.createNewFile(); 
     }
  }
  //移动文件(isDel为true删除目标文件)
  public static void MoveTo(String oldPath, String newPath, boolean isDel) throws IOException{    
      File fold = new File(oldPath); 
      File fnew = new File(newPath);   
      if(fnew.exists()){
         if(isDel){
            fnew.delete();  
            fold.renameTo(fnew);    
         }        
      }else{
        fold.renameTo(fnew); 
      }
  }
  //写文件方法1
  public static void WriteTxtMethod1(String path, String content, boolean append) throws IOException{
       PrintWriter pw = new PrintWriter(new FileWriter(path, append));
       pw.print(content);
       pw.close();
  }
  //写文件方法2
  public static void WriteTxtMethod2(String path, String content, boolean append) throws IOException{
       BufferedWriter bw = new BufferedWriter(new FileWriter(path, append));
       bw.write(content);    
       bw.close();
  }
  //写文件方法3
  public static void WriteTxtMethod3(String path, String content, boolean append) throws IOException{
       OutputStream os = new FileOutputStream(new File(path), append);
       os.write(content.getBytes());
       os.close(); 
  }
  //读文件方法1
  public static String ReadTxtMethod1(String path) throws IOException{
       BufferedReader br = new BufferedReader(new FileReader(new File(path)));            
       StringBuffer bd = new StringBuffer();
       while (true) {
          String str = br.readLine();
          if (str == null) break;                
          bd.append(str + "\n");
       } 
       br.close();
       return bd.toString();
  }
  //读文件方法2
  public static String ReadTxtMethod2(String path) throws IOException{
       File file = new File(path);
       InputStream is = new FileInputStream(file);
       byte[] b = new byte[Integer.parseInt(file.length()+ "")];
       is.read(b);
       is.close();
       String str = new String(b);   
       return str;
  }
  //读文件方法3
  public static String ReadTxtMethod3(String path) throws IOException{
       File file = new File(path);
       Reader r = new FileReader(file);
       char[] c = new char[(int) file.length()];
       r.read(c);             
       r.close();
       String str = new String(c); 
       return str; 
  }

  public static void main(String arg[]) {
    try{

      String dirPath = "E:/Lc/java/test"; //目录路径
      String filePath = "E:/Lc/java/test.txt"; //原文件路径
      String nfilePath = "E:/Lc/java/test/test.txt"; //新文件路径

      //创建目录
      CreateDir(dirPath);

      //创建文件
      CreateFile(filePath, true);

      //移动文件
      MoveTo(filePath, nfilePath, true);

      //获取指定目录下的目录和文件
      File[] files = new File(dirPath).listFiles();
      for(int i=0;i<files.length;i++){
         if(files[i].isDirectory()){
           System.out.println("目录:" + files[i].getName() + " 路径:" + files[i].getAbsolutePath());
         }else{
           System.out.println("文件:" + files[i].getName() + " 路径:" + files[i].getAbsolutePath());
         }
      }

      //写文件
      WriteTxtMethod1(nfilePath, "测试写文件test1\r\n", true);
      WriteTxtMethod2(nfilePath, "测试写文件test2\r\n", true);
      WriteTxtMethod3(nfilePath, "测试写文件test3\r\n", true);
    
      //读文件
      String s1 = ReadTxtMethod1(nfilePath);
      System.out.println(s1);
      String s2 = ReadTxtMethod2(nfilePath);
      System.out.println(s2);
      String s3 = ReadTxtMethod3(nfilePath);
      System.out.println(s3);

     }catch(Exception e){ 
        System.out.println(e.getMessage());
     }
  }
}




/*
E:\Lc\java>javac IODemo.java

E:\Lc\java>java IODemo
文件:test.txt 路径:E:\Lc\java\test\test.txt
测试写文件test1测试写文件test2测试写文件test3
测试写文件test1
测试写文件test2
测试写文件test3

测试写文件test1
测试写文件test2
测试写文件test3
*/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值