Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* The MIT License
*
* Copyright (C) 2007 Asterios Raptis
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package de.alpharogroup.file.csv;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import lombok.experimental.ExtensionMethod;
import de.alpharogroup.file.write.WriteFileExtensions;
import de.alpharogroup.io.StreamExtensions;
import de.alpharogroup.string.StringExtensions;
/**
* Utility class for the use of cvs-files.
*
* @version 1.0
* @author Asterios Raptis
*/
@ExtensionMethod(StringExtensions.class)
public final class CsvFileExtensions
{
/**
* Reads every line from the File splits the data through a comma and puts them to the List.
*
* @param input
* The File from where the input comes.
* @param encoding
* The encoding from the file.
* @return The List with all lines from the file.
*/
public static List formatKommaSeperatedFileToList(final File input,
final String encoding)
{
// The List where the data from every line from the File to put.
final List output = new ArrayList<>();
BufferedReader reader = null;
try
{
// create the bufferedreader
reader = (BufferedReader)StreamExtensions.getReader(input, encoding, false);
// the line.
String line = null;
// read all lines from the file
do
{
line = reader.readLine();
// if null break the loop
if (line == null)
{
break;
}
// Split the line
final String[] splittedData = line.split(",");
// Iterate throuh the array
for (final String element : splittedData)
{
// add the line to the list
output.add(element.trim());
}
}
while (true);
}
catch (final UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (final FileNotFoundException e)
{
e.printStackTrace();
}
catch (final IOException e)
{
e.printStackTrace();
}
finally
{
StreamExtensions.closeReader(reader);
}
// return the list with all lines from the file.
return output;
}
/**
* Formats the List that contains String-object to a csv-file wich is plus-minus 100 characters
* in every line.
*
* @param list
* The List with the Strings.
* @return The String produced from the List.
*/
private static String formatListToString(final List list)
{
int lineLength = 0;
final StringBuffer sb = new StringBuffer();
for (final String str : list)
{
final int length = str.length();
lineLength = length + lineLength;
sb.append(str);
sb.append(", ");
if (100 < lineLength)
{
sb.append("\n");
lineLength = 0;
}
}
return sb.toString().trim();
}
/**
* Formats a file that has in every line one input-data into a csv-file.
*
* @param input
* The input-file to format. The current format from the file is every data in a
* line.
* @param output
* The file where the formatted data should be inserted.
* @param encoding
* the encoding
* @throws IOException
* When an io error occurs.
*/
public static void formatToCSV(final File input, final File output, final String encoding)
throws IOException
{
final List list = readLinesInList(input, "UTF-8");
final String sb = formatListToString(list);
WriteFileExtensions.writeStringToFile(output, sb, encoding);
}
/**
* Gets the given cvs file as list of maps. Every map has as key the header from the column and
* the corresponding value for this line.
*
* @param input
* the input
* @return the cvs as list map
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static List