Tuesday, March 10, 2020

Why Java is Platform Independant - Detailed Discription

Most Important Question for Beginners

JDK , JVM , JRE & 
Why Java is Platform Independant??



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Why String is Immutable?


For Beginners as well Professionals , the most important and frequently asked question.
Why String is Immutable??


Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

How to create Custom Immutable Class?


How to create Custom Immutable Class?



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

The Difference between String , String Buffer and String Builder?


The Difference between String , String Buffer and String Builder?



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Wrapper Classes and AutoBixing and UnBoxing Concept?


Wrapper Classes and AutoBixing and UnBoxing Concept??



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Coupling and Cohesion



Coupling and Cohesion 



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Method Overloading and Method Overriding ?

Important Concept

Method Overloading and Method Overriding ?



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Inheritance , Association , Aggregation and Composition


Inheritance , Association , Aggregation and
Composition




Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Interface and Abstract Class


Interface and Abstract Class



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Concept of OOPS Programming Part-1


Concept of OOPS Programming
Part 1



Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

Concept of OOPs Programming Part-2


Concept of OOPs Programming
Part 2




Please like and subscribe our Youtube channel Java Wave. Click on the Link below to subscribe
https://www.youtube.com/channel/UCuH4HjG0CdEX43HT18UrUow/featured

How to read Pipe Separated Text File in Java

Hello Developer , I am going to read a .txt file which is having content/data separated by Pipe ( | )
For instance,it would look like below file-

PipeSeparatedFile.txt
C20 | Java | 25
C21 | Python | 26


So lets start with the key things-
1.If you want to store the contents , then it is better to create a Java Pojo class and save the content in fields. For instance i have created a Student class with below fields and storing the file contents in it.

2.Always give the correct file path so that file is read and processed.

3.Have the logic of reading the file contents in a separate method so that its readable and specific to function.

Below is the code for reading the .txt file

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


class Student{
    private String fieldOne;
    private String fieldTwo;
    private int fieldThree;
    public String getFieldOne() {
        return fieldOne;
    }
    public void setFieldOne(String fieldOne) {
        this.fieldOne = fieldOne;
    }
    public String getFieldTwo() {
        return fieldTwo;
    }
    public void setFieldTwo(String fieldTwo) {
        this.fieldTwo = fieldTwo;
    }
    public int getFieldThree() {
        return fieldThree;
    }
    public void setFieldThree(int fieldThree) {
        this.fieldThree = fieldThree;
    }
}

public class readingFileWithPipeSeparators {
    public static void main(String[] args) {

        String filePath = "E://docs//PipeSeparatedFile.txt";
        List<Student> listOfStudents = null;  //new ArrayList<Student>();
       
        File file = new File(filePath);
        if(file.exists()) {
            System.out.println("file exists");
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
               
                listOfStudents = readAndLoadFile(br);
                System.out.println(listOfStudents.size());
               
                fileReader.close();
                br.close();
               
            } catch (FileNotFoundException e) {           
                e.printStackTrace();
            }catch (Exception e) {           
                e.printStackTrace();
            }                       
        }
    }

    private static List<Student> readAndLoadFile(BufferedReader br) {
        System.out.println("into method readAndLoadFile");
        List<Student> listOfStudentsInfo = new ArrayList<Student>();
        String st;    
        try {
            while( (st = br.readLine()) != null) {
               
                String[] pairs = st.split("\\|",-1);
               
                if(pairs.length>1) {
                    System.out.println("data present");
                    Student student = new Student();
                   
                    student.setFieldOne(pairs[0].trim());
                    student.setFieldTwo(pairs[1].trim());
                    student.setFieldThree( Integer.parseInt(pairs[2].trim()));
                   
                    listOfStudentsInfo.add(student);
                               
                }             
            }
        } catch (IOException e) {
            e.printStackTrace();
        }       
        return listOfStudentsInfo;
    }
}


The .trim() is used to cut the unnecessary spaces from the content.

Happy coding and do subscribe!😉