2013-05-28 175 views
0

让我先确立一个事实:我是java的新手,所以请耐心等待。Java扫描仪从文件中读取

在我的编程课上,我们接受了一个在家里使用Scanner类的练习。活动说明如下编码行使有:

import java.io.*; 
import java.util.*; 

public class FileReadWrite { 

    public static void main(String[] args) throws FileNotFoundException { 
      String[] strArr = new String[100]; 

      int size = 0; 

      try { 
       Scanner scFile = new Scanner(new File("Names1.txt")); 
       while (scFile.hasNext()) { 
        strArr[size] = scFile.next(); 
        size++; 
       } 
       scFile.close(); 

       for (int i = 0; i < size; i++) { 
        System.out.print(strArr[i] + " "); 
       } 
      } catch (FileNotFoundException e) { 
       System.err.println("FileNotFoundException: " + e.getMessage()); 
      } 

    } 
} 

程序似乎不能工作正确。我使用NetBeans来运行代码,当我运行代码时,它不会在文本文件Names.txt中显示数据。这是为什么?然而,该程序确实完全没有错误。

我尝试过扫描仪类javadocs,但它没有帮助我。

请向我解释,以便我可以从所犯的错误中学习。

感谢, 约翰

+0

是否有错误,或者......? –

+0

请详细说明“似乎不工作”。 –

+0

@kbbucks新的String()如何提供帮助? –

回答

-1

这里的工作的例子。也许尝试使用BufferedReader。

import java.io.*; 
import java.util.Scanner; 

public class ScanXan { 
public static void main(String[] args) throws IOException { 

    Scanner s = null; 

    try { 
     s = new Scanner(new BufferedReader(new FileReader("xanadu.txt"))); 

     while (s.hasNext()) { 
      System.out.println(s.next()); 
     } 
    } finally { 
     if (s != null) { 
      s.close(); 
     } 
    } 
} 
} 

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

+0

您的实际工作! Hooraaaay!谢谢! –

0

我想在我的Mac你的代码,它的工作原理。所以我想你可能会输入一个Names1.txt文件的错误路径。

在Java中,当您只使用“what_ever_file_name.txt”作为文件路径时,Java将只搜索源代码文件夹中的文件。如果没有发现,则会引发“FILE_NOT_FOUND_EXCEPTION”。

+0

它肯定会找到该文件,因为它不会抛出“FileNotFound异常”。我已将该文件放在src文件夹中,以便它可以轻松找到信息。 –

0

我同意user2170674。我也使用Eclipse在Windows机器上试过你的代码,并且一切都很顺利。也许你把你的文件放在错误的路径上。两个选项:

  • 您可以使用完整路径,如(如果您使用Windows)“C:\ Names1.txt”;
  • ,或者更通用的解决方案,使用的JFileChooser:

     // create your FileChooser 
         final JFileChooser chooser = new JFileChooser(); 
         // open the FileChooser 
         int returnValue = chooser.showOpenDialog(null); 
    
         // if you select a file 
         if (returnValue == JFileChooser.APPROVE_OPTION) { 
          // get the file and do what you need 
          File file = chooser.getSelectedFile(); 
    
         } else { 
          // throw an exception or just a message in the log... 
         } 
    
+0

我会给JFileChooser一个尝试。不知道我是否可以在考试中使用它。我已经改变了路径,但仍然没有给我文件中的内容。文本文件中的数据可能是错误的?它应该是什么样子?我用我的愚蠢来创建自己的文件,因为他们没有给我们一个。 –

+0

嗨琼, 有一些替代方案来调查问题: *您是否尝试debbug代码,看看发生了什么,一行一行? *控制台上打印什么? 关于文件中的数据是错误的:我给你建议使用JFileChooser只是为了保证你正在得到正确的文件。该文件的内容应该如何?我想,只是文本,每行结尾都有返回字符。 –

0

您的代码看起来不错。
使用几条消息进行调试。
最后,添加一个System.out.println()System.out.flush()
将异常块位置移至文件使用后(尽量减少块大小)并在finally块内移动close()。
请务必查看Netbeans的输出窗口(窗口 - >输出 - >输出)

public class FileReadWrite { 

public static void main(String[] args) throws FileNotFoundException { 
     System.out.println("##### Starting main method..."); 
     String[] strArr = new String[100]; 

     int size = 0; 

     try { 
      Scanner scFile = new Scanner(new File("Names1.txt")); 
      while (scFile.hasNext()) { 
       strArr[size] = scFile.next(); 
       size++; 
      } 
      System.out.println("##### Finished scan. Found %d tokens.", size); 
     } catch (FileNotFoundException e) { 
      System.err.println("FileNotFoundException: " + e.getMessage()); 
     } catch (Exception e) { 
      System.err.println("Exception: " + e.getMessage()); 
      e.printStackTrace(); 
     } finally { 
      if (scFile != null) { 
       scFile.close(); 
       System.out.println("##### Closed scanner."); 
      } 
     } 
     System.out.println("##### Printing tokens..."); 

     for (int i = 0; i < size; i++) { 
      System.out.print(strArr[i] + " "); 
     } 
     System.out.println(""); 
     System.out.println("##### Exiting main."); 
} 
} 
+0

感谢编写所有这些问题!看起来虽然编码有错误。当我尝试运行它时,我在NetBeans中遇到了三个初始错误。 1)Line15:System.out.println ..... ERR:找不到适合println(String,int)的方法。 2)Line22:scFile!= 0 ..... ERR:找不到符号。 3)Line23:scFile.close()..... ERR:找不到符号 –

+0

因此,在对编码进行微小调整后,我已经运行了,但它仍然给我提到了上面提到的问题,它没有显示txt文件中的内容文件。它遍历所有代码,在代码中打印不断,但是当它必须打印令牌时,它会创建一个新的空白行,然后继续退出main。 –

+0

试试这个打印令牌:System.out.print(“<”+ strArr [i] +“>,”);是否打印大量空的标记?:<>,<>,<>, –