找不到文本文件,java文件I / O错误

时间:2016-03-07 01:21:34

标签: java file-io fileinputstream mainclass

不确定为什么这不会读取保存在同一目录中的txt文件。它编译得很好但是当我输入java BlindfoldsAside到cmd ln时,它说它无法找到或加载我的主类BlindfoldsAside。就我所见,这个案例都是正确的,一切都在正确的文件路径中,所以我不确定我哪里出错了!

package blindfoldsaside;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class BlindfoldsAside {
    public static void main (String[] args) {
        Scanner scannerIn = null;
        FileInputStream in = null;
        BufferedReader inputStream = null;
        int fileChar; //character's int equivalent
        String fileLine;
        try {
            // FileInputStream calls the txt file
            in = new FileInputStream("blindfoldsaside.txt");
            System.out.println("These lyrics tell the story of a woman, named Kezia, who is" + 
        " \nbeing put to death after being forced into prostitution. This song is from the prison" + 
        " \nguard's (who is also the executioner) point of view. I hope you enjoy.");
            // this reads the file one char at a time
            while ((fileChar = in.read()) != -1) {
                System.out.print((char) fileChar);
            }// end while
            System.out.println(""); //separates the file output
            System.out.println("");
            System.out.println("This song was written by Arif Mirabdolbaghi (bassist) and performed by " + 
        " Protest the Hero. This song appears on the band's first full length album, Kezia, " + 
        " released in their native country (Canada) on August 30th, 2005 and to the U.S. on " + 
        " April 4th, 2006. This is the 6th track on the album.");
        } catch (IOException io) {
            System.out.println("File IO exception" + io.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }// end if
                if (inputStream != null) {
                    inputStream.close();
                }// end if
            } catch (IOException io) {
                System.out.println("There was a problem closing your files" + io.getMessage());
            }// end catch
        }// end finally
    }// end main
}// end class

1 个答案:

答案 0 :(得分:1)

尝试将txt文件移动到项目根文件夹中。这应该没有问题。如果有什么变化或它是否正常工作,请告诉我。