文件输入输出的例子

来源:互联网 发布:淘宝诈骗卖家如何应对 编辑:程序博客网 时间:2024/05/23 00:01

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

public class FileProcess {

   /**
    * @param args
    */
   public static void main(String[] args) {
      FileReader fr = null;
      BufferedReader br = null;
      PrintStream ps = null;
      String[] fileNames = null;
      try {

         File deployFile = new File("file/dml&pkg/deploy.txt");
         // 创建输入流
         if (deployFile.exists()) {
            fr = new FileReader(deployFile);
            // 创建缓冲流,方便整行读取数据
            br = new BufferedReader(fr);
            String buffer = null;
            StringBuffer sb = new StringBuffer();
            while ((buffer = br.readLine()) != null) {
               sb.append(buffer).append("/");
            }
            fileNames = sb.toString().split("/");
         }else{
            File deployDir = new File("file/dml&pkg");
            fileNames = deployDir.list();
         }

         // 创建打印流,方便整行输出数据,可以自动添加回车哦
         ps = new PrintStream("file/deploy.sql");
         String projectPath = System.getProperty("user.dir");
         String filePath = "@" + projectPath + "\\file\\";
         for (int i = 0; i < fileNames.length; i++) {
            ps.println(filePath + fileNames[i]);
         }
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }finally{
         try {
            if(br != null)br.close();
            ps.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
        
      }
   }
}

 

原创粉丝点击