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();
        }
2
  • What if your csv has a "\n". There is an easier way to do this. stackoverflow.com/a/7779959/2219600 Commented Jul 29, 2014 at 14:06
  • My code is not seeing nextline Commented Jul 29, 2014 at 14:17

1 Answer 1

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();
                }
            }


        }
Sign up to request clarification or add additional context in comments.

11 Comments

But my code is having no issue writing first line but it doesnt write second line
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
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]
I have use , split so both csv value can be stored in an array
The problem of that solution, its you are limited to the number of lines your file will have because the length of the array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.