generate excel file as output in Java - java

The following java code:
import java.io.File;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class extract
{
public static void main (String[] args) throws java.lang.Exception
{
String testData = new Scanner( new File("109.txt") ).useDelimiter("\\A").next();
//String testData = "#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d $1|1391436893 ?[176.08179, -13.839829, -1.0054213] %PKKV7|00:7f:28:3f:17:9d|-67|2437 %DC2VJ|f8:e4:fb:a0:06:f8|-71|2412 %VVWSP|00:7f:28:d5:92:65|-71|2462 %SVT8H|f8:e4:fb:8e:d6:9b|-77|2437 %ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452 %2X4C8|00:7f:28:44:23:da|-75|2437 %STDGD|f8:e4:fb:70:86:f4|-82|2462 %DeathStar|00:7f:28:be:c8:94|-84|2412 %Freeinternet|00:1e:58:f4:15:f7|-59|2437 %QB657|00:26:62:b7:16:4b|-88|2462 %375F2|00:26:b8:3e:0a:14|-70|2412 %E1K38|00:26:62:cf:90:37|-81|2412";
String regularExpression = "\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}\\W{1}-\\d{2}";
Pattern pattern = Pattern.compile(regularExpression);
Matcher matcher = pattern.matcher(testData);
while(matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
generates the following output:
00:1a:1e:87:04:42|-87
00:1a:1e:8e:e9:a2|-77
00:1a:1e:87:04:51|-95
00:1a:1e:84:92:02|-84
00:1a:1e:8d:f7:a2|-67
00:1a:1e:82:b8:e1|-56
00:1a:1e:82:b8:e2|-54
00:1a:1e:82:b8:e0|-56
00:1a:1e:87:04:41|-88
00:1a:1e:8d:f7:b1|-78
00:1a:1e:8d:f7:b2|-78
I'm trying to save output file as an excel file separated by columns.
Does anybody has any suggestions on how to achieve this?
Thank you!

Just add some commas (currently you don't have any), and save it as a text file named some_file_name.csv. You may use e.g. a BufferedWriter for this.
Saying 'Excel file separated by commas' is incorrect, actually the proper term is 'comma separated values file' or 'CSV file'. Programs other than Excel can open such files too.

Related

Replace Pattern match with Preferred Text Java

Hey I am trying to replace the a regex pattern in a directory of files and replace with this character 'X'. I started out trying alter one file but that is not working. I cam eup with the following code any help would be appreciated.
My goal is to read all the file content find the regex pattern and replace it.
Also this code is not working it runs but dose nothing to the text file.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class DataChange {
public static void main(String[] args) throws IOException {
String absolutePathOne = "C:\\Users\\hoflerj\\Desktop\\After\\test.txt";
String[] files = { "test.txt" };
for (String file : files) {
File f = new File(file);
String content = FileUtils.readFileToString(new File(absolutePathOne));
FileUtils.writeStringToFile(f, content.replaceAll("2018(.+)", "X"));
}
}
}
File Content inside the file is:
3-MAAAA2017/2/00346
I am trying to have it read through and replace 2017/2/00346 with XXX's
my goal is to do this for like 3 files at one time also.

Delete specific rownumbers in .csv file using java

For ex: I am trying search a text with name "abc"in .csv file which is present in column no 6 in multiple rows and I need to delete those rows.
I tried below code. I am able to get the line no/row no where text "abc" is present in column 6 but it is not deleting the rows.
import java.io.BufferedReader;
import java.io.*;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
public class ReadExcel {
public static void main(String[] args) throws Exception{
String csvFile = "csv filelocation";
CSVReader reader = new CSVReader(new FileReader(csvFile));
List<String[]> allElements = reader.readAll();
String [] nextLine;
int lineNumber = 0;
while ((nextLine = reader.readNext()) != null) {
lineNumber++;
if(nextLine[5].equalsIgnoreCase("abc")){
System.out.println("Line # " + lineNumber);
allElements.remove(lineNumber);
}
}
For reading the files in CSV format, I am currently using the library super-csv. There are various examples.
Let me know if you need help to use it.
So, if you would like to use the opencsv library, I start a new example for writing the new content in a CSV file. I take inspiration from your example code.
List<String[]> allElements; /* This list will contain the lines that cover your criteria */
/*
...
*/
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"));
writer.writeAll(allElements);
writer.close();

create regex with Xeger or Generex

I'm trying to get a valid regex for password using Xeger or Generex by this code:
import com.mifmif.common.regex.Generex;
import nl.flotsam.xeger.Xeger;
public class main {
public static void main(String[] args) {
String regex = "((?=.*[a-z])(?=.*\\d)(?=.*[A-Z])(?=.*[\\p{Punct}]).{8,12})";
Xeger generator = new Xeger(regex);
Generex generator1 = new Generex(regex);
System.out.println(generator.generate());
System.out.println(generator1.random());
}
}
But what I got in the console is 2 very long strings (much more than 12 chars) and with some very strange chars.
Example for such string:
?=??JY??p? x??x??f????v????c4\??6??'??y4?Xoq?d??tT??o??tH?r^c??l??v?l$????*n xF?+?:y??^?"?dD&o#????????p'f???s?lx[j?iW???^??=?????u??x???=??A??a??)KNyu?ֽSUu?c?wA??jrn???qEi????s??e?Ybp?oj???N#d????\iH?V?u
??c?eM?Y9???kF????b?]rco?gB??L?E?J?\(y1????d?wgg?Mc???D?e?c?o?C?j?w???w??kr?a?????y#VxDi???hCz?j??p?t????h??d5f??L??S??i?Y??z&?#w?X??<os)sF?o??]??M??????k?='????R?V?dPH#CWBV)$d?d?_?d`?%??????Y^??IEK??2?=????dK??d)?8Td]d?=UGK?! ????X#J?X&?MX?={nptrcT]kpp?^op|?o{pPpo
How can I create a valid string?
Thanks.

Java OpenCSV How to edit specific cells from csv file

I have a CSV file which looks like this:
http://gyazo.com/5dcfb8eca4e133cbeac87f514099e320.png
I need to figure out how I can read specific cells and update them in the file.
This is the code I am using:
import java.util.List;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.opencsv.*;
public class ReadCSV {
private static final char SEPARATOR = ';';
public static void updateCSV(String input, String output, String replace, int row, int col) throws IOException {
CSVReader reader = new CSVReader(new FileReader(input),SEPARATOR);
List<String[]> csvBody = reader.readAll();
csvBody.get(row)[col]=replace;
reader.close();
CSVWriter writer = new CSVWriter(new FileWriter(output),SEPARATOR,' ');
writer.writeAll(csvBody);
writer.flush();
writer.close();
}
public static void main(String[] args) throws IOException {
String source = "townhall_levels.csv";
String destiantion="output.csv";
ReadCSV.updateCSV(source, destiantion, "lol", 1, 1);
}
}
In this code I am just trying to change A1 to "lol" as an example test to see if it works but I get the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at ReadCSV.updateCSV(ReadCSV.java:16)
at ReadCSV.main(ReadCSV.java:30)
How should I go about achieving my goal and fixing the error?
CSV File: www.forumalliance.net/townhall_levels.csv
You're using ;as the separator to parse the file. Your file uses ,. Also, using a space as the quote char doesn't make much sense. You should use " instead, since that's also what your file uses.
The first values you're passing to row and col are 1 and 1. However, these need to start at 0.

extract information separated with pipes from file and save it to another file

considering the following file "sample.txt" as input for a java program which contains the following information:
#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d
$1|1391436893
?[176.08179, -13.839829, -1.0054213]
%PKKV7|00:7f:28:3f:17:9d|-67|2437
%DC2VJ|f8:e4:fb:a0:06:f8|-71|2412
%VVWSP|00:7f:28:d5:92:65|-71|2462
%SVT8H|f8:e4:fb:8e:d6:9b|-77|2437
%ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452
%2X4C8|00:7f:28:44:23:da|-75|2437
%STDGD|f8:e4:fb:70:86:f4|-82|2462
%DeathStar|00:7f:28:be:c8:94|-84|2412
%Freeinternet|00:1e:58:f4:15:f7|-59|2437
%QB657|00:26:62:b7:16:4b|-88|2462
%375F2|00:26:b8:3e:0a:14|-70|2412
%E1K38|00:26:62:cf:90:37|-81|2412
I'm trying to get an "output.txt" file as following:
00:7f:28:3f:17:9d|-67
f8:e4:fb:a0:06:f8|-71
00:7f:28:d5:92:65|-71
f8:e4:fb:8e:d6:9b|-77
20:10:7a:14:6a:f7|-66
00:7f:28:44:23:da|-75
f8:e4:fb:70:86:f4|-82
00:7f:28:be:c8:94|-84
00:1e:58:f4:15:f7|-59
00:26:62:b7:16:4b|-88
00:26:b8:3e:0a:14|-70
00:26:62:cf:90:37|-81
any recommendations on how to achieve this in java?
The following regular expression will extract this:
\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}\W{1}-\d{2}
http://ideone.com/FwpV7z
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String testData = "#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d $1|1391436893 ?[176.08179, -13.839829, -1.0054213] %PKKV7|00:7f:28:3f:17:9d|-67|2437 %DC2VJ|f8:e4:fb:a0:06:f8|-71|2412 %VVWSP|00:7f:28:d5:92:65|-71|2462 %SVT8H|f8:e4:fb:8e:d6:9b|-77|2437 %ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452 %2X4C8|00:7f:28:44:23:da|-75|2437 %STDGD|f8:e4:fb:70:86:f4|-82|2462 %DeathStar|00:7f:28:be:c8:94|-84|2412 %Freeinternet|00:1e:58:f4:15:f7|-59|2437 %QB657|00:26:62:b7:16:4b|-88|2462 %375F2|00:26:b8:3e:0a:14|-70|2412 %E1K38|00:26:62:cf:90:37|-81|2412";
String regularExpression = "\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}\\W{1}-\\d{2}";
Pattern pattern = Pattern.compile(regularExpression);
Matcher matcher = pattern.matcher(testData);
while(matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
output:
00:7f:28:3f:17:9d|-67
f8:e4:fb:a0:06:f8|-71
00:7f:28:d5:92:65|-71
f8:e4:fb:8e:d6:9b|-77
20:10:7a:14:6a:f7|-66
00:7f:28:44:23:da|-75
f8:e4:fb:70:86:f4|-82
If you read the line as a String you can use split to split it up for the pipes.
Here's a good start (might be typo's since I did write it straight in here)
//Read all lines, one line at a time with a simple scanner:
Scanner sc = new Scanner(new File("sample.txt"));
while (sc.hasNextLine()){
String line = sc.nextLine();
//If the line starts with a % do something, else ignore
if (line.charAt(0) == '%'){
int firstPipe = line.indexOf("|");
int secondPipe = line.indexOf("|",firstPipe);
//Now use String.substring() and perhaps a temporary StringBuffer storage followed by, for example, a FileWriter to create the output file

Categories