JAVA IO 实例程序

来源:互联网 发布:淘宝网站发展历程 编辑:程序博客网 时间:2024/06/07 19:13

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

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位