我使用下面的代码创建了一个名为Test.txt的.txt文件,但它确实有效。现在我希望java将信息读回给我,当我收到错误INBETWEEN两个代码段时。我只是想写一个文件,保存它,并能够读取它,这样我就可以创建一个小型数据库。 谢谢!
package textFile;
import java.io.*;
import java.lang.*;
import java.util.*;
public class createFile {
private Formatter x;
public void openfile(){
try{
x = new Formatter("Test.txt");
}
catch(Exception e) {
System.out.println("you have and error");
}
}
public void addRecords(){
x.format("%s%s%s", "Coty", "xcallidus", "Password");
}
public void closefile(){
x.close();
}
}
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at readToMe.read.readFile(read.java:21)
at readToMe.Main.main(Main.java:11)
package readToMe;
import java.lang.*;
import java.io.*;
import java.util.*;
public class read {
private Scanner x;
public void openFile(){
try{
x = new Scanner(new File("Test.txt"));
}
catch(Exception e){
System.out.println("Could not find file");
}
}
public void readFile(){
while(x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();
System.out.printf("%s %s %s", a,b,c);
}
}
public void closeFile(){
x.close();
}
}
答案 0 :(得分:0)
问题在于
public void addRecords(){
x.format("%s%s%s", "Coty", "xcallidus", "Password");
//This creates CotyxcallidusPassword - Without spaces
}
您正在添加三个没有空格的字符串,并尝试使用空格读取它们。它应该是
public void addRecords(){
x.format("%s %s %s", "Coty", "xcallidus", "Password");
}