Javafx: Reading from an File and Spliting the result with .split method - java

I want to by reading the data of a file to split the results based on .split(",") in another words for this particular example i want to have 2 Indexes with each containing up to 5 informations which i would also like to acces with the .[0] and .[1] Method.
the File with the Data.
File Reading Method.
public void fileReading(ActionEvent event) throws IOException {
File file = new File("src/DateSpeicher/datenSpeicher.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
}
}
The method does work very greatly however, i wonder how can i split those two in two Indexes or String arrays which both can be accessed through respective indecies [0], [1]. For first data in the firm array - 655464 [0][0] for last in the second Array [1][4].
My approach:
1. Making an ArrayList for every ,
2. Adding data till ","
Issue: eventho approach above works, you cant do such things as array1[0] - it gives an error, however the index method is crucial.
How can i solve this problem?

Path path = Paths.get("src/DateSpeicher/datenSpeicher.txt"); // Or:
Path path = Paths.get(new URL("/DateSpeicher/datenSpeicher.txt").toURI());
Either two Strings, and then handling them:
String content = new String(Files.readAllBytes(path), Charset.defaultCharset());
String[] data = content.split(",\\R");
or a list of lists:
List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
// Result:
List<List<String>> lists = new ArrayList<>();
List<String> newList = null;
boolean addNewList = true;
for (int i = 0; i < lines.size(); ++i) {
if (addNewList) {
newList = new ArrayList<>();
lists.add(newList);
addNewList = false;
}
String line = lines.get(i);
if (line.endsWith(",")) {
line = line.substring(0, line.length() - 1);
addNewList = true;
}
newList.add(line);
}

Related

csv with two delimiters

I am trying to read a csv with two delimiters, which has the following format:
Payment Date,Amount,Member No/branchno
2018/01/25,58,294416/0
the first part is the date and the last column is the column I am facing issues with. I need to split that last column into two columns after the slash.
my problem is that i do not know how to separate the last column without affecting the first column, any Help is really appreciated.
I can already read through the csv and split the commas.
here is the code for reading through the csv:
public ArrayList<String[]> ReadCSVFile(File DataFile)
{
try(BufferedReader reader = new BufferedReader (new FileReader(DataFile));){
//while loop to read through the data, while bufferedreader is not null-do ....
while(reader.readLine()!= null)
{
String read = reader.readLine();//bufferedreader string variable
String[] OneRow = read.split(","||"/");
rs2.add(OneRow);
System.out.println(Arrays.toString(OneRow));
//
}
//BufferedReader to Read through CSV Contents
reader.close();
}//end try
catch(Exception ex){
String errmsg = ex.getMessage();
//System.out.println("File not Found: "+errmsg);
}//end exception handling
You can do something like this;
while (reader.readLine() != null) {
String read = reader.readLine();// bufferedreader string variable
String[] rawRow = read.split(",");
String lastEntry = rawRow[rawRow.length - 1]; // this contains Member No/branchno
String[] properLastEntry = lastEntry.split("/"); // this contains Member No, branchno
String[] oneRow = new String[rawRow.length + 1];
System.arraycopy(rawRow, 0, oneRow, 0, rawRow.length - 1);
System.arraycopy(properLastEntry, 0, oneRow, oneRow.length - 2, 2);
}
If you don't want to hard code the length of 2 in the properLastEntry array, you can do something like this instead
while (reader.readLine() != null) {
String read = reader.readLine();// bufferedreader string variable
String[] rawRow = read.split(",");
String lastEntry = rawRow[rawRow.length - 1]; // this contains Member No/branchno
String[] properLastEntry = lastEntry.split("/"); // this contains Member No, branchno as entries
// create a memory which can contain rawRow and properLastEntry in a single
// array
String[] oneRow = new String[rawRow.length - 1 + properLastEntry.length];
// copy the data for the finalRow
System.arraycopy(rawRow, 0, oneRow, 0, rawRow.length - 1);
System.arraycopy(properLastEntry, 0, oneRow, oneRow.length - properLastEntry.length,
properLastEntry.length);
}
You want to split string based on two delimiters so you can mention multiple delimiters as follows
split(",|/")
Please go through the below link.
Use string split() with multiple delimiters

How can i read the same file two times in Java?

I want to counter the lines of the file and in the second pass i want to take every single line and manipulating it. It doesn't have a compilation error but it can't go inside the second while ((line = br.readLine()) != null) .
Is there a different way to get the lines(movies) of the file and storing in an array ?
BufferedReader br = null;
try { // try to read the file
br = new BufferedReader(new FileReader("movies.txt"));
String line;
int numberOfMovies = 0;
while ((line = br.readLine()) != null) {
numberOfMovies++;
}
Movie[] movies = new Movie[numberOfMovies]; // store in a Movie
// array every movie of
// the file
String title = "";
int id = 0;
int likes = 0;
int icounter = 0; // count to create new movie for each line
while ((line = br.readLine()) != null) {
line = line.trim();
line = line.replaceAll("/t", "");
line = line.toLowerCase();
String[] tokens = line.split(" "); // store every token in a
// string array
id = Integer.parseInt(tokens[0]);
likes = Integer.parseInt(tokens[tokens.length]);
for (int i = 1; i < tokens.length; i++) {
title = title + " " + tokens[i];
}
movies[icounter] = new Movie(id, title, likes);
icounter++;
}
} catch (IOException e) {
e.printStackTrace();
}
Simplest way would be to reset br again.
try { // try to read the file
br = new BufferedReader(new FileReader("movies.txt"));
String line; int numberOfMovies = 0;
while (br.hasNextLine()){
numberOfMovies++;
}
br.close();
Movie[] movies = new Movie[numberOfMovies];
// store in a Movie
// array every movie of
// the file
String title = "";
int id = 0;
int likes = 0;
int icounter = 0;
// count to create new movie for each line
br = new BufferedReader(new FileReader("movies.txt"));
while ((br.hasNextLine()) {
line = line.trim();
line = line.replaceAll("/t", "");
line = line.toLowerCase();
String[] tokens = line.split(" ");
// store every token in a
// string array
id = Integer.parseInt(tokens[0]);
likes = Integer.parseInt(tokens[tokens.length]);
for (int i = 1; i < tokens.length; i++) {
title = title + " " + tokens[i];
}
movies[icounter] = new Movie(id, title, likes);
icounter++;
}
} catch (IOException e) { e.printStackTrace(); }
I changed br.nextLine() != null to br.hasNextLine() because it's shorter and more appropriate in this case. Plus it won't consume a line.
There are two things here:
InputStreams and Readers are one-shot structures: once you've read them to the end, you either need to explicitly rewind them (if they support rewinding), or you need to close them (always close your streams and readers!) and open a new one.
However in this case the two passes are completely unnecessary, just use a dynamically growing structure to collect your Movie objects instead of arrays: an ArrayList for example.
Firstly, there is no need to read the file twice.
Secondly, why don't you use the java.nio.file.Files class to read your file.
It has a method readAllLines(Path path, Charset cs) that gives you back a List<String>.
Then if you want to know how many lines just call the size() method on the list and you can use the list to construct the Movie objects.
List<Movie> movieList = new ArrayList<>();
for (String line : Files.readAllLines(Paths.get("movies.txt"), Charset.defaultCharset())) {
// Construct your Movie object from each individual line and add to the list of Movies
movieList.add(new Movie(id, title, likes));
}
The use of the Files class also reduces your boilerplate code as it will handle closing the resource when it has completed reading meaning you will not need a finally block to close anything.
If you use the same Reader, everything is already read once you reach the second loop.
Close the first Reader, then create another one to read a second time.
You are running through the file with the BufferedReader, until the nextline points towards null. As your BufferedReader IS null, it won't even enter the second while((line = br.readline) != null), as the first read line is null.
Try getting a new BufferedReader. something like this:
...
int id = 0;
int likes = 0;
int icounter = 0;
br = new BufferedReader(new FileReader("movies.txt")) //Re-initialize the br to point
//onto the first line again
while ((line = br.readLine()) != null)
...
EDIT:
Close the reader first..
This is a combination of a couple of other answers already on this post, but this is how I would go about rewriting your code to populate a List. This doubly solves the problem of 1) needing to read the file twice 2) removing the boilerplate around using BufferedReader while using Java8 Streams to make the initializing of your List as concise as possible:
private static class Movie {
private Movie(int id, String title, int likes) {
//TODO: set your instance state here
}
}
private static Movie movieFromFileLine(String line) {
line = line.trim();
line = line.replaceAll("/t", "");
line = line.toLowerCase();
String[] tokens = line.split(" "); // store every token in a
String title = "";
int id = Integer.parseInt(tokens[0]);
int likes = Integer.parseInt(tokens[tokens.length]);
for (int i = 1; i < tokens.length; i++) {
title = title + " " + tokens[i];
}
return new Movie(id, title, likes);
}
public static void main(String[] args) throws IOException {
List<Movie> movies = Files.readAllLines(Paths.get("movies.txt"), Charset.defaultCharset()).stream().map
(App::movieFromFileLine).collect(Collectors.toList());
//TODO: Make some magic with your list of Movies
}
For cases where you absolutely need to read a source (file, URL, or other) twice, then you need to be aware that it is quite possible for the contents to change between the first and second readings and be prepared to handle those differences.
If you can make a reasonable assumption that the content of the source will fit in to memory and your code fully expects to work on multiple instances of Readers/InputStreams, you may first consider using an appropriate IOUtils.copy method from commons-io to read the contents of the source and copy it to a ByteArrayOutputStream to create a byte[] that can be re-read over and over again.

comparing two string arrays using java

we are trying to compare two string arrays( as[ ] and bs[ ]) and update the array string as[ ] with the new strings present in bs[ ] .We are not able to update the as[ ].Pls help us with the following codes.Thank u;)
public class Aa {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// Create an array of 4 strings (indexes 0 - 3)
String as[] = new String[5];
String bs[] = new String[16];
int i;
try {
// Create a bufferreader object to read our file with.
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
BufferedReader reader1;
reader1 = new BufferedReader(new FileReader("a1.txt"));
// Line will hold our line read from the file
String line = "";
String line1 = "";
// The counter will keep track of how many lines we have read
int counter = 0;
int counter1 = 0;
// Read in a line from the file and store it in "line". Do this while we don't hit null or while the counter is less than 4.
// The counter prevents us from reading in too many lines.
while (((line = reader.readLine()) != null) && (counter < 4)) {
as[counter] = line;
counter++;
}
while (((line1 = reader1.readLine()) != null) && (counter1 < 16)) {
bs[counter1] = line1;
counter1++;
}
System.out.println("value"+as[0]);
System.out.println("value"+bs[0]);
int temp,temp1,j;
temp=as.length;
temp1=bs.length;
System.out.println("length:"+temp);
System.out.println("length1:"+temp1);
for(i=0;i<bs.length;i++)
{
for(j=0;j<as.length;j++)
{
if(as[j].equals(bs[i]))
{
//ignore
}
else
{
temp++;
as[temp]=bs[i];
}
}
}
// With a foreach style loop we loop through the array of strings and print them out to show they were read in.
reader1.close();
reader.close();
}
catch (Exception ex) { System.out.println("Exception: " + ex.getMessage()); }
}
}
Since you are using two arrays containing only strings, its better to convert both to list and add
List aList = (Arrays.asList(as));
List bList = (Arrays.asList(bs));
bList.removeAll(aList); // assuming you have some common objects in both
aList.addAll(bList);
as = aList.toArray(); // Convert back to array
Take a look at Apache Commons ArrayUtils:
You can use the combination of contains and a third temporary Array to store the differences (i.e. !contains).
Thanks.
else
{
temp++;
as[temp]=bs[i];
}
This doesn't work at Java as Thilo said in comments. You can not increase size of an array once its size is set.
I suggest to use ArrayList instead of array. You can simply add new items to an array list without any problem.
If you insist on using arrays, you can create a longer new array and copy your old array in here and add your new element. I wouldn't recommend this.

How To Compare Values Between Two Different Sized Csv Files?

I was wondering what is the most appropriate way of looping through two csv files and comparing their columns. Specifically I want to compare the csv file1 1st column to every iteration of csv file2 column 20 and check to see if there is a match. Here is what i have so far. In addition csv file1 is considerably smaller than csv file2.
public class ClassifyData {
public static void main(String[]args) throws IOException{
File file1 = new File("file1.csv");
File file2 = new File("file2.csv");
FileWriter writer = new FileWriter("/Users/home/Work.csv");
PrintWriter pw = new PrintWriter(writer);
Scanner in = new Scanner(file1);
Scanner in2 = new Scanner(file2);
boolean firstLine = true;
String[] temp = null;
String [] temp2 = null;
String line = null;
String line2 = null;
while((line = in.nextLine())!=null){
temp= line.split(",");
while(line2 = in2.nextLine() !=null){
temp2 = line2.split(",");
if(temp[0] == temp[20]){
System.out.println("match");
pw.append("0");
continue;
}
pw.append("\n");
}
}
pw.flush();
pw.close();
writer.close();
}
}
In the line if(temp[0] == temp[20]) you probably mean if(temp[0].equals(temp2[20])). This will give you the comparison you want. However, you're inner while loop still won't start over at the beginning of the second file like you seem to want. I don't think Scanner objects can start over on a file, and even if they could, you'd be wasting a lot of file reads by reading the same file over and over. Something like this will be more efficient for your disk:
ArrayList<String> list1 = new ArrayList<String>;
while((line = in.nextLine())!=null){
temp= line.split(",");
list1.add(temp[0]);
}
// ...
for(int i = 0; i < list1.size(); i++){
for(int j = 0; j < list2.size(); j++){
if(list1.get(i).equals(list2.get(j))){
System.out.println("Match found");
}
}
}
Warning: untested code
I don't think your solution is going to work because you're going through both files just once (you're sequentially incrementing through both files simultaneously). Given that the first file is small, I suggest going through that file completely once, and store the values in the first column in a hashtable. Then cycle through the second file, and check if the value in the 20th column appears in the hashtable or not.

How to read a String (file) to array in java

Suppose there is a file named as SUN.txt
File contains : a,b,dd,ss,
I want to make dynamic array depending upon the number of attributes in file.
If ther is a char after comma then array will be of 0-4 i.e of length 5.
In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too.
How do i do that?
Sundhas
You should think about
Reading the file into a String
Splitting the file by separator ','
Using a list for adding the characters and convert the list to an array, when the list is filled
As Markus said, you want to do something like this..
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//The StringBuffer will be used to create a string if your file has multiple lines
StringBuffer sb = new StringBuffer();
String line;
while((line = reader.readLine())!= null)
{
sb.append(line);
}
//We now split the line on the "," to get a string array of the values
String [] store = sb.toString().split(",");
I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is.
If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in.
If you would like a NULL you would have to add it in yourself at the end so you could do something like
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//Use an arraylist to store the values including nulls
ArrayList<String> store = new ArrayList<String>();
String line;
while((line = reader.readLine())!= null)
{
String [] splitLine = line.split(",");
for(String x : splitLine)
{
store.add(line);
}
//This tests to see if the last character of the line is , and will add a null into the array list
if(line.endsWith(","))
store.add(null);
}
String [] storeWithNull = store.toArray();
Well if you want want to simply open the file and store the content in a array of string then
1) open the file into a string
2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
but I'm curious why you can't use a String file directly ?
For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
data.add(line.split(","));
line = readNextLine();
}
(assuming, your file contains 1..n lines of comma separated values)
You may want to have it like this:
"a,b,c,d," -> {"a", "b", "c", "d", null}
Here's a suggestion how to solve that problem:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
String[] values = new String[5];
String[] pieces = line.split(",");
for (int i = 0; i<pieces.length; i++)
values[i] = pieces[i];
data.add(values);
line = readNextLine();
}
its seems like a CSV file something like this will work assuming it has 5 lines and 5 values
String [][] value = new String [5][5];
File file = new File("SUN.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
while((line = br.readLine()) != null ){
StringTokenizer s = new StringTokenizer(line,",");
while (s.hasMoreTokens()){
value[row][col] = s.nextToken();
col++;
}
col = 0;
row++;
}
i havent tested this code
Read the file, using BufferedReader, one line at the time.
Use split(",", -1) to convert to an array of String[] including also empty strings beyond the last comma as part of your array.
Load the String[] parts into a List.

Categories