简体   繁体   中英

java read in a file error

I need to read in from a .txt file. 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. 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:

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:

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

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

while exception is saying that

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

Please make sure what file name is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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