i'm trying to read data from .csv file and it give me grabage so i will be pleased if someone help me


import java.io.*;
public class ReadFile {

        public  void File2 () throws FileNotFoundException, IOException{
        FileInputStream fstream = new FileInputStream("C:/myfile.csv");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader ( new InputStreamReader (in));
        String strline ;

        strline = br.readLine();

        while (strline != null){



the simplest way to read from a file is:

        Scanner sc=new Scanner(System.in);

        System.out.println("Please enter your files name and path i.e C:\\test.txt: ");//prompt for file name

        filename = sc.nextLine();//read in the file name

        Scanner fileToRead = null;

        try {

            fileToRead = new Scanner(new File(filename)); //point the scanner method to a file

            //check if there is a next line and it is not null and then read it in
            for (String line; fileToRead.hasNextLine() && (line = fileToRead.nextLine()) != null; ) {

                System.out.println(line);//print each line as its read

            fileToRead.close();//this is used to release the scanner from file

or this:

        try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(filename))));

        for (String line; (line = reader.readLine()) != null;) {



        } catch (IOException ex) {

       System.out.println("The file " + filename + " could not be found or opened! "+ex.getMessage());


Edited 4 Years Ago by DavidKroukamp: code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;

public class ParseCSVFileExample {

        public static void main(String[] args) {


                        //csv file containing data
                        String strFile = "C:/FileIO/example.csv";

                        //create BufferedReader to read csv file
                        BufferedReader br = new BufferedReader( new FileReader(strFile));
                        String strLine = "";
                        StringTokenizer st = null;
                        int lineNumber = 0, tokenNumber = 0;

                        //read comma separated file line by line
                        while( (strLine = br.readLine()) != null)

                                //break comma separated line using ","
                                st = new StringTokenizer(strLine, ",");

                                        //display csv values
                                        System.out.println("Line # " + lineNumber +
                                                        ", Token # " + tokenNumber
                                                        + ", Token : "+ st.nextToken());

                                //reset token number
                                tokenNumber = 0;


                catch(Exception e)
                        System.out.println("Exception while reading csv file: " + e);                  

this is an example of csv parser.Hope you can modify it according to your requirement.

@norm1: it gives me squares with avery weird pattern

@cor : is this code applicable for reading from excel file

@iim : how can i put the readed data in ArrayList

thank u guys very much
i now can read from the file , but how can I put this data in an ArrayList to use it

sorry for distrubtion
now the situation changed ican now read from file to array list
but i want to take specific element from file to arraylist and leave other how can i do it

You mean that you only want certain elemtents in your arraylist?

If so, simply add an if statement within the loop you use to get text from your file to your arraylist, and put the add() method inside the if statement.

This question has already been answered. Start a new discussion instead.