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

thanks

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){
            System.out.println(strline);
            System.out.println();
        }
        in.close();

    }

    }

Please post some of what is printed out.
Why are you using the DataInputStream?

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;) {

        System.out.println(line);

        }

        } catch (IOException ex) {

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

       System.exit(0);
        }

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) {

                try
                {

                        //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)
                        {
                                lineNumber++;

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

                                while(st.hasMoreTokens())
                                {
                                        //display csv values
                                        tokenNumber++;
                                        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

csv files contain text. Use the classes shown in IIM's post

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

Did you try using the add() method?

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

Read the API doc for the ArrayList class. It has methods for doing things with the contents of an arraylist.
Click Here

Member Avatar for joankim

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.