基于@plirke示例代码和他的帮助,我编写了一个最终的工作代码。在这里分享它,这样它可能对有类似需求的人有用。

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Random;

public class Demo1 {

public static String getRandomNumber() {

String CHARS = "1234567890";

StringBuilder random = new StringBuilder();

Random rnd = new Random();

while (random.length() < 18) // length of the random string.

{

int index = (int) (rnd.nextFloat() * CHARS.length());

random.append(CHARS.charAt(index));

}

String finaldata = random.toString();

return finaldata;

}

public static void main(String[] args) throws IOException {

File sourceCsvFile = null;

File finalCsvFile = null;

// String sourceCsvFileName = "";

sourceCsvFile = new File("C:\\MyData\\Input.csv");

finalCsvFile = new File("C:\\MyData\\Input_1.csv");

String line = "";

String cvsSplitBy = ",";

BufferedWriter writer = new BufferedWriter(new FileWriter(finalCsvFile));

try (BufferedReader br = new BufferedReader(new FileReader(sourceCsvFile))) // read the actual Source downloaded csv file

{

line = br.readLine(); // read only first line

String newFileLine = line + cvsSplitBy + "HashValue"; // append "," and new column

writer.write(newFileLine); // will be written as first line in new csv

writer.newLine(); // go to next line for writing next lines

while ((line = br.readLine()) != null) // this loop to write data for all lines except headers

{

newFileLine = line + cvsSplitBy + getRandomNumber(); // will add random numbers for each row

writer.write(newFileLine);

writer.newLine();

}

}

writer.close();

if(finalCsvFile.exists() && finalCsvFile.length() > 0)

{

System.out.println("New File with HashValue column created...");

if(sourceCsvFile.delete())

{

System.out.println("Old File deleted successfully...");

}

else

{

System.out.println("Failed to delete the Old file...");

}

}

else if (!finalCsvFile.exists())

{

System.out.println("New File with HashValue column not created...");

}

}

}

java csv 追加_如何在Java中添加一个包含CSV数据的列相关推荐

  1. java注释中添加删除线_如何在Word中添加和删除注释

    java注释中添加删除线 Leaving comments in a Word document is a good way to leave feedback on a piece you're r ...

  2. java adt怎么使用_如何在eclipse中添加android ADT ADT插件的安装图文教程

    对于程序开发的学者来说,eclipse并不陌生,它为我们提供了一个非常广阔的平台来开发程序.同样我们也可以用它来开发android程序.但是在eclipse中并不能直接开发android程序,需要我们 ...

  3. 符号在excel中的引用_如何在Excel中添加项目符号

    &符号在excel中的引用 There's no built-in feature for bullets in Excel, like there is in a Word document ...

  4. ai中如何插入签名_如何在PDF中插入一个或多个空白页?

    在编辑或修改PDF文档时,一般都需要新增空白页后再添加文本或图片等内容,那么如何在PDF中插入一个或多个空白页呢? 首先打开极速PDF编辑器,或打开需要编辑的PDF文档后,点击右上角"文档& ...

  5. webbrowser控件 有数据 但页面空白_如何在Excel中实现可以切换不同数据系列的滚珠图?...

    ▲更多精彩内容,请点击上方Excel小铲子▲ 操作系统版本 Windows 10 64位 Excel版本 Microsoft Excel 2016 64位 案例文档下载 链接:https://pan. ...

  6. 让一个图片填满一个控件_如何在Android中实现一个全景图控件(二)

    一.背景 在 如何在Android中实现一个全景图控件(一)中,介绍了项目的一些基本情况(有 demo 演示),如果项目对你有帮助,希望文章赏个赞,项目 star 一下. 项目地址:https://g ...

  7. element 搜索匹配_如何在Element-ui中实现一个远程搜索功能

    如何在Element-ui中实现一个远程搜索功能 发布时间:2021-01-29 14:50:00 来源:亿速云 阅读:87 作者:Leah 这篇文章给大家介绍如何在Element-ui中实现一个远程 ...

  8. wordpress 背景_如何在WordPress中添加全屏背景图片

    wordpress 背景 Full screen background image seems to be a new design trend that is becoming fashionabl ...

  9. wordpress图像大小_如何在WordPress中添加图像积分(逐步操作)

    wordpress图像大小 Recently, some of our readers asked us about how to properly add image credits in Word ...

最新文章

  1. 【组队学习】【30期】时间序列分析
  2. 这样建统一告警平台,运维的告警麻痹症有救了
  3. 详解结构体、类等内存字节对齐
  4. 智能音箱二战:国内Q1出货量5倍增长,BAX上屏、扩类、做家居
  5. VTK:绘图之CompareRandomGeneratorsCxx
  6. pov-inc_yourself劳自己-懒惰的设计师的POV和一些Figma
  7. coreldraw错误代码14001_CorelDRAW文件损坏的几种解决方法
  8. java nio 对消息分片_Java NIO:应用
  9. java初学者必看经典
  10. c语言最小公倍数最简单求法,c语言最小公倍数与最大公约数的求法集锦
  11. TC118AH单通道内置MOS单通道直流无刷马达驱动IC
  12. 2500个常用汉字(用来练普通话的)
  13. 全新的备份利器推荐:Duplicity使用评测
  14. 小米路由器R3原厂BootLoader和eeprom备份
  15. FTP服务器、部署YUM仓库与NFS共享服务
  16. MySQL 升级--1
  17. java哪个软件编程好学吗_java编程好学吗
  18. 第3天-Jenkins详解
  19. 火山视窗按钮,图形按钮,标签,编辑框,图片框,进度条,滑块条组件介绍
  20. 华为专家 | 轻量化微服务测试实践

热门文章

  1. java8 di_java8 多个list对象用lambda求差集操作
  2. python urllib发送post请求_python爬虫 urllib模块发起post请求过程解析
  3. 人人都能读懂的编译器原理
  4. 何传启:第六次科技革命的三大“猜想
  5. 如果再出恶性安全事件,滴滴会有人被追究刑责吗?
  6. 我的世界服务器修改数据,我的世界常用指令大全,轻松调整服务器数值状态
  7. 面向对象设计之CRC卡片
  8. 基于 Scheduled SQL 对 VPC FlowLog 实现细粒度时间窗口分析
  9. 10+知识图谱开放下载,让你的学习效率提升5倍! | “右脑”开发套餐
  10. 容器环境自建数据库、中间件一键接入阿里云 Prometheus 监控