The code snippet splits a CSV file into multiple CSV files and writes the first column content to the child CSV files. What I observed with this code is the column header "UNIQUE ID" is only appearing in FIRST CSV file. The following CSV files only contains data without the header. In order to get header to all files I thought of using an ArrayList so that I can put the header at the first index of ArrayList and rest of data afterwards. But I failed miserably.
I require suggestion or help for how to modify the code so that all the child files should have an additional UNIQUE IDENTIFIER row along as the first row with the column data. I am pasting the code which I tried and didn't work. Child csv should look like this This is what I am getting
public static void myFunction(int lines, int files) throws FileNotFoundException, IOException {
String inputfile = "C:/Users/Downloads/CONSOLIDATED.csv";
BufferedReader br = new BufferedReader(new FileReader(inputfile));
String strLine = null;
for (int i = 1; i <= files; i++) {
FileWriter fstream1 = new FileWriter("C:/Users/Downloads/FileNumber_" + i + ".csv");
BufferedWriter out = new BufferedWriter(fstream1);
for (int j = 0; j < lines; j++) {
strLine = br.readLine();
if (strLine != null) {
String strar[] = strLine.split(",");
ArrayList<String> al=new ArrayList<String>();
al.add(0,"Unique Identifier");
al.add(1,strar[0]);
char c[] = al.toString().toCharArray();
out.write(c);
out.newLine();
}
}
out.close();
}
br.close();
}
Your problem is that you are not keeping the headers out of the loops. Try something reading the first line before the main loop and store the headers in the List. then, every time you create a new file, before starting the inner loop, write the header in the first line of each file.
public static void myFunction(int lines, int files) throws FileNotFoundException, IOException {
String inputfile = "C:/Users/Downloads/CONSOLIDATED.csv";
BufferedReader br = new BufferedReader(new FileReader(inputfile));
String strLine = br.readLine(); //here you have the headers
String[] headers=strLine.split(",");
for (int i = 1; i <= files; i++) {
FileWriter fstream1 = new FileWriter("C:/Users/Downloads/FileNumber_" + i + ".csv");
BufferedWriter out = new BufferedWriter(fstream1);
out.write(headers[0]);
for (int j = 0; j < lines; j++) {
out.newLine();
strLine = br.readLine();
if (strLine != null) {
String strar[] = strLine.split(",");
out.write(strar[0]);
}
}
out.close();
}
br.close();
}
Related
I have a csv file which contains some data
e.g:
Name, Age
John, 25
Joe, 26
I want to add a new column after the age. I tried a lot and here's my code
public static void main(String args[]) throws IOException {
BufferedWriter br = new BufferedWriter(new FileWriter("D:\\text1.csv",true));
StringBuilder sb = new StringBuilder();
String id[] = {"3","4"};
for (int i = 0; i < id.length; i++) {
sb.append(id[i] + "\t");
sb.append("\n");
}
br.write(sb.toString());
br.close();
}
The values of String[id] is getting append but below 1st column. Also is there any way to add a header ?
I need to have the data from a csv file into excel in selenium.
Having csv file in format like:
PERIOD|EMPLID|EMPL_RCD|HOME HOST|NAME|FIRST_NAME|LAST_NAME|FTE|EMPL_STATUS
5/04/2018|78787|0|Home|mandon|steven|jabobs|1|A
6/04/2018|78789|0|Home|stacy|carvin|tans|1|A
11/04/2018|17892|0|Home|neel|harvis|bammer|1|A
Need to have this data in excel like shown in image:
EDIT My attempt at creating an Excel file
I am using the below code for generating the (.xls) file from csv file with pipe symbol delimiter as shown in the image
but is is giving java.lang.NullPointerException after reading first line.
public class DelimitedToXls {
#SuppressWarnings("deprecation")
public static void main(String args[]) throws IOException {
ArrayList<ArrayList<String>> allRowAndColData = null;
ArrayList<String> oneRowData = null;
String fName = "C:\\input.csv";
String currentLine;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i = 0;
allRowAndColData = new ArrayList<ArrayList<String>>();
while ((currentLine = myInput.readLine()) != null) {
oneRowData = new ArrayList<String>();
String oneRowArray[] = currentLine.split(";");
for (int j = 0; j < oneRowArray.length; j++) {
oneRowData.add(oneRowArray[j]);
}
allRowAndColData.add(oneRowData);
System.out.println();
i++;
}
try {
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet("sheet1");
for (int i = 0; i < allRowAndColData.size(); i++) {
ArrayList<?> ardata = (ArrayList<?>) allRowAndColData.get(i);
HSSFRow row = sheet.createRow((short) 0 + i);
for (int k = 0; k < ardata.size(); k++) {
System.out.print(ardata.get(k));
HSSFCell cell = row.createCell((short) k);
cell.setCellValue(ardata.get(k).toString());
}
System.out.println();
}
FileOutputStream fileOutputStream = new FileOutputStream("C:\\outputFile.xls");
workBook.write(fileOutputStream);
fileOutputStream.close();
} catch (Exception ex) {
}
}
}
You can directly open the file with excel. Open file->Select file type: text file.
Select the file, then 'delimited' option on next window. Next window select 'other' and type | as delimiter.
Of course, save it as xls.
That's all.
You have 3 main options:
Open it directly with Excel and setting the delimiter as | (pipe)
Rewrite it as a valid CSV (Comma-Separated Values) file (ie, replace the pipes by commas)
Write the content of the file into a proper Excel file.
Option 1 - Open it directly with Excel
See Fabrizio's answer.
Option 2 - Rewrite it as a valid CSV file
If you are sure you have no commas in your file
You just need to replace all occurrences of | by , to have a valid csv (Comma-Separated Values) file. Then you can open it with Excel.
String fileName = "/path/to/your/file/textFile.txt";
String csvFileName = "/path/to/your/file/csvFile.csv";
try (BufferedReader br = new BufferedReader(new FileReader(fileName));
Writer writer = new FileWriter(csvFileName)) {
String line;
while ((line = br.readLine()) != null) {
writer.append(line.replaceAll("[|]", ","));
writer.append("\n");
}
} catch(Exception e) {
e.printStackTrace();
}
This code changes the content of your file to
PERIOD,EMPLID,EMPL_RCD,HOME HOST,NAME,FIRST_NAME,LAST_NAME,FTE,EMPL_STATUS
5/04/2018,78787,0,Home,mandon,steven,jabobs,1,A
6/04/2018,78789,0,Home,stacy,carvin,tans,1,A
11/04/2018,17892,0,Home,neel,harvis,bammer,1,A
If you might have commas in your file
You need to read token by token, and surround tokens which contain a comma by double quotes. Then replace all pipes by commas. Example, this line
5/04/2018|78787|0|Home, Work|mandon|steven|jabobs|1|A
Would be transformed to
5/04/2018,78787,0,"Home, Work",mandon,steven,jabobs,1,A
You can do it this way:
String fileName = "/path/to/your/file/textFile.txt";
String csvFileName = "/path/to/your/file/csvFile.csv";
try (BufferedReader br = new BufferedReader(new FileReader(fileName));
Writer writer = new FileWriter(csvFileName)) {
String line;
while ((line = br.readLine()) != null) {
String csvLine = Arrays.stream(line.split("[|]")) // split on pipes
.map(token -> token.contains(",") ? "\""+token+"\"" : token) // surround with double quotes if there is a comma in the value
.collect(Collectors.joining(",", "", "\n")); // join with commas
writer.append(csvLine);
}
} catch(Exception e) {
e.printStackTrace();
}
Option 3 - Writing to an Excel file
You can also create a proper Excel file .xls or .xlsx using the Apache POI library. Here is an example using POI-OOXML 3.17 (latest version as of today) You can get it from Maven Repository
String fileName = "/path/to/your/file/textFile.txt";
String excelFileName = "/path/to/your/file/excelFile.xlsx";
// Create a Workbook and a sheet in it
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");
// Read your input file and make cells into the workbook
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
Row row;
Cell cell;
int rowIndex = 0;
while ((line = br.readLine()) != null) {
row = sheet.createRow(rowIndex);
String[] tokens = line.split("[|]");
for(int iToken = 0; iToken < tokens.length; iToken++) {
cell = row.createCell(iToken);
cell.setCellValue(tokens[iToken]);
}
rowIndex++;
}
} catch(Exception e) {
e.printStackTrace();
}
// Write your xlsx file
try (FileOutputStream outputStream = new FileOutputStream(excelFileName)) {
workbook.write(outputStream);
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
You can replace | with char \t and store that in .csv file.
I have done something similar while converting CSV to XLS:
try{
FileReader fr=new FileReader("TEJAS.CSV");
FileWriter fw=new FileWriter("TEJASEXEL.xls");
while((c=fr.read())!=-1){
if(c==','){
c='\t';
}
fw.write(c);
}
fr.close();
fw.close();
}
I am trying to read a given text file filled with a bunch of doubles, the text file looks something like this(no spaces between each line):
0, 0.007133248, 0.003747135, 0.0034464097, 0.009517824, 0.0036065334,
0.007921185, 0.0041013476
1, 0.006223865, 5.804103E-4, 5.6967576E-4, 0.008850083, 0.003269921,
3.7322243E-4, 0.0011008179
2, 0.0051101227, 0.008973722, 0.0013274436, 0.00922496, 0.0050817304,
0.004631279, 0.0069321943
essentially 1000 rows with 8 columns, and am trying to turn this into a 2d array of data[1000][8], am having trouble iterating through the data though. Heres what I have so far, any help would be appreciated!
public static void readFile2() throws IOException{
Double[][] data = new Double[1000][8];
int row = 0;
int col = 0;
Scanner scanner = null;
scanner = new Scanner(new BufferedReader(new FileReader("/Users/Roman/Documents/workspace/cisc124/Logger (1).csv")));
while (scanner.hasNext() && scanner !=null) {
scanner.useDelimiter(",");
while(row<data.length && col<8){
data[row][col] = Double.parseDouble(scanner.next());
col++;
//System.out.print(Arrays.deepToString(data));
}
col=0;
row++;
}
System.out.println(Arrays.deepToString(data));
scanner.close();
}
It would be more convenient reading thefile line by line..
String[] data = new String[1000];
int row = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
String line;
while ((line = br.readLine()) != null) {
data[row]= line;
row++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
and at the end do the same
System.out.println(Arrays.ToString(data));
or if you need the element at a given index then split a row and use Double.parse();
I am creating a function to print a mase, which is stored in a 2 Dimensional ArrayList<ArrayList<String>>. However, the function instead prints out a large portion of blank space. I am attempting to first iterate through each ArrayList<String> and then iterate through each String element inside the nested ArrayList<String>s. I am creating the ArrayList<ArrayList<String>> from reading a file.
Here is the file:
XXXXXXXXSOOOOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXOOOOOXXXXXX
XXXXXXXOXXXXXXXXXXX
XXXXXXXEXXXXXXXXXXX
Here is where I create it:
String fileName = "maze.txt";
String line = null;
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
// Setup FileReader, BufferedReader, and LineReader
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
// Get all the lines inside the maze file
while ((line = bufferedReader.readLine()) != null)
{
ArrayList<String> lineList = new ArrayList<String>();
for (int i = 0; i < line.length(); i++)
{
lineList.add(line.substring(i, i));
}
lines.add(lineList);
}
Here is my function:
public static void printMase(ArrayList<ArrayList<String>> lines)
{
for (ArrayList<String> row: lines)
{
for (String elem: row)
{
System.out.println(elem);
}
}
}
There are just some minor mistakes in your code. Make the following changes.
// This is adding a blank space
lineList.add(line.substring(i, i));
To
// This will add a single character
lineList.add(line.substring(i, i+1));
and modify printMase(ArrayList<ArrayList<String>> lines)
public static void printMase(ArrayList<ArrayList<String>> lines)
{
for (ArrayList<String> row: lines)
{
for (String elem: row)
{
// Using print so each character is on the same line
System.out.print(elem);
}
// Now use println to end the line
System.out.println();
}
}
Full code looks like:
public static void main(String[] args) throws Exception {
String fileName = "maze.txt";
String line = null;
ArrayList<ArrayList<String>> lines = new ArrayList<>();
// Setup FileReader, BufferedReader, and LineReader
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
// Get all the lines inside the maze file
while ((line = bufferedReader.readLine()) != null) {
ArrayList<String> lineList = new ArrayList<>();
for (int i = 0; i < line.length(); i++) {
// Adds a single character
lineList.add(line.substring(i, i+1));
}
lines.add(lineList);
}
printMase(lines);
}
public static void printMase(ArrayList<ArrayList<String>> lines)
{
for (ArrayList<String> row: lines)
{
for (String elem: row)
{
// Using print so each character is on the same line
System.out.print(elem);
}
// Now use println to end the line
System.out.println();
}
}
Results:
XXXXXXXXSOOOOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXOOOOOXXXXXXX
XXXXXXXOXXXXXXXXXXX
XXXXXXXEXXXXXXXXXXX
Woops, I realized that I was adding blank space during initialization. I updated my code such that it was adding the actual characters:
String fileName = "maze.txt";
String line = null;
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
// Setup FileReader, BufferedReader, and LineReader
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null)
{
ArrayList<String> lineList = new ArrayList<String>();
for (int i = 0; i < line.length(); i++)
{
lineList.add(Character.toString(line.charAt(i)));
}
lines.add(lineList);
}
I need to get the name of the file which actually begins with something like 45-something.log. I could grab 45, but not the rest since something consists of random numbers. Moreover, such this file is already on server and I need to look for it beforehand.
I already tried something like following:
<%
String line ="";
String file = "/tmp/smsrouter/" + pageContext.getAttribute("cid");
BufferedReader br = new BufferedReader(new FileReader(file));
int count = 0;
int lineNumber = 0;
while((line=br.readLine())!=null)
{
String[] parts = line.split("\\,");
lineNumber++;
if(parts[3].equals("0") && count < lineNumber)
{
count++;
}
}
count = (count/lineNumber)*100;
br.close();
%>
obviously, it won't get any result as expected. What should I do then?
Why not iterator /tmp/smsrouter/ dir and match file name you want?
File[] files = new File("/tmp/smsrouter/").listFiles();
for (File file : files) {
if (file.isDirectory()||!file.getName().startsWith("45-")) {
continue;
} else {
BufferedReader br = new BufferedReader(new FileReader(file));
int count = 0;
int lineNumber = 0;
while((line=br.readLine())!=null)
{
String[] parts = line.split("\\,");
lineNumber++;
if(parts[3].equals("0") && count < lineNumber)
{
count++;
}
}
count = (count/lineNumber)*100;
br.close();
}
}
probably you want extract the count logic to another method. Hope this help.
This is not suggested in real production system , as the invoking maybe very long that request timeout maybe raised.