Android CSV文件上传到阵列

Android CSV文件上传到阵列,android,csv,Android,Csv,我已经创建了这个代码,但不知道为什么它不工作。代码不会打印csvfile的所有行 try{ File csvfile = new File(FullPath); FileInputStream csvStream = new FileInputStream(csvfile); BufferedReader in = new BufferedReader(new InputStreamReader(csvStr

我已经创建了这个代码,但不知道为什么它不工作。代码不会打印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();
        }

如果问题是没有打印文件的所有行,则覆盖了循环上的数组。您应该编辑您的问题,因为数组正在工作,真正的问题是文件没有打印所有行

以下是名称、数字格式的解决方案:

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


    }
import java.io.BufferedInputStream;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
公共类测试{
公共静态void main(字符串[]args)引发IOException{
Teste x=新的Teste();
x、 doTeste();
}
public void doTeste()引发IOException{
String fileName=“D:\\Projetos\\Testes\\src\\teste.txt”;
BufferedReader in=null;
试一试{
文件csvfile=新文件(文件名);
FileInputStream csvStream=新的FileInputStream(csvfile);
in=新的BufferedReader(新的InputStreamReader(csvStream));
弦线;
String[]rowName=新字符串[(countLines(文件名)+1];
String[]rowNameData=新字符串[(countLines(文件名)+1];
int-linha=0;
而((line=in.readLine())!=null){
String[]数组=line.split(“,”);
rowName[linha]=数组[0];
rowNameData[linha]=数组[1];
++林哈;
}
System.out.println(“行名数组”);
for(字符串s:rowName){
系统输出打印项次;
}
System.out.println(“rowNameData数组”);
用于(字符串s:rowNameData){
系统输出打印项次;
}
}
捕获(例外e)
{
e、 printStackTrace();
}
最后{
in.close();
}
}
公共静态int countLines(字符串文件名)引发IOException{
InputStream is=new BufferedInputStream(new FileInputStream(filename));
试一试{
字节[]c=新字节[1024];
整数计数=0;
int readChars=0;
布尔空=真;
而((readChars=is.read(c))!=-1){
空=假;
for(int i=0;i
一行中的名称和下一行的编号:

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


        }
import java.io.BufferedInputStream;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
公共类测试{
公共静态void main(字符串[]args)引发IOException{
Teste x=新的Teste();
x、 doTeste();
}
public void doTeste()引发IOException{
String fileName=“D:\\Projetos\\Testes\\src\\teste.txt”;
BufferedReader in=null;
试一试{
文件csvfile=新文件(文件名);
FileInputStream csvStream=新的FileInputStream(csvfile);
in=新的BufferedReader(新的InputStreamReader(csvStream));
弦线;
String[]rowName=新字符串[(countLines(文件名)+1)/2];
String[]rowNameData=新字符串[(countLines(文件名)+1)/2];
int-iCount=0;
int x=0;
int y=0;
而((line=in.readLine())!=null){
如果(iCount==0 | | iCount%2==0){
rowName[x]=行;
++x;
}
否则{
rowNameData[y]=行;
++y;
}
iCount++;
}
System.out.println(“行名数组”);
for(字符串s:rowName){
系统输出打印项次;
}
System.out.println(“rowNameData数组”);
用于(字符串s:rowNameData){
系统输出打印项次;
}
}
捕获(例外e)
{
e、 printStackTrace();
}
最后{
in.close();
}
}
公共静态int countLines(字符串文件名)引发IOException{
InputStream is=new BufferedInputStream(new FileInputStream(filename));
试一试{
字节[]c=新字节[1024];
整数计数=0;
int readChars=0;
布尔空=真;
而((readChars=is.read(c))!=-1){
空=假;
for(int i=0;i
如果您的csv