簡體   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