简体   繁体   English

java读取文件错误

[英]java read in a file error

I need to read in from a .txt file. 我需要从.txt文件读入。 I have a try-catch harness on the driver class, but I can't seem to get it to read in a text file that is saved on my computer. 我在驱动程序类上有一个try-catch工具,但是似乎无法从保存在计算机上的文本文件中读取它。 Could someone point me in the right direction? 有人可以指出我正确的方向吗?

Here is my driver class: 这是我的驱动程序类:

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);
}
}

here is the DenseScrabble class: 这是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());
}
}

Here is the Echo class: 这是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);
   }
 }

This is the exception that I'm getting: 这是我得到的例外:

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;

Here filename must be like this 这里的文件名必须是这样的

C:/Folder/sample.txt

But if you have saved your file exactly in the same folder where your class is than just sample.txt 但是,如果您将文件完全保存在班级所在的同一文件夹中,而不仅仅是sample.txt

You can use file class to create file also like this. 您也可以使用文件类来创建文件。

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

it will create file in the directory where your class is. 它将在您的课程所在的目录中创建文件。

Make sure what you are passing. 确保您正在通过。

One more thing in your question what you have stated you want to read from simple.txt 问题中的另一件事是您要从simple.txt阅读的simple.txt

while exception is saying that 而例外是说

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

Please make sure what file name is. 请确保文件名是。

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

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