示例我在我的文件(记事本)中有这些数字或数组
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;
}
}
提前感谢。
我会这样做
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
}
以下代码将帮助您将文件中的所有数字保存到内存
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中的每个数组来搜索任何整数,如下所示:
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"));
要读取文本文件的所有行,可以这样做:
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 + " ";
}
}
}