JAVA IO 实例程序

//读取指定目录下的指定文件 

protected String readFile(String filePath,String fileName) throws IOException {

        StringBuffer strBuff = new StringBuffer();
        BufferedReader reader = null;      
        FileInputStream in = null;
        try {
         in = new FileInputStream(filePath + fileName);
            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                strBuff.append(line);
            }
            reader.close();
            in.close();
        } catch (FileNotFoundException e) {
         throw new IOException(e);
   
  }

        return strBuff.toString();
}

 

 

//读取指定目录下的所有文件,把每个文件的内容放置到一个outputList中
private void makeFile(String table){
  
   String filePath = "C:/migration/";
   String[] fileList = new File(filePath).list();
   List outputList = new ArrayList();
   for (int i = 0; i < fileList.length; i++){   
       String fileName = fileList[i];
       //如果当前文件是子目录

       String subFile= filePath + fileName + "/";
       if(new File(subFile).isDirectory()){
             ................;

       }
        continue;


      // 读文件写到List中  
      try {
          String identifier = fileName.substring(0, fileName.indexOf(".xml"));
          String strFile = readFile(filePath,fileName);
          outputList.add(strTmp);
      } catch (IOException e) {
          e.printStackTrace();
          continue;
      }
   } 

}

 

 

//生成的outputList里的内容写入文件

 private void makeInsertSQLFile(List outputList,String fileName){
  try {
     FileOutputStream fos = new FileOutputStream("C:/migration/" + fileName);
     OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
     BufferedWriter bw = new BufferedWriter(osw);
     for (int i = 0; i < outputList.size(); i ++) {
        bw.write((String)outputList.get(i));
       bw.newLine();
     }   
     bw.close();
     osw.close();
     fos.close();
   } catch (Exception e) {
      e.printStackTrace();
   }
  
 }

 

 

//读去CSV文件的内容,FileWriter,PrintWriter生产新的文件
private void  makeDltSQL(){
  String directory = "C:/migration/"; 
  FileWriter fw;
  PrintWriter pw;  
  try{  
     //make sql file
     File targetSQLFile = new File(directory + "/delete.sql");
     if (targetSQLFile.exists()){
       targetSQLFile.delete();
     }
     fw = new FileWriter(directory + "/delete.sql");
     //
     InputStream is = new FileInputStream(directory + "/" + "draftdelete.csv");            
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     String line;
     while(null != (line = reader.readLine())){
          String id = line.trim();
          String strTmp = "";
          strTmp = "DELETE FROM D306900.DRAFT WHERE IDENTIFIER = '" + id + "';";
          System.out.println(strTmp); 
          pw = new PrintWriter(fw);
          pw.println(strTmp);
          pw.flush();
     }
 
     is.close();   
     fw.close();      
   }catch(IOException iox){
      System.err.println(iox);
   }
 }

 

//读取工程下的文件

 private Map createModuleIDMap(){
  Map map = new HashMap();
  
  try {
   File file = new File("JavaSource/com/ibm/gcms/migration/ModuleList.txt");
   FileReader in = new FileReader(file);
   BufferedReader br = new BufferedReader(in);
   String line;
   while ((line = br.readLine()) != null) {
    String[] str = line.split(",");
    if(str.length > 1) {
     map.put(str[0], str[1]);
    } else {
     map.put(str[0], "");
    }
   }
   br.close();
   in.close();
   
  } catch(Exception e) {
   e.printStackTrace();
  }
  return map;
 }

 

=========================================================

line.split(",");

String.indexOf(".xml");//取得字符串中出现'.xml'的'.'的索引位置,也可以用来判断字符串中是否包含'.xml',不包含返回-1.

String identifier = xmlFileName.substring(0, xmlFileName.indexOf(".xml"));
String strCategory = type.substring(4);//截取前4位

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值