栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中修改复杂的CSV文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中修改复杂的CSV文件

我认为这应该对您有所帮助。基本上,您的CSV杂乱无章。下面是我安排您的csv的代码

   public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {        PrintWriter writer = new PrintWriter("file.txt", "UTF-8"); try{   //Create object of FileReader   FileReader inputFile = new FileReader("csv.txt");   //Instantiate the BufferedReader Class   BufferedReader bufferReader = new BufferedReader(inputFile);   //Variable to hold the one line data   String line;   String date="";String org ="";String student ="";   // Read file line by line and print on the console   while ((line = bufferReader.readLine()) != null)   {       if(line.contains("1.")){if(date!="" || org!=""){    writer.println(date+","+org+","+student);    student =""; }   date = line.substring(2);  }else if(line.contains("2.")){      org = line.substring(2);}else{      line = "("+line+")";    student += line+",";       }         System.out.println(line);   }   writer.println(date+","+org+","+student);   //Close the buffer reader   bufferReader.close();      }catch(Exception e){        System.out.println("Error while reading file line by line:" + e.getMessage());   }       writer.close();  }

这是您将为此获得的输出

   10/09/2016, cycling club,(3. sam, 1000, oklahoma),(   henry, 1001, california),(   bill, 1002, NY),   11/15/2016, swimming club,(3. jane, 9001, georgia),(   elizabeth, 9002, lousiana),

我正在从csv.txt中读取文件。while循环遍历文本文件的每一行。所有字段都存储在变量中。下一个日期到来时,我将它们全部写入输出文件。在while循环终止后,csv的最后一行被写入文件。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号