【问题标题】:Java :Exporting output into files and storing in a Auto generated FolderJava:将输出导出到文件并存储在自动生成的文件夹中
【发布时间】:2014-09-05 12:05:26
【问题描述】:

我有一个 CSV 格式的输出:PSD_CSV,我希望将此生成的输出导出到一个用分号而不是逗号分隔的文件中,我还希望为每个特定研究生成一个名为“Study_UID”的文件夹,其中包括文件 PSD_CSV

我想在java中获得这个结果,任何提示都非常感谢。

【问题讨论】:

    标签: java csv directory export-to-csv


    【解决方案1】:

    这样的事情应该可以完成这项工作

        boolean success = (new File("your_new_dir")).mkdirs());
        if (!success) {
         //TODO handle me
        }
    
        RandomAccessFile raf = new RandomAccessFile("yourfile", "ro");
        RandomAccessFile raf_out = new RandomAccessFile("your_new_dir/yourfile", "rw");
        String line;
        while ((line = raf.readLine()) != null) {
            line.replaceAll(",", ";");
            raf_out.writeChars(line);;
        }
        raf.close();
        raf_out.close();
    

    【讨论】:

      【解决方案2】:

      您可以使用文件处理读取 csv 文件并将 , 替换为 ; 然后将其写入新文件

      String fileName = "PSD_CSV.csv"; 
      File   file     = new File("NEW_PSD_CSV.txt");  
      PrintWriter output = new PrintWriter(file);  
      try {    
          BufferedReader br = new BufferedReader( new FileReader(fileName));    
          StringTokenizer st = null;    
          while( (st = br.readLine()) != null) {    
              output.println(st.replace(",",";"));  
              output.println();  
          }    
      } catch (Exception e) {    
                  e.printStackTrace();    
      } finally {
         output.close();   
      }
      

      【讨论】:

      • readLine() 然后StringTokenizer\n?这样做的目的是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2015-08-24
      • 2013-12-31
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多