我试图从文本文件中读取某些数字。几天前它正在工作,现在突然间它没有读出某些字后的数字。这是我的java函数,用于写和读。注意这是在JSP中使用的:
public void writeToFile() {
try {
File aFile = new File(nameOfFile + "MAIN" + ".txt");
aFile.createNewFile();
PrintWriter writeTo = new PrintWriter(aFile);
writeTo.print("Matrix 1" + "\nRows: " + row1 + "\n");
writeTo.print("Columns: " + column1 + "\n");
writeTo.println("Method used: " + operationName);
if("DotProduct".equals(operationName))
writeTo.println("Vector: " + vectorRow1);
writeTo.println();
for(int i = 0; i < getRow1(); i++) {
int counter = 0;
for(int j = 0; j < getCol1(); j++, counter++)
writeTo.print(matrixToFile[i][j] + "\t");
if(counter == getCol1()) {
writeTo.print("\n");
}
}
writeTo.close();
File aFile2 = new File(nameOfFile + "SECOND.txt");
aFile2.createNewFile();
writeTo = new PrintWriter(aFile2);
writeTo.print("Matrix 2" + "\nRows: " + row2 + "\n");
writeTo.print("Columns: " + column2 + "\n");
if("DotProduct".equals(operationName))
writeTo.println("Vector: " + vectorCol2);
writeTo.println();
for(int i = 0; i < getRow2(); i++) {
int counter = 0;
for(int j = 0; j < getCol2(); j++, counter++)
writeTo.print(matrix2ToFile[i][j] + "\t");
if(counter == getCol2()) {
writeTo.print("\n");
}
}
writeTo.close();
File aFile3 = new File(nameOfFile + "RESULT.txt");
aFile3.createNewFile();
writeTo = new PrintWriter(aFile3);
writeTo.print("Result" + "\nRows: " + row3 + "\n");
writeTo.print("Columns: " + column3 + "\n");
writeTo.println();
if("DotProduct".equals(operationName))
writeTo.println("Result:" + resultVector);
else {
for(int i = 0; i < getRow3(); i++) {
int counter = 0;
for(int j = 0; j < getCol3(); j++, counter++)
writeTo.print(result[i][j] + "\t");
if(counter == getCol3()) {
writeTo.print("\n");
}
}
}
writeTo.close();
nameOfFile = "/var/lib/tomcat7/webapps/Matrices/WEB-INF/MatrixData/";
errors.put("dataStored", "Your results have been stored!");
} catch(IOException ex) {
System.out.printf("Error!");
}
}
这是读取文件:
public void readFromFile(String resultOrNot, String fileName) {
nameOfFile = "/var/lib/tomcat7/webapps/Matrices/WEB-INF/MatrixData/";
workMe = fileName;
try {
Scanner readFile = new Scanner(new File(nameOfFile + workMe));
readFile.useDelimiter("Rows: ");
while(readFile.hasNextInt()) {
stringRow = readFile.next();
}
setRow1VIAString(stringRow);
readFile.useDelimiter("Columns: ");
while(readFile.hasNextInt()) {
stringCol = readFile.next();
}
setColumn1VIAString(stringCol);
readFile.useDelimiter("Method used: ");
while(readFile.hasNext()) {
operationName = readFile.next();
}
readFile.useDelimiter("\n");
for(int i = 0; i < row1 || readFile.hasNextDouble(); i++)
for(int j = 0; j < column1; j++)
matrix1[i][j] = readFile.nextDouble();
readFile.close();
workMe = workMe.replace("MAIN", "SECOND");
readFile = new Scanner(new File(nameOfFile + workMe));
readFile.useDelimiter("Rows: ");
while(readFile.hasNextInt()) {
stringRow = readFile.next();
}
setRow2VIAString(stringRow);
readFile.useDelimiter("Columns: ");
while(readFile.hasNextInt()) {
stringCol = readFile.next();
}
setColumn2VIAString(stringCol);
readFile.useDelimiter("\n");
for(int i = 0; i < row1 || readFile.hasNextDouble(); i++)
for(int j = 0; j < column1; j++)
matrix1[i][j] = readFile.nextDouble();
readFile.close();
if("giveMeResult".equals(resultOrNot)) {
workMe = workMe.replace("SECOND", "RESULT");
readFile = new Scanner(new File(nameOfFile + workMe));
readFile.useDelimiter("Rows: ");
while(readFile.hasNextInt()) {
stringRow = readFile.next();
}
setRow3VIAString(stringRow);
readFile.useDelimiter("Columns: ");
while(readFile.hasNextInt()) {
stringCol = readFile.next();
}
setColumn3VIAString(stringCol);
readFile.useDelimiter("\n");
for(int i = 0; i < row3 || readFile.hasNextDouble(); i++)
for(int j = 0; j < column3; j++)
matrix1[i][j] = readFile.nextDouble();
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
转换功能:
public void setRow1VIAString(String aRow) {
row1 = Integer.parseInt(aRow);
}
这就是错误:
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:454)
java.lang.Integer.parseInt(Integer.java:527)
matrixcalculator.MatrixCalculator.setRow1VIAString(MatrixCalculator.java:171)
matrixcalculator.MatrixCalculator.readFromFile(MatrixCalculator.java:728)
org.apache.jsp.choosing_jsp._jspService(choosing_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
我知道这意味着尝试解析的字符串是null,我不明白为什么现在它为null?它一直工作正常,现在突然这样做了。我知道这些空白,但我把它们计算在内,以免出现任何问题。