Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中读取文件并将其存储在数组中_Java_Arrays_String_Store - Fatal编程技术网

如何在java中读取文件并将其存储在数组中

如何在java中读取文件并将其存储在数组中,java,arrays,string,store,Java,Arrays,String,Store,我想读取一个文本文件并将其存储为java中的字符串多维数组 输入将如下所示 11 12 13 12 11 16 33 45 6 我想把这个储存在家里 String[][] as={{"11","12","13"}, {"12","11","16"}, {"33","45"}}; 我的代码 String file="e:\\s.txt"; try { int counterCol=0,counterRow=0;

我想读取一个文本文件并将其存储为java中的字符串多维数组

输入将如下所示

11 12 13

12 11 16

33 45 6

我想把这个储存在家里

 String[][] as={{"11","12","13"},
       {"12","11","16"},
       {"33","45"}};
我的代码

String file="e:\\s.txt";

         try
       {
       int counterCol=0,counterRow=0;

       String[][] d=null;

       BufferedReader bw=new BufferedReader(new FileReader(file));


        String str=bw.readLine();

        String[] words=str.split(",");

        System.out.println(words.length+"Counterrow");

    counterCol=words.length; //get total words split by comma : column

         while(bw.readLine()!=null)
        {

            counterRow++;    
  // to get the total words as it gives total row count
        }

         String[][] d=new String[counterRow][counterCol];
        for(int x=0;x<counterRow;x++)
        {

            for(int y=0;y<counterCol;y++)
            {

       d[x][y]=bw.readLine();  
   //storing in array. But here gives me the exception
            }

        }
String file=“e:\\s.txt”;
尝试
{
int counterCol=0,countercrow=0;
字符串[][]d=null;
BufferedReader bw=新BufferedReader(新文件读取器(文件));
字符串str=bw.readLine();
String[]words=str.split(“,”);
System.out.println(文字长度+计数器行);
counterCol=words.length;//获取按逗号分割的单词总数:列
while(bw.readLine()!=null)
{
柜台++;
//获取总字数,因为它给出了总行数
}
字符串[][]d=新字符串[计数器行][计数器列];

对于(int x=0;x,由于数组“d”为空,因此会得到null指针:

String[][] d=null;
初始化它,它应该可以工作:

String[][] d= new String [counterCol][counterRow];

这里有很多错误:

  • 数组未初始化
  • 使用
    BufferedReader
  • 您使用的是逗号而不是示例数据中指定的空格进行拆分
  • 在这方面,使用Java将对您有所帮助

    试一试这样的东西:

    String file="e:\\s.txt";
    
            try {
                int counterRow = 0;
    
                String[][] d = new String[1][1];
    
                BufferedReader bw = new BufferedReader(new FileReader(file));
    
                List<List<String>> stringListList = new ArrayList<List<String>>();
    
                String currentLine;
    
                while ((currentLine = bw.readLine()) != null) {
                    if (currentLine != null) {
                        String[] words = currentLine.split(" ");
                        stringListList.add(Arrays.asList(words));
                    }
                }
    
                // Now convert stringListList into your array if needed
                d = Arrays.copyOf(d, stringListList.size());
    
                for (List<String> stringList : stringListList) {
    
                    String[] newArray = new String[stringList.size()];
    
                    for (int i = 0; i < stringList.size(); i++) {
                        newArray[i] = stringList.get(i);
                    }
    
                    d[counterRow] = newArray;
    
                    counterRow ++;
                }
    
            } catch (Exception e) {
                // Handle exception
            }
    
    String file=“e:\\s.txt”;
    试一试{
    int计数器行=0;
    字符串[][]d=新字符串[1][1];
    BufferedReader bw=新BufferedReader(新文件读取器(文件));
    List stringListList=新建ArrayList();
    串电流线;
    而((currentLine=bw.readLine())!=null){
    if(currentLine!=null){
    String[]words=currentLine.split(“”);
    添加(Arrays.asList(words));
    }
    }
    //如果需要,现在将stringListList转换为数组
    d=Arrays.copyOf(d,stringListList.size());
    对于(列表stringList:stringListList){
    String[]newArray=新字符串[stringList.size()];
    对于(int i=0;i
    Initialize your array dd[x][y]=new String[counterCol][counterRow];是的,我已更改并初始化。但我无法存储我的值并检索回来。获取nullFYI:您的数据示例没有任何逗号。如果文件中的数据相同,那么您所拥有的将无法工作,因为您正在使用
    String[]拆分行words=str.split(“,”;