日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2024/9/27 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java csv 追加_如何在Java中添加一个包含CSV数据的列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基于@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数据的列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。