文件夹中的文件并具有特定的句子 [英] File in a folder and have specific sentence

查看:82
本文介绍了文件夹中的文件并具有特定的句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序,我希望它找到一个文件而不是目录然后搜索其中的特定句子并打印文件的名称

我编写了这段代码并且它有FileNotFoundException



  public   static   void  main( String  [] args)抛出FileNotFoundException {
// 此处的TODO代码应用程序逻辑
文件file = new 文件( / Users / amoona / Desktop / MyDir);
File [] matches = file.listFiles();


for int i = 0 ; i< file.length(); i ++){
扫描仪sc = 扫描仪(匹配[i] );
if (匹配[i] .isFile()== true ){
系统。 out .println( 它是一个文件);
while (sc.hasNext()){
String s = sc。下一个();

if King AbdulAziz University .equalsIgnoreCase(s)){
System。 out .println(matches [i] .getName());
}}
}
}


}





我尝试了什么:



i尝试了上面的代码,但它有

它是一个文件

线程main中的异常java.io.FileNotFoundException:/ Users / amoona / Desktop / MyDir / Dir1(是一个目录)

at java.io.FileInputStream.open0 (本机方法)

在java.io.FileInputStream.open(FileInputStream.java:195)

在java.io.FileInputStream。< init>(FileInputStream.java :138)

在java.util.Scanner。< init>(Scanner.java:611)

在lab4.Lab4.main(Lab4.java:38)

/Users/amoona/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53:Java返回:1

BUILD FAILED(总时间:0秒)

解决方案

您的代码中有错误,因此它尝试处理的条目多于从 listFiles <调用返回的条目/ code>。您还应该在创建Scanner对象之前测试该目录,因此:

  public   static   void  main( String  [] args) throws  FileNotFoundException {
// TODO代码应用程序逻辑在这里
文件文件=新文件( / Users / amoona / Desktop / MyDir);
File [] matches = file.listFiles();


for int i = 0; i if (匹配[i] .isFile()== true){
// 如果条目是目录,则跳过此代码
System.out.println( 它是一个文件);
扫描仪sc =新扫描仪(匹配[i]);
(sc.hasNext()){
String s = sc.next();

if King AbdulAziz University .equalsIgnoreCase(s)){
System.out.println(匹配[i] .getName());
}
}
}
}
}



你可以也可以使用foreach简化你的for循环,因此:

  for (File ff:matches){ 



并使用 ff 代替匹配[i]


i have this program and i want it to find a file not a directory and then search a specific sentence in it and print the name of the file
and i wrote this code and it has FileNotFoundException

public static void main(String[] args)  throws FileNotFoundException{
       // TODO code application logic here
       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()==true){
              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());
             }}
           }
       }


   }



What I have tried:

i tried the code above but it has
It Is A File
Exception in thread "main" java.io.FileNotFoundException: /Users/amoona/Desktop/MyDir/Dir1 (Is a directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at lab4.Lab4.main(Lab4.java:38)
/Users/amoona/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

解决方案

You have an error in your code, so it is trying to process more entries than are returned from the call to listFiles. You should also test for the directory before creating the Scanner object, thus:

public static void main(String[] args)  throws FileNotFoundException{
    // TODO code application logic here
    File file=new File("/Users/amoona/Desktop/MyDir");
    File[] matches = file.listFiles();
    
    
    for(int i=0;i        if (matches[i].isFile()==true){
            // skip this code if entry is a directory
            System.out.println("It Is A File");
            Scanner sc=new Scanner (matches[i]);
            while(sc.hasNext()){
                String s=sc.next();
                
                if ("King AbdulAziz University".equalsIgnoreCase(s)){
                    System.out.println(matches[i].getName());
                }
            }
        }
    }
}


You could also simplify your for loop by using a foreach, thus:

for(File ff : matches){


and use ff in place of matches[i].


这篇关于文件夹中的文件并具有特定的句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆