0

I have created this code but don't know why its not working. The code doesn't print all lines of the csvfile.

        try{
            File csvfile = new File(FullPath);
            FileInputStream csvStream = new FileInputStream(csvfile);
            BufferedReader in = new BufferedReader(new InputStreamReader(csvStream));
            String line;

            int iCount=0;
                    while ((line = in.readLine()) != null){
                        String[] RowData = line.split(",");
                        name[iCount] = RowData[0];
                        Toast.makeText(NewMessage.this, "CSV", 2000).show();
                        number[iCount] = RowData[1];
                        iCount++;

        }
                    in.close();
                    Toast.makeText(NewMessage.this, "CSV Has uploaded", 2000).show();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
Kirtesh
  • 29
  • 1
  • 10

1 Answers1

0

If the problem is that is not printing all lines of the file, you are overriding the array on loop. You should edit your question, because the array is working, the really problem is that the file wasn't printing all lines.

Here's the solution for name,number format:

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    public class Teste {

        public static void main(String[] args) throws IOException {
            Teste x = new Teste();
            x.doTeste();
        }




        public void doTeste() throws IOException{
            String fileName = "D:\\Projetos\\Testes\\src\\teste.txt"; 
            BufferedReader in = null;
            try{

                File csvfile = new File(fileName);
                FileInputStream csvStream = new FileInputStream(csvfile);
                in = new BufferedReader(new InputStreamReader(csvStream));
                String line;
                String[] rowName = new String[(countLines(fileName)+1)];
                String[] rowNameData = new String[(countLines(fileName)+1)];

                int linha = 0;


                while ((line = in.readLine()) != null){
                    String[] array = line.split(",");
                    rowName[linha] = array[0];
                    rowNameData[linha] = array[1];
                    ++linha;
                }

                System.out.println("The rowName ARRAY");
                for (String s: rowName){
                    System.out.println(s);
                }

                System.out.println("The rowNameData ARRAY");
                for (String s: rowNameData){
                    System.out.println(s);
                }

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally {
                in.close();
            }
        }

        public static int countLines(String filename) throws IOException {
            InputStream is = new BufferedInputStream(new FileInputStream(filename));
            try {
                byte[] c = new byte[1024];
                int count = 0;
                int readChars = 0;
                boolean empty = true;
                while ((readChars = is.read(c)) != -1) {
                    empty = false;
                    for (int i = 0; i < readChars; ++i) {
                        if (c[i] == '\n') {
                            ++count;
                        }
                    }
                }
                return (count == 0 && !empty) ? 1 : count;
            } finally {
                is.close();
            }
        }


    }

and for name in one line, and number the next line:

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    public class Teste {

            public static void main(String[] args) throws IOException {
                Teste x = new Teste();
                x.doTeste();
            }




            public void doTeste() throws IOException{
                String fileName = "D:\\Projetos\\Testes\\src\\teste.txt"; 
                BufferedReader in = null;
                try{

                    File csvfile = new File(fileName);
                    FileInputStream csvStream = new FileInputStream(csvfile);
                    in = new BufferedReader(new InputStreamReader(csvStream));
                    String line;
                    String[] rowName = new String[(countLines(fileName)+1)/2];
                    String[] rowNameData = new String[(countLines(fileName)+1)/2];

                    int iCount=0;
                    int x = 0;
                    int y = 0;


                    while ((line = in.readLine()) != null){
                        if (iCount == 0 || iCount%2 == 0) {
                        rowName[x] = line;
                        ++x;
                        }
                        else {
                            rowNameData[y] = line; 
                            ++y;
                        }
                        iCount++;

                    }

                    System.out.println("The rowName ARRAY");
                    for (String s: rowName){
                        System.out.println(s);
                    }

                    System.out.println("The rowNameData ARRAY");
                    for (String s: rowNameData){
                        System.out.println(s);
                    }

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                finally {
                    in.close();
                }
            }

            public static int countLines(String filename) throws IOException {
                InputStream is = new BufferedInputStream(new FileInputStream(filename));
                try {
                    byte[] c = new byte[1024];
                    int count = 0;
                    int readChars = 0;
                    boolean empty = true;
                    while ((readChars = is.read(c)) != -1) {
                        empty = false;
                        for (int i = 0; i < readChars; ++i) {
                            if (c[i] == '\n') {
                                ++count;
                            }
                        }
                    }
                    return (count == 0 && !empty) ? 1 : count;
                } finally {
                    is.close();
                }
            }


        }
  • But my code is having no issue writing first line but it doesnt write second line – Kirtesh Jul 29 '14 at 14:23
  • Now its printing all the lines. You should edit you question because you are saying that your array is not working and its working. The problem is that your file isn't printing all lines –  Jul 29 '14 at 14:40
  • But i want to store all data in an array there are 2 row so there will be 2 array. So where should i use a[icount]=rowData[0] b[icount]=rowData[1] – Kirtesh Jul 29 '14 at 14:47
  • I have use , split so both csv value can be stored in an array – Kirtesh Jul 29 '14 at 14:53
  • The problem of that solution, its you are limited to the number of lines your file will have because the length of the array –  Jul 29 '14 at 15:09
  • Mine array might contain more than 1000 values – Kirtesh Jul 29 '14 at 15:29
  • the maximum lines is 2.147.483.642 or the size of your heap. Probably you don't exceed. –  Jul 29 '14 at 15:32
  • But dude I m not getting in which two arrays my 1st and 2nd values are storing – Kirtesh Jul 29 '14 at 15:40
  • Your question is about your file that doesn't print all lines. You must take the solution and readjust to what you want. –  Jul 29 '14 at 15:47
  • Ok here is my clear description what i exactly want is I have CSV file it have two values like kirtesh,123456789 and i want to store name[icount] = 1st row of the CSV i.e. Kirtesh number[icount] = 2nd row of the CSV i.e. 123456789 – Kirtesh Jul 29 '14 at 15:50
  • here you are. the full code. I think is that what you mean all the time. –  Jul 29 '14 at 18:03