Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
104
Task/CSV-data-manipulation/Java/csv-data-manipulation-1.java
Normal file
104
Task/CSV-data-manipulation/Java/csv-data-manipulation-1.java
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import java.io.*;
|
||||
import java.awt.Point;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class CSV {
|
||||
|
||||
private HashMap<Point, String> _map = new HashMap<Point, String>();
|
||||
private int _cols;
|
||||
private int _rows;
|
||||
|
||||
public void open(File file) throws FileNotFoundException, IOException {
|
||||
open(file, ',');
|
||||
}
|
||||
|
||||
public void open(File file, char delimiter)
|
||||
throws FileNotFoundException, IOException {
|
||||
Scanner scanner = new Scanner(file);
|
||||
scanner.useDelimiter(Character.toString(delimiter));
|
||||
|
||||
clear();
|
||||
|
||||
while(scanner.hasNextLine()) {
|
||||
String[] values = scanner.nextLine().split(Character.toString(delimiter));
|
||||
|
||||
int col = 0;
|
||||
for ( String value: values ) {
|
||||
_map.put(new Point(col, _rows), value);
|
||||
_cols = Math.max(_cols, ++col);
|
||||
}
|
||||
_rows++;
|
||||
}
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
public void save(File file) throws IOException {
|
||||
save(file, ',');
|
||||
}
|
||||
|
||||
public void save(File file, char delimiter) throws IOException {
|
||||
FileWriter fw = new FileWriter(file);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
|
||||
for (int row = 0; row < _rows; row++) {
|
||||
for (int col = 0; col < _cols; col++) {
|
||||
Point key = new Point(col, row);
|
||||
if (_map.containsKey(key)) {
|
||||
bw.write(_map.get(key));
|
||||
}
|
||||
|
||||
if ((col + 1) < _cols) {
|
||||
bw.write(delimiter);
|
||||
}
|
||||
}
|
||||
bw.newLine();
|
||||
}
|
||||
bw.flush();
|
||||
bw.close();
|
||||
}
|
||||
|
||||
public String get(int col, int row) {
|
||||
String val = "";
|
||||
Point key = new Point(col, row);
|
||||
if (_map.containsKey(key)) {
|
||||
val = _map.get(key);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
public void put(int col, int row, String value) {
|
||||
_map.put(new Point(col, row), value);
|
||||
_cols = Math.max(_cols, col+1);
|
||||
_rows = Math.max(_rows, row+1);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
_map.clear();
|
||||
_cols = 0;
|
||||
_rows = 0;
|
||||
}
|
||||
|
||||
public int rows() {
|
||||
return _rows;
|
||||
}
|
||||
|
||||
public int cols() {
|
||||
return _cols;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
CSV csv = new CSV();
|
||||
|
||||
csv.open(new File("test_in.csv"));
|
||||
csv.put(0, 0, "Column0");
|
||||
csv.put(1, 1, "100");
|
||||
csv.put(2, 2, "200");
|
||||
csv.put(3, 3, "300");
|
||||
csv.put(4, 4, "400");
|
||||
csv.save(new File("test_out.csv"));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
125
Task/CSV-data-manipulation/Java/csv-data-manipulation-2.java
Normal file
125
Task/CSV-data-manipulation/Java/csv-data-manipulation-2.java
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.apache.commons.csv.*;
|
||||
|
||||
public class RCsv {
|
||||
private static final String NL = System.getProperty("line.separator");
|
||||
private static final String FILENAME_IR = "data/csvtest_in.csv";
|
||||
private static final String FILENAME_OR = "data/csvtest_sum.csv";
|
||||
private static final String COL_NAME_SUM = "SUM, \"integers\""; // demonstrate white space, comma & quote handling
|
||||
|
||||
public static void main(String[] args) {
|
||||
Reader iCvs = null;
|
||||
Writer oCvs = null;
|
||||
System.out.println(textFileContentsToString(FILENAME_IR));
|
||||
try {
|
||||
iCvs = new BufferedReader(new FileReader(FILENAME_IR));
|
||||
oCvs = new BufferedWriter(new FileWriter(FILENAME_OR));
|
||||
processCsv(iCvs, oCvs);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (iCvs != null) { iCvs.close(); }
|
||||
if (oCvs != null) { oCvs.close(); }
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println(textFileContentsToString(FILENAME_OR));
|
||||
return;
|
||||
}
|
||||
|
||||
public static void processCsv(Reader iCvs, Writer oCvs) throws IOException {
|
||||
CSVPrinter printer = null;
|
||||
try {
|
||||
printer = new CSVPrinter(oCvs, CSVFormat.DEFAULT.withRecordSeparator(NL));
|
||||
List<String> oCvsHeaders;
|
||||
List<String> oCvsRecord;
|
||||
CSVParser records = CSVFormat.DEFAULT.withHeader().parse(iCvs);
|
||||
Map<String, Integer> irHeader = records.getHeaderMap();
|
||||
oCvsHeaders = new ArrayList<String>(Arrays.asList((irHeader.keySet()).toArray(new String[0])));
|
||||
oCvsHeaders.add(COL_NAME_SUM);
|
||||
printer.printRecord(oCvsHeaders);
|
||||
for (CSVRecord record : records) {
|
||||
oCvsRecord = record2list(record, oCvsHeaders);
|
||||
printer.printRecord(oCvsRecord);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (printer != null) {
|
||||
printer.close();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private static List<String> record2list(CSVRecord record, List<String> oCvsHeaders) {
|
||||
List<String> cvsRecord;
|
||||
Map<String, String> rMap = record.toMap();
|
||||
long recNo = record.getRecordNumber();
|
||||
rMap = alterRecord(rMap, recNo);
|
||||
int sum = 0;
|
||||
sum = summation(rMap);
|
||||
rMap.put(COL_NAME_SUM, String.valueOf(sum));
|
||||
cvsRecord = new ArrayList<String>();
|
||||
for (String key : oCvsHeaders) {
|
||||
cvsRecord.add(rMap.get(key));
|
||||
}
|
||||
return cvsRecord;
|
||||
}
|
||||
|
||||
private static Map<String, String> alterRecord(Map<String, String> rMap, long recNo) {
|
||||
int rv;
|
||||
Random rg = new Random(recNo);
|
||||
rv = rg.nextInt(50);
|
||||
String[] ks = rMap.keySet().toArray(new String[0]);
|
||||
int ix = rg.nextInt(ks.length);
|
||||
long yv = 0;
|
||||
String ky = ks[ix];
|
||||
String xv = rMap.get(ky);
|
||||
if (xv != null && xv.length() > 0) {
|
||||
yv = Long.valueOf(xv) + rv;
|
||||
rMap.put(ks[ix], String.valueOf(yv));
|
||||
}
|
||||
return rMap;
|
||||
}
|
||||
|
||||
private static int summation(Map<String, String> rMap) {
|
||||
int sum = 0;
|
||||
for (String col : rMap.keySet()) {
|
||||
String nv = rMap.get(col);
|
||||
sum += nv != null && nv.length() > 0 ? Integer.valueOf(nv) : 0;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static String textFileContentsToString(String filename) {
|
||||
StringBuilder lineOut = new StringBuilder();
|
||||
Scanner fs = null;
|
||||
try {
|
||||
fs = new Scanner(new File(filename));
|
||||
lineOut.append(filename);
|
||||
lineOut.append(NL);
|
||||
while (fs.hasNextLine()) {
|
||||
String line = fs.nextLine();
|
||||
lineOut.append(line);
|
||||
lineOut.append(NL);
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException ex) {
|
||||
// TODO Auto-generated catch block
|
||||
ex.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
if (fs != null) {
|
||||
fs.close();
|
||||
}
|
||||
}
|
||||
return lineOut.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue