我有这个程序,我希望它找到一个文件而不是目录,然后搜索其中的特定句子并打印文件的名称 我写了这段代码,它有FileNotFoundException
public static void main(String[] args) throws FileNotFoundException {
File file=new File("/Users/amoona/Desktop/MyDir");
File[] matches = file.listFiles();
for (int i=0;i<file.length();i++) {
Scanner sc=new Scanner (matches[i]);
if (matches[i].isFile()) {
System.out.println("It Is A File");
while (sc.hasNext()) {
String s = sc.next();
if ("King AbdulAziz University".equalsIgnoreCase(s)) {
System.out.println(matches[i].getName());
}
}
}
}
}
////////////
我将代码更改为此,但问题是它不会打印具有特定句子的文件名
文件文件=新文件(&#34; / Users / amoona / Desktop / MyDir&#34;); File [] fList = file.listFiles();
for(File ff : fList){
if (ff.isDirectory()!=true){
Scanner sc=new Scanner (ff);
System.out.println("It Is A File");
System.out.println(ff.getName());
while(sc.hasNext()){
String s=sc.nextLine();
if (s.equalsIgnoreCase("King AbdulAziz University")){
System.out.println("The File That Has King AbdulAziz University");
System.out.println(ff.getName());
}}
}
}
答案 0 :(得分:1)
在for循环中,当您使用#radiobuttonlist label
{
vertical-align: super;
}
时,您正在使用var omzetteller = document.getElementById('omzet');
var omzet = 100;
omzetteller.innerHTML = omzet;
var afbeeldingArray = ['1.png', '2.png', '3.png', '4.png', '5.png'];
var afbeeldingen = document.querySelectorAll('img');
function randomPlaatje() {
var image = Math.floor((Math.random() * afbeeldingArray.length));
var afb = "img/" + afbeeldingArray[image];
return afb;
}
function krijgInzet() {
var inzet = document.getElementById('inzet').value;
return inzet;
}
var i;
for (i = 0; i < afbeeldingen.length; i++) {
afbeeldingen[i].src = randomPlaatje();
}
function berekenOmzet(afb1, afb2, afb3){
if(afb1 == afb2 && afb2 == afb3 && afb1 == afb3) { omzet += (krijgInzet() * 10); }
else { omzet -= krijgInzet(); }
}
function gebruikPlaatje(){
var afb1 = randomPlaatje();
var afb2 = randomPlaatje();
var afb3 = randomPlaatje();
document.getElementById('img1').src = afb1;
document.getElementById('img2').src = afb2;
document.getElementById('img3').src = afb3;
setTimeout(function() {
berekenOmzet(afb1, afb2, afb3);
omzetteller.innerHTML = omzet;
}, 1000); // 1000 = 1 seconde
setInterval(function() {
randomPlaatje();
clearInterval();
}, 1000); // 1000 = 1 seconde
}
function start(){
setTimeout(function() {
gebruikPlaatje();
}, 1000); // 1000 = 1 seconde
}
var spin = document.getElementById('spin');
var reset = document.getElementById('reset');
spin.addEventListener('click', start);
reset.addEventListener('click', function() {
omzet = 100;
omzetteller.innerHTML = omzet;
});
。
答案 1 :(得分:0)
您的方法存在以下几个问题:
1)正如其他人所说,你的for循环正在使用file.length()
,它应该是matches.length
- 匹配是一个数组 - 你不需要()
2)创建Scanner
以访问文件应放在if(subFile.isFile())
检查之后。您不想在目录上打开扫描仪,或者“拒绝访问”&#39;错误是可能的,也可能是。
3)在你的if条件:if (matches[i].isFile()==true)
中,你不需要&#39; == true&#39;部分。虽然这在语法上是正确的,但它是不必要的,因为isFile()
返回一个布尔值,并且在if检查中只需要它。
以下是您的方法,稍加修改以包含以上几点:
public static void main(String[] args) throws FileNotFoundException{
File file=new File("/Users/amoona/Desktop/MyDir");
File[] matches = file.listFiles();
for(int i=0;i<matches.length;i++){
File subFile = matches[i];
if (subFile.isFile()){
System.out.println("Checking the file: " + subFile.getPath());
Scanner sc=new Scanner (subFile);
while(sc.hasNext()){
String s=sc.nextLine();
if ("King AbdulAziz University".equalsIgnoreCase(s)){
System.out.println("=====>> Found a match! " + subFile.getPath());
}
}
}
}
}