繁体   English   中英

java读取文件错误

[英]java read in a file error

我需要从.txt文件读入。 我在驱动程序类上有一个try-catch工具,但是似乎无法从保存在计算机上的文本文件中读取它。 有人可以指出我正确的方向吗?

这是我的驱动程序类:

import java.util.Scanner;
import java.io.*;
public class DenseScrabbleTester {
  public static void main(String args[]) throws IOException {
   String fileName;
   Scanner nameReader = new Scanner(System.in);
   int x = 1;
   do{
   try{
   System.out.println("Enter a file name");
   fileName = nameReader.nextLine();
   DenseScrabble e = new DenseScrabble(fileName);
   e.readLines();
   e.results();
   x=2;
  }
   catch(Exception e){
     System.out.println("The file does not exist");
   }
  }
   while(x==1);
}
}

这是DenseScrabble类:

import java.io.*;
public class DenseScrabble extends Echo{
  double max = 0.0;
  String bestWord = "";
  int lineNumer = 0;
  public DenseScrabble(String f) throws IOException {
    super(f);
  }
//scrabbles
int[] scrabbles = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
//process the given line
public void processLine(String s) {
  s.toLowerCase();
  int score = 0;
  for(int i = 0; i<s.length(); i++){
    char ch = s.charAt(i);
    if(Character.isLetter(ch)){
      int pos = ch - 'a';
      score += scrabbles[pos];
      }
    }
  if(score > max){
    max = score;
    bestWord = s;
  }
}
//displays the winner and score
public void results() {
System.out.println("Winner: " + bestWord);
System.out.println("score: " + max/bestWord.length());
}
}

这是Echo类:

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

  public class Echo{
    String fileName; // external file name
    Scanner scan; // Scanner object for reading from external file

    public Echo(String f) throws IOException
    {
     fileName = f;
     scan = new Scanner(new FileReader(fileName));
   }

   public void readLines(){ // reads lines, hands each to processLine
     while(scan.hasNext()){
       processLine(scan.nextLine());
     }
     scan.close();
   }

   public void processLine(String line){ // does the real processing work
    System.out.println(line);
   }
 }

这是我得到的例外:

java.io.FileNotFoundException: sampletext.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at Echo.<init>(Echo.java:11)
    at DenseScrabble.<init>(DenseScrabble.java:7)
    at DenseScrabbleTester.main(DenseScrabbleTester.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
String fileName;

这里的文件名必须是这样的

C:/Folder/sample.txt

但是,如果您将文件完全保存在班级所在的同一文件夹中,而不仅仅是sample.txt

您也可以使用文件类来创建文件。

File file=new File("sample.txt");

它将在您的课程所在的目录中创建文件。

确保您正在通过。

问题中的另一件事是您要从simple.txt阅读的simple.txt

而例外是说

sampletext.txt (The system cannot find the....

请确保文件名是。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM