Java的。读取文件中的2维数组(记事本)

时间:2014-07-21 16:11:25

标签: java arrays netbeans

示例我的文件(记事本)上有这个数字或数组

2 3 4 5 7 2 6 2

2 4 6 8 9 4 8 1

我想问一下如何阅读下一行。我只能使用此代码读取第一行。

  

    String path = "/path/notepad.txt";

    String stringOfNumbers[];

    BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    BufferedReader br2 = new BufferedReader (new FileReader(path));

    String lineOfNumbers = br2.readLine();

    stringOfNumbers = lineOfNumbers.split(" ");
  //stringOfNumbers = lineOfNumbers.split("\n");
    String str = lineOfNumbers.replace(","," ");
    System.out.println(str);
    System.out.print("");


    int numbers[][] = new int [stringOfNumbers.length][stringOfNumbers.length];

    for (int i = 0; i < numbers.length; i++)
    {
        numbers[i][i] = Integer.parseInt(stringOfNumbers[i]);
    }

    System.out.print("Enter the number to search:   ");
    int searchNumber = Integer.parseInt(br.readLine());

    int location = 0;

    for (int i = 0; i < numbers.length; i++)
    {
        if (numbers[i][i] == searchNumber)
        {
            location = i+ 1;
        }
    }

提前谢谢。

3 个答案:

答案 0 :(得分:0)

我会做这样的事情

FileReader fr = new FileReader("myFileName");
BufferedReader br = new BufferedReader(fr);

while((line=br.readLine())!=null) // as  long as there are lines in the file
{
  stringOfNumbers = line.split(" ");
// other code
}

答案 1 :(得分:0)

要阅读文本文件的所有行,您可以执行以下操作:

    String path = "/path/notepad.txt";

    String stringOfNumbers[];

    BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    BufferedReader br2 = new BufferedReader (new FileReader(path));

    ArrayList<String> listOfLines = new ArrayList<String>();
    //String lineOfNumbers = "";
    String line = "";
    String allIndexes = "";

    while ((line = br2.readLine()) != null) {
        if(!line.isEmpty()){
            listOfLines.add(line);
        }
    }

    for(String lineOfNumbers : listOfLines){
        stringOfNumbers = lineOfNumbers.split(" ");
          //stringOfNumbers = lineOfNumbers.split("\n");
        String str = lineOfNumbers.replace(","," ");
        System.out.println(str);
        System.out.print("");

        int numbers[][] = new int [stringOfNumbers.length][listOfLines.size()];

        for (int i = 0; i < numbers.length; i++)
        {
            numbers[i][listOfLines.indexOf(lineOfNumbers)] = Integer.parseInt(stringOfNumbers[i]);
        }

        System.out.print("Enter the number to search:   ");
        int searchNumber = Integer.parseInt(br.readLine());

        int locationI = 0;
        int locationJ = 0;

        for (int i = 0; i < numbers.length; i++)
        {
            for(int j = 0; j < listOfLines.size(); j++)
                if (numbers[i][j] == searchNumber)
                {
                    locationI = i + 1;
                    locationJ = j + 1;
                    allIndexes += locationI + ":" + locationJ + " ";
                }
        }
    }

答案 2 :(得分:0)

以下代码将帮助您将文件中的所有数字保存到内存

Scanner scanner = new Scanner(path);

List<Integer[]> integerArList = new ArrayList<Integer[]>();

while(scanner.hasNextLine()){
String lineOfNumbers = scanner.nextLine();


        stringOfNumbers = lineOfNumbers.split(" ");
        //stringOfNumbers = lineOfNumbers.split("\n");
        String str = lineOfNumbers.replace(","," ");
        System.out.println(str);
        System.out.print("");


    Integer[] numbers = new Integer[stringOfNumbers.length];

    for (int i = 0; i < numbers.length; i++)
    {
        numbers[i] = Integer.parseInt(stringOfNumbers[i]);
    }
    integerArList.add(numbers);
}

在此之后,您可以通过遍历List中的每个数组来搜索任何Integer:

int searchMe = <get this from user>
int location=0;
boolean found=false;
for(Integer[] intAr: integerArList){
    for(int i=0;i<intAr.length;i++){
        if(intAr[i]==searchMe){
            location+=(i+1)
            found=true;
            break;
        }

    }

    if(found) break;
    location+=intAr.length;

}

System.out.println("Location of " + searchMe +" : " +(found?location:"Not Found in Data"));

希望这有帮助。