Java read CSV File 📰

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 28. 07. 2024
  • IMPORTANT NOTE: If values in your CSV contain commas naturally, they will be split. (Ex. $1,000). If this is the case, replace:
    String[] row = line.split(",");
    with
    String[] row = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    ^ This is regex and is very complicated. It looks for string patterns. Explaining this requires another video entirely.
    //******************************************************
    import java.io.*;
    public class Main {
    public static void main(String[] args) {
    //CSV = Comma-Separated Values
    // text file that uses a comma to separate values
    String file = "src\\students.csv";
    BufferedReader reader = null;
    String line = "";
    try {
    reader = new BufferedReader(new FileReader(file));
    while((line = reader.readLine()) != null) {
    String[] row = line.split(",");
    //String[] row = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    //use this if your values already contain commas
    for(String index : row) {
    System.out.printf("%-10s", index);
    }
    System.out.println();
    }
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    finally {
    try {
    reader.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    //******************************************************
  • Věda a technologie

Komentáƙe • 102

  • @AwaidhGaming
    @AwaidhGaming Pƙed 2 lety +4

    You helped me in each and every step of my project ...Thanks

  • @Confusedcapybara8772
    @Confusedcapybara8772 Pƙed rokem +2

    Still the best coding channels on yt. love you bro

  • @BroCodez
    @BroCodez  Pƙed 4 lety +10

    IMPORTANT NOTE: If values in your CSV contain commas naturally, they will be split. (Ex. $1,000). If this is the case, replace:
    String[] row = line.split(",");
    with
    String[] row = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    ^ This is regex and is very complicated. It looks for string patterns. Explaining this requires another video entirely.
    //******************************************************
    import java.io.*;
    public class Main {
    public static void main(String[] args) {

    //CSV = Comma-Separated Values
    // text file that uses a comma to separate values

    String file = "src\\students.csv";
    BufferedReader reader = null;
    String line = "";

    try {
    reader = new BufferedReader(new FileReader(file));
    while((line = reader.readLine()) != null) {

    String[] row = line.split(",");
    //String[] row = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    //use this if your values already contain commas
    for(String index : row) {
    System.out.printf("%-10s", index);
    }
    System.out.println();
    }
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    finally {
    try {
    reader.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    //******************************************************

    • @anandtandon6489
      @anandtandon6489 Pƙed 8 měsĂ­ci

      Doesn't work if there are empty cells in the csv :(

  • @ignacioleonmorales7225
    @ignacioleonmorales7225 Pƙed 2 lety +3

    Bro thanks for your video, it helps me a lot. I'm starting in java programming, so it was very usefull.

  • @scilesful
    @scilesful Pƙed 3 lety +15

    any ideas about how to store values directly in a 2-Dimensional array?

  • @heavencanceller1863
    @heavencanceller1863 Pƙed 2 lety +1

    Your videos are a blessing

  • @muhammadsalah3981
    @muhammadsalah3981 Pƙed 4 lety

    Very good and clear illustration bro ❀

  • @kafwankamunshimbwe9235
    @kafwankamunshimbwe9235 Pƙed 2 lety

    Your video helped me out big time with my lab

  • @janetkalu3064
    @janetkalu3064 Pƙed rokem

    Thank you Brocode, you rock my tech world.

  • @svetlanamazhaykina6918
    @svetlanamazhaykina6918 Pƙed 2 měsĂ­ci

    Thanks for your work! Well done!

  • @frozen1093
    @frozen1093 Pƙed 2 lety +1

    Lol i saved it as csv but it was separated by ; and had to separat it by , manually
    Thanks for the tutorial!

  • @mohamedal-ziadi5640
    @mohamedal-ziadi5640 Pƙed 3 lety +5

    Very Helpful video thanks. Do you know how to convert CSV file to Java Objects?

  • @palo8818
    @palo8818 Pƙed 2 lety

    I love your tutorials

  • @lucyledezma709
    @lucyledezma709 Pƙed 4 lety +4

    Hi!. Your videos are so useful!. Thanks for share!

  • @tanmaytrivedi8188
    @tanmaytrivedi8188 Pƙed 2 lety

    Informative video for me actually i am a cse student and i want to read from csv and perform some operation on it and print it in pdf form . I will use concept to read from csv .

  • @simpleton7758
    @simpleton7758 Pƙed 2 lety

    very helpful~thank you so much.

  • @honey30prachi
    @honey30prachi Pƙed 2 lety

    Simple and helpful video.

  • @t_min_o
    @t_min_o Pƙed 2 lety

    Bro, helpful video, thanks a ton!. can you also make how to do it using apache csv parser please
    ?

  • @Alan-pz9gf
    @Alan-pz9gf Pƙed 3 lety +6

    Appreciate the video. By any chance do you have a how to write in a csv and how to place the info in a javaTable?

    • @vijayp9870
      @vijayp9870 Pƙed 2 lety

      Can you share how to write in csv

  • @raqiburrahman2666
    @raqiburrahman2666 Pƙed 3 lety

    Thank you so much... It works well

  • @Saikumar_Miryala
    @Saikumar_Miryala Pƙed 3 lety +2

    if the commma consists in between value then it fails

  • @Wadkar07
    @Wadkar07 Pƙed rokem

    bro u really make it easy every time, thankx for that, but one thing i want to know is what is the name of the song which u have in intro

  • @Adrastus_
    @Adrastus_ Pƙed rokem

    I've been trying to study and practice and learn java for about a year now but it has mainly been on and off and lately I find myself with no time to practice, so I basically feel like I forgot everything I have learned and am a beginner again. This is all to say, why would one ever create a java program to read a csv file, when what was printed here is the exact same thing as the google sheets? Is it a matter of formatting that you could do with java?

  • @relaxingmeditationmusic9761
    @relaxingmeditationmusic9761 Pƙed 3 lety +1

    How do i start reading the file at like the 3rd row?

  • @hristogeorgiev6531
    @hristogeorgiev6531 Pƙed 3 lety +11

    Bro very helpful tutorial. But what if I need to save each column in a separate array/ArrayList? Do you have a video where you show something similar?

    • @willvall7868
      @willvall7868 Pƙed 2 lety +1

      do you found any solution for that problem?

    • @hristogeorgiev6531
      @hristogeorgiev6531 Pƙed 2 lety +1

      @@willvall7868 Could you give me some more context to your problem? I found a solution but as you can see the comment was posted a year ago.

    • @willvall7868
      @willvall7868 Pƙed 2 lety +1

      @@hristogeorgiev6531 as you already told by yourself :) np. I found a solution by myself. But thank you for caring :D

    • @hristogeorgiev6531
      @hristogeorgiev6531 Pƙed 2 lety +1

      @@willvall7868 The problem is I myself don't remember what I was trying to do. Glad you found a solution though!

  • @user-vs9cq9nm5x
    @user-vs9cq9nm5x Pƙed 9 měsĂ­ci

    the best channel

  • @cristianbau2353
    @cristianbau2353 Pƙed 6 měsĂ­ci

    you saved me thank you man

  • @ruturajpal6500
    @ruturajpal6500 Pƙed rokem

    thanks for the code and video

  • @beingpeterkevin
    @beingpeterkevin Pƙed 2 lety

    Thank You bro!

  • @hamitalkin9294
    @hamitalkin9294 Pƙed 3 lety +1

    can some one give me a tutorial how to safe them into an two deminsonal array?

  • @mihailbelev
    @mihailbelev Pƙed 2 lety

    very helpful !!!

  • @chaer7898
    @chaer7898 Pƙed 4 lety +1

    Here bro sorry but I think I agree with guys comment.hope you can make a course on what he is saying cuz it will be great!!! Hiro code I love your videos hoping you will add java applet to your java course and we will do game development like creating adventure games and also we will do some algorithm and data structure and it will be more fun if we learn it through game development like development a sport game!!!!! thanks for video it has help me a lot......

    • @BroCodez
      @BroCodez  Pƙed 4 lety +2

      Well it does seem that the game tutorials have been doing well...

    • @chaer7898
      @chaer7898 Pƙed 4 lety

      Yeah so add more gaming development tutorials and aslo Java applet bro

  • @GreenT3A_4u
    @GreenT3A_4u Pƙed rokem

    What do I do if my csv file have empty cells? How do I skip them?

  • @orbmaster9815
    @orbmaster9815 Pƙed 2 lety

    It worked thank you

  • @user-mv4qh2jt7d
    @user-mv4qh2jt7d Pƙed 2 lety

    Thank you!

  • @SeasonedChicken5
    @SeasonedChicken5 Pƙed 2 lety +6

    How would you go about storing it in a 2D array?

  • @omidragon15
    @omidragon15 Pƙed 2 lety

    Good stuff

  • @sabermmirza
    @sabermmirza Pƙed 3 lety

    Thank you ❀

  • @barathn6239
    @barathn6239 Pƙed 2 lety

    How to write a new data in csv file using java, bro?

  • @user-gi1vc5qt6p
    @user-gi1vc5qt6p Pƙed rokem

    superb

  • @MdAbdullah-qz4jp
    @MdAbdullah-qz4jp Pƙed 3 lety +1

    How can I change something from a csv file. Like I want to make some condition. if a student gets 40 then he/she will pass.

  • @samsepiol7
    @samsepiol7 Pƙed 7 měsĂ­ci

    I have a question, I need to read 3 csv file. How can I make this ?

  • @nehalayaaz9406
    @nehalayaaz9406 Pƙed 4 lety

    Thanks 🙏

  • @DanielRamos-to1pd
    @DanielRamos-to1pd Pƙed 4 měsĂ­ci

    love you bro

  • @jeljelespuerta5068
    @jeljelespuerta5068 Pƙed 3 lety

    Bro code, question, is there a way for me to read a single row only? if there is can u help me with that? thanks! more power to your channel!

  • @Nomad5915
    @Nomad5915 Pƙed 3 lety

    Thank you

  • @anubhavsingh145
    @anubhavsingh145 Pƙed 2 lety

    How we can get extract data rowise

  • @Mohammed-tx1ok
    @Mohammed-tx1ok Pƙed 4 lety

    What is regular expression? I see it everywhere in java and it looks very difficult. Can you make a video for regular expression

    • @BroCodez
      @BroCodez  Pƙed 4 lety +2

      It's a sequence of characters that define a search pattern. The one that I posted looks for commas that are not within a set of " " and splits the line. I'd like to make a video on it but I don't think I'm ready to explain it yet

    • @Mohammed-tx1ok
      @Mohammed-tx1ok Pƙed 4 lety

      Bro Code alright that is fine

  • @gerdsfargen6687
    @gerdsfargen6687 Pƙed rokem

    OpenCSV and streams work great together though.

  • @abirbek4315
    @abirbek4315 Pƙed 3 lety

    thank youuuuuuuu

  • @devibala6949
    @devibala6949 Pƙed 2 lety

    Bro. How to read three different CSV files and convert it into json after mapping?

  • @eglencedostu6433
    @eglencedostu6433 Pƙed 4 lety

    Thanks

  • @mimikrama
    @mimikrama Pƙed rokem

    ty

  • @aniketchopra5061
    @aniketchopra5061 Pƙed rokem

    great

  • @sagarpapineni7843
    @sagarpapineni7843 Pƙed 3 lety

    Hi need your assistance

  • @stebboy
    @stebboy Pƙed 11 měsĂ­ci

    nice

  • @everydayartes
    @everydayartes Pƙed 2 lety

    Don’t work for android

  • @NachoGalvisRuiz
    @NachoGalvisRuiz Pƙed 2 lety

    Gracias

  • @darpyy8102
    @darpyy8102 Pƙed 9 měsĂ­ci

    wow!

  • @manojseenivasan8854
    @manojseenivasan8854 Pƙed 3 lety +1

    Bro do for Android development

  • @teenva
    @teenva Pƙed 10 měsĂ­ci

    ❀

  • @tong4925
    @tong4925 Pƙed 3 lety

    It's so funny that Snoop Dogg passed and Karen failed lmao

  • @kex6799
    @kex6799 Pƙed 2 měsĂ­ci

    alalalallalalala for the yt

  • @RaselAhmed-ix5ee
    @RaselAhmed-ix5ee Pƙed 3 lety

    bro i need an urgent help please reply me... i am stuck in a program please reply ASAP

    • @BroCodez
      @BroCodez  Pƙed 3 lety

      what's the problem?

    • @RaselAhmed-ix5ee
      @RaselAhmed-ix5ee Pƙed 3 lety

      @@BroCodez i need to read some data from student record and find the mean ,average , of marks of each student and print it another file?

    • @BroCodez
      @BroCodez  Pƙed 3 lety

      what's your current code look like?

    • @RaselAhmed-ix5ee
      @RaselAhmed-ix5ee Pƙed 3 lety

      @@BroCodez where can i send you the screenshot?

    • @BroCodez
      @BroCodez  Pƙed 3 lety

      if you can, post the code in the comments

  • @jgposner
    @jgposner Pƙed 2 lety

    👍

  • @sagarpapineni7843
    @sagarpapineni7843 Pƙed 3 lety

    Hello

  • @knucklechukka
    @knucklechukka Pƙed 9 měsĂ­ci

    commenting per instruction

  • @curtking8803
    @curtking8803 Pƙed 3 lety

    Every illustrated error catching exercise should be from a Karen like karen trying to divide by zero or some logical error

    • @BroCodez
      @BroCodez  Pƙed 3 lety

      That's usually Steve's job

  • @noah77
    @noah77 Pƙed 4 lety +1

    Hey bro, I commented a huuuuugggggeee comment on Qazi's latest video, please see it.

    • @BroCodez
      @BroCodez  Pƙed 4 lety +1

      What's the link?

    • @noah77
      @noah77 Pƙed 4 lety +1

      @@BroCodez czcams.com/video/MLfegvV79yY/video.html

  • @freelancepakistan424
    @freelancepakistan424 Pƙed 2 lety

    k

  • @augischadiegils.5109
    @augischadiegils.5109 Pƙed 3 lety

    ❀