使用 FileReader 测试文件是否为空
Test if file is blank using FileReader
我正在尝试将 else if
添加到我的 while 循环内的代码块,下面测试 LandingsData.txt
文件是否为空。如果为空,则打印"Starting with an empty system"
FileReader fr = null;
try {
fr = new FileReader("LandingsData.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
Scanner myFile = new Scanner(fr);
String line;
myFile.useDelimiter(",|\n");
while (myFile.hasNext()) {
line = myFile.next();
if (line.equals("sl")) {
StrongLanding sl = new StrongLanding();
sl.setLandingId(Integer.parseInt(myFile.next()));
sl.setLandingDesc(myFile.next());
sl.setNumLandings(Integer.parseInt(myFile.next()));
sl.setCost(Double.parseDouble(myFile.next()));
landings.add(sl);
} else if (line.equals("ul")) {
UltimateLanding ul = new UltimateLanding();
ul.setLandingId(Integer.parseInt(myFile.next()));
ul.setLandingDesc(myFile.next());
ul.setNumLandings(Integer.parseInt(myFile.next()));
ul.setCost(Double.parseDouble(myFile.next()));
landings.add(ul);
} else if (fr.length() == 0) {
System.out.println("Starting with an empty system");
}
}
我使用 fr.length()
或 fr.count()
尝试了几种不同的方法,但我似乎收到错误 The method length() is undefined for the type FileReader
我还在 while 循环之外尝试了几个不同的地方,因为我认为这可能是问题所在,但没有成功
TIA
Scanner myFile = new Scanner(fr);
String line;
myFile.useDelimiter(",|\n");
if (!myFile.hasNext()){
System.out.println("Starting with an empty system");
} else {
while (myFile.hasNext()) {
line = myFile.next();
if (line.equals("sl")) {
// Do stuff
} else if (line.equals("ul")) {
// Do stuff
}
}
}
读取文件前调用hasNext()
一次,查看文件是否为空。
您可以使用以下代码检查文件是否为空:
File myFile = new file("file.txt");
if(myFile.isEmpty())
{
System.out.println("Starting with an empty system");
}
使用 File
class 而不是 FileReader
。
或者如果你需要使用FileReader
,你可以这样做:
// Checking if the first line of the file is empty
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
if (br.readLine() == null)
{
System.out.println("No errors, and file empty");
}
并在开始 while 循环之前进行检查,将第一行与两个字符串进行比较然后检查文件是否为空...
我正在尝试将 else if
添加到我的 while 循环内的代码块,下面测试 LandingsData.txt
文件是否为空。如果为空,则打印"Starting with an empty system"
FileReader fr = null;
try {
fr = new FileReader("LandingsData.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
Scanner myFile = new Scanner(fr);
String line;
myFile.useDelimiter(",|\n");
while (myFile.hasNext()) {
line = myFile.next();
if (line.equals("sl")) {
StrongLanding sl = new StrongLanding();
sl.setLandingId(Integer.parseInt(myFile.next()));
sl.setLandingDesc(myFile.next());
sl.setNumLandings(Integer.parseInt(myFile.next()));
sl.setCost(Double.parseDouble(myFile.next()));
landings.add(sl);
} else if (line.equals("ul")) {
UltimateLanding ul = new UltimateLanding();
ul.setLandingId(Integer.parseInt(myFile.next()));
ul.setLandingDesc(myFile.next());
ul.setNumLandings(Integer.parseInt(myFile.next()));
ul.setCost(Double.parseDouble(myFile.next()));
landings.add(ul);
} else if (fr.length() == 0) {
System.out.println("Starting with an empty system");
}
}
我使用 fr.length()
或 fr.count()
尝试了几种不同的方法,但我似乎收到错误 The method length() is undefined for the type FileReader
我还在 while 循环之外尝试了几个不同的地方,因为我认为这可能是问题所在,但没有成功
TIA
Scanner myFile = new Scanner(fr);
String line;
myFile.useDelimiter(",|\n");
if (!myFile.hasNext()){
System.out.println("Starting with an empty system");
} else {
while (myFile.hasNext()) {
line = myFile.next();
if (line.equals("sl")) {
// Do stuff
} else if (line.equals("ul")) {
// Do stuff
}
}
}
读取文件前调用hasNext()
一次,查看文件是否为空。
您可以使用以下代码检查文件是否为空:
File myFile = new file("file.txt");
if(myFile.isEmpty())
{
System.out.println("Starting with an empty system");
}
使用 File
class 而不是 FileReader
。
或者如果你需要使用FileReader
,你可以这样做:
// Checking if the first line of the file is empty
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
if (br.readLine() == null)
{
System.out.println("No errors, and file empty");
}
并在开始 while 循环之前进行检查,将第一行与两个字符串进行比较然后检查文件是否为空...