Java read specific parts of a CSV - java

I created the following code to read a CSV-file:
public void read(String csvFile) {
try {
File file = new File(csvFile);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = "";
String[] tempArr;
while((line = br.readLine()) != null) {
tempArr = line.split(ABSTAND);
anzahl++;
for(String tempStr : tempArr) {
System.out.print(tempStr + " ");
}
System.out.println();
}
br.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
I have a CSV with more than 300'000 lines that look like that:
{9149F314-862B-4DBC-B291-05A083658D69};Gebaeude;TLM_GEBAEUDE;;Schiessstand;{41C949A2-9F7B-41EE-93FD-631B76F2176D};Altdorf 300m;offiziell;Hochdeutsch inkl. Lokalsprachen;Einfacher Name;;684600;295930;400
How can I now only get the some parts out of that? I only need the bold/italic parts to work with.

Without further specifying what your requirements/input limitations are the following should work within your loop.
String str = "{9149F314-862B-4DBC-B291-05A083658D69};Gebaeude;TLM_GEBAEUDE;;Schiessstand;{41C949A2-9F7B-41EE-93FD-631B76F2176D};Altdorf 300m;offiziell;Hochdeutsch inkl. Lokalsprachen;Einfacher Name;;684600;295930;400";
String[] arr = str.split("[; ]", -1);
int cnt=0;
// for (String a : arr)
// System.out.println(cnt++ + ": " + a);
System.out.println(arr[6] + ", " + arr[15] + ", " + arr[16]);
Note that this assumes your delimiters are either a semicolon or a space and that the fields desired are in the fix positions (6, 15, 16).
Result:
Altdorf, 684600, 295930

Related

How to read in a file into a specific format

I have a file containing some data that looks like this.
[Todays date] Some text
some more text
even more text
[A different date] Some text
[Another different date] More text
Final text block
I'd like to read this into a List<String> if possible but I don't want to have it exactly like the file. Ideally the List<String> would look like.
[Todays date] some text some more text even more text
[A different date] some text
[Another different date] More text Final text block
If the line of text doesn't start with a squared bracket I want to just concatenate that line to the above line. I can read it in normally using this code. I tried to use String.startsWith but I couldn't figure it out.
List<String> testList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("SystemOut.log"));
String line = br.readLine();
while (line!=null) {
testList.add(line);
line=br.readLine();
}
br.close();
I'm looking for either a change to this method that will make it read it in to my desired format or maybe a method that will act on my List<String> and sort this problem. Thanks
A better solution (3rd attempt) which (hopefully) will work better with large files as it avoids reading the whole file into buffer:
public static void main(String[] args) throws IOException {
String input = "[04/06/2021] Some text\n" +
"some more text\n" +
"even more text\n" +
"[01/01/2020] Some text \n" +
"[31/12/2020] More text\n" +
"Final text block";
List<String> testList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new StringReader(input))) {
br.lines().forEach(
line -> {
if (testList.isEmpty() || line.startsWith("[")) {
testList.add(line + " ");
} else {
testList.set(
testList.size() - 1,
testList.get(testList.size() - 1) + line + " ");
}
}
);
}
testList.forEach(System.out::println);
}
I came up with this tedious method:
public static void main(String[] args) throws IOException {
String input = "[04/06/2021] Some text\n" +
"some more text\n" +
"even more text\n" +
"[01/01/2020] Some text \n" +
"[31/12/2020] More text\n" +
"Final text block";
List<String> testList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new StringReader(input))) {
String nextLine = br.readLine();
StringBuilder currentLine = new StringBuilder(nextLine + " ");
while (nextLine != null) {
nextLine = br.readLine();
if (nextLine != null) {
if (nextLine.startsWith("[")) {
testList.add(currentLine.toString());
currentLine = new StringBuilder();
}
currentLine.append(nextLine).append(" ");
}
}
if (currentLine.length() > 0) {
testList.add(currentLine.toString());
}
}
testList.forEach(System.out::println);
}
If you can move away from your loop, a better/simpler approach would be:
public static void main(String[] args) throws IOException {
String input = "[04/06/2021] Some text\n" +
"some more text\n" +
"even more text\n" +
"[01/01/2020] Some text \n" +
"[31/12/2020] More text\n" +
"Final text block";
List<String> testList = new ArrayList<>();
String[] inputs = input.split("\\n");
StringBuilder currentLine = new StringBuilder(inputs[0] + " ");
for (int i = 1; i < inputs.length; i++) {
if (inputs[i].startsWith("[")) {
testList.add(currentLine.toString());
currentLine = new StringBuilder();
}
currentLine.append(inputs[i]).append(" ");
}
testList.add(currentLine.toString());
testList.forEach(System.out::println);
}
Output:
[04/06/2021] Some text some more text even more text
[01/01/2020] Some text
[31/12/2020] More text Final text block

Read csv row by row using java and concatenate as string

I have a CSV(2 columns) that has 1000 rows. The task I am working on is to retrieve a set of 100 first column rows and concatenate as a string( 'col1row1'+'col1row2'+'col1row3'+....+'col1row100' ). So this formatted string i will use as a parameter for a new function.
The following code only displays my CSV(having 2 columns) as an Array. Can someone help me here?
public static void main(String[] args) {
try {
String csvFile = "data.csv";
File file = new File(csvFile);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = null;
String tempArr[] ;
br.readLine();
while((line = br.readLine()) != null) {
tempArr = line.split(" ");
System.out.print(Arrays.toString(tempArr) + " ");
}
br.close();
} catch(Exception ioe) {
ioe.printStackTrace();
}
}
String[] colArr= br. readLine(). split(" ");
String col1= colArr[0], col2=colArr[1];
while(...){
tempArr= line. split();
col1Str += col1 + tempArr[0];
col2Str += col2 + tempArr[1];
}

Removing Array Items from file

lets say we have a list of array items
ex:- a,b,c,d
which needs to be removed from a file which is full of data , I am missing something can anyone please help me in achieving this , thanks in advance
public static void rmvFromXML(String strFilePath,
String strTmpFilePath) throws IOException {
String currentLine = "";
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
BufferedReader reader = new BufferedReader(new FileReader(strFilePath));
BufferedWriter fileWriter = null;
fileWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(strTmpFilePath)));
while ((currentLine = reader.readLine()) != null) {
String trimmedLine = currentLine.trim();
System.out.println("Trimmed Line :- " + trimmedLine);
for (String value : list) {
System.out.println("Array Value:- " + value);
if (trimmedLine.equals(value))
continue;
fileWriter.write(trimmedLine + System.getProperty("line.separator"));
}
}
fileWriter.close();
reader.close();
}
you can use regex. so that you wont have to iterate and check whether the
line contains your strings
while ((currentLine = reader.readLine()) != null) {
Pattern removeWords = Pattern.compile("\\b(?:a|b|c|d)\\b\\s*", Pattern.CASE_INSENSITIVE);
Matcher fix = removeWords.matcher(currentLine);
String fixedString = fix.replaceAll("");
}
try doing it with above approach
input :
abcdefg
output
efg
You need to replace provided substrings on each occurrence in lines from file, so replace it with empty spaces:
public static void rmvFromXML(String strFilePath,
String strTmpFilePath) throws IOException {
String currentLine = "";
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
BufferedReader reader = new BufferedReader(new FileReader(strFilePath));
BufferedWriter fileWriter = null;
fileWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(strTmpFilePath)));
while ((currentLine = reader.readLine()) != null) {
String trimmedLine = currentLine.trim();
System.out.println("Trimmed Line :- " + trimmedLine);
for (String value : list) {
System.out.println("Array Value:- " + value);
trimmedLine = trimmedLine.replaceAll(value, "");
}
fileWriter.write(trimmedLine + System.getProperty("line.separator"));
}
fileWriter.close();
reader.close();
}
So now for input:
sdfsdf
sdfsdf
sfsdfs
fa
a
b
c
asdasdad
d
asdasd
We get output:
sfsf
sfsf
sfsfs
f
ss
ss
Replace:
for (String value : list) {
System.out.println("Array Value:- " + value);
if (trimmedLine.equals(value))
continue;
fileWriter.write(trimmedLine + System.getProperty("line.separator"));
}
by:
bool found=false:
for (String value : list) {
System.out.println("Array Value:- " + value);
if (trimmedLine.equals(value))
found=true;
}
if(!found)
fileWriter.write(trimmedLine + System.getProperty("line.separator"));
The fileWriter.write call should be outside of the "for" loop.

How to trim last 4 characters of each elements present in an arraylist in java?

I have imported a set of values into an array list from a csv file. Now i need to remove the extension .tar from each element or allow only first 8 characters to be inserted into the array list while importing it from a csv file. This is my code and i want that change in array1 part
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.lang.*;
public class compare
{
public static void main(String[] args)
{
ArrayList<String> array = new ArrayList<String>(); //Array for storing values
ArrayList<String> array1 = new ArrayList<String>(); //Array for storing values
try
{
String strFile = "D:\\Ramakanth\\PT2573\\ftp.csv"; //csv file containing data
BufferedReader br = new BufferedReader( new FileReader(strFile)); //create BufferedReader to
String strLine = "";
StringTokenizer st = null;
while( (strLine = br.readLine()) != null) //read comma separated file line by line
{
st = new StringTokenizer(strLine, ","); //break comma separated line using ","
while(st.hasMoreTokens())
{
array.add(st.nextToken()); //store csv values in array
}
}
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);
}
try
{
String strFile1 = "D:\\Ramakanth\\PT2573\\target.csv"; //csv file containing data
BufferedReader br1 = new BufferedReader( new FileReader(strFile1)); //create BufferedReader
String strLine1 = "";
StringTokenizer st1 = null;
while( (strLine1 = br1.readLine()) != null) //read comma separated file line by line
{
st1 = new StringTokenizer(strLine1, ","); //break comma separated line using ","
while(st1.hasMoreTokens())
{
array1.add(st1.nextToken()); //store csv values in array
}
}
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);
}
array.removeAll(array1);
System.out.println(array);
try
{
BufferedWriter br2 = new BufferedWriter(new FileWriter("D:\\Ramakanth\\PT2573\\output.csv"));
StringBuilder sb1 = new StringBuilder();
for (String element : array)
{
sb1.append(element);
sb1.append(" ");
}
br2.write(sb1.toString());
br2.close();
}
catch(Exception e)
{
System.out.println("Exception while writing csv file: " + e);
}
}
}
Try this,
String element = "extension.tar";
int index = element.lastIndexOf(".");
System.out.println("if Dot extension means : " + element.substring(0, index));
element = element.substring(0, 8);
System.out.println("if first 8 character means : " + element);
output:
if Dot extension means : extension
if first 8 character means : extensio
You can do by using StringUtils.substringBefore class by using
StringUtils.substringBefore(yourstring, ".tar" );
It will give you the desired output

BufferedReader - count lines containing a string

I am using a .txt file that contains: "Hello world\nHow are you doing this day?" I want to count whether a line contains a string or not, as well as the total number of lines. I use:
File file = new File(file_Path);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int i=0;
int j=0;
while ((line = br.readLine()) != null) {
j++;
if (line.contains("o")) { //<----------
i++;
}
}
System.out.print("Lines containing the string: " + i + " of total lines " + j-1);
As I run and test line.contains("o"), it prints 2 lines containing "o", which is correct as well as 2 total lines. As I run line.contains("world"), it prints 0 lines which is wrong but gives 2 lines total. But what do I do wrong?
I tested it with a StringReader,
String str = "Hello world\nHow are you doing this day?";
StringReader sr = new StringReader(str);
try {
BufferedReader br = new BufferedReader(sr);
String line;
int i = 0;
int j = 0;
while ((line = br.readLine()) != null) {
j++;
if (line.contains("world")) { // <----------
i++;
}
}
System.out
.println("Lines containing the string: " + i
+ " of total lines " + j);
} catch (Exception e) {
e.printStackTrace();
}
Your file contents must not be what you think because I get
Lines containing the string: 1 of total lines 2
As the others answers and comments, I also think you may not be reading the file you think you are... (Relax it happens to everyone from time to time)
But, also it could be the encoder of the file or the version of the jdk you have, maybe if you could answer:
What did you use to create the file?
What OS you are running
this?
What JDK are you using?
It could clarify what may have happened
Just for you to know, I ran the same code you have using jdk8 and worked fine for me.
As follows the test I did:
1) I put your code in a function:
int countLines(String filename, String wording) {
File file = new File(filename);
String line;
int rowsWithWord = 0;
int totalRows = 0;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
while ((line = br.readLine()) != null) {
totalRows++;
if (line.contains(wording)) {
rowsWithWord++;
}
}
} catch (IOException e) {
System.out.println("Error Counting: " + e.getMessage());
}
System.out.println(String.format("Found %s rows in %s total rows", rowsWithWord, totalRows));
return rowsWithWord;
}
2) and ran the following unit test
#Test
public void testFile() {
try (FileWriter fileWriter = new FileWriter(new File("C:\\TEMP\\DELETE\\Hello.txt"));
BufferedWriter writer = new BufferedWriter(fileWriter)) {
writer.write("Hello world\nHow are you doing this day?");
} catch (IOException e) {
System.out.println("Error writing... " + e);
}
int countO = fileUtils.countLines("C:\\TEMP\\DELETE\\Hello.txt", "o");
Assert.assertEquals("It did not find 2 lines with the letters = o", 2, countO);
int countWorld = fileUtils.countLines("C:\\TEMP\\DELETE\\Hello.txt", "world");
Assert.assertEquals("It did not find 1 line with the word = world", 1, countWorld);
}
And I got the expected result:
Found 2 rows in 2 total rows
Found 1 rows in 2 total rows

Categories

Resources