读取导入的文本文件

时间:2015-07-13 09:36:23

标签: java eclipse file-io

我试图阅读我在Eclipse中导入的文本文件,但它给了我一个FileNotFoundException,即使我已将其导入到与代码相同的源文件夹中。它在我写文本文件的路径时有效,但在我只写文件名时却没有。

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

public class Text {
public static void main(String [] args) {
    String un;
    int pass;

    // The name of the file to open or path.
    String fileName = "Pass.txt";

    String line = null;

    System.out.println("Welcome, please enter the username and password to read the content of the file");
    Scanner scan = new Scanner(System.in);
    System.out.println("Username?:");
    un = scan.nextLine();
    System.out.println("Password?:");
    pass = scan.nextInt();

    if(un.equals("Hudhud") && pass==123){
        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bf = new BufferedReader(fileReader);
            while((line= bf.readLine()) !=null){
                System.out.println(line);
        }   

        // close file after reading
        bf.close();       

    }
    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");               
    }
    catch(IOException ex) {
        System.out.println("Error reading file '"+ fileName + "'");                   

    }
}
else {
    System.out.println("Dude, you ain't got rights to open this file");
  }
 }
}

修改

public class Text {
public static void main(String [] args) throws Exception{
    String un;
    int pass;

    // The name of the file to open or path.
    String fileName = "Pass.txt";

    String line = null;


    System.out.println("Welcome, please enter the username and password to read the content of the file");
    Scanner scan = new Scanner(System.in);
    System.out.println("Username?:");
    un = scan.nextLine();
    System.out.println("Password?:");
    pass = scan.nextInt();
    Class cls = Class.forName("com.demo.Text");
    ClassLoader cLoader = cls.getClassLoader();
    URL url = cLoader.getResource("Pass.txt");

    if(un.equals("Hudhud") && pass==123){
        try {
            FileReader fileReader = new FileReader(new File(url.getFile()));
            BufferedReader br = new BufferedReader(fileReader);
            JFrame frame = new JFrame();
            JTextArea jta = new JTextArea();
            jta.setFont(new Font("Serif", Font.BOLD, 40));
            frame.getContentPane().add(jta);
            frame.setSize(370,100);
            jta.setBackground(Color.YELLOW);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            while ((line = br.readLine()) != null)
                jta.append(line);

            // close file after reading
            br.close();       

        }
        catch(FileNotFoundException ex) {
            System.out.println("Unable to open file '" + fileName + "'");               
        }
        catch(IOException ex) {
            System.out.println("Error reading file '"+ fileName + "'");                   

        }
    }
    else {
        System.out.println("Dude, you ain't got rights to open this file");
    }
 }
}

2 个答案:

答案 0 :(得分:0)

即使您已在源目录中添加了该文件,它依赖于Eclipse上的配置,即您的程序的工作目录。 您可以从运行配置... 选项配置工作目录。 您还可以使用此java行找到工作目录:

  

System.out.println(“Working Directory =”+                 System.getProperty( “user.dir来”));

答案 1 :(得分:0)

见下面的答案。它对我来说很好。我把我的课程和文本文件放在 com / demo / 包中。稍后,它会在.class文件格式后自动复制到 bin / com / demo / 文件夹。

package com.demo;


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

public class New {
public static void main(String [] args) throws Exception{
    String un;
    int pass;

    // The name of the file to open or path.
    String fileName = "Pass.txt";

    String line = null;

    System.out.println("Welcome, please enter the username and password to read the content of the file");
    Scanner scan = new Scanner(System.in);
    System.out.println("Username?:");
    un = scan.nextLine();
    System.out.println("Password?:");
    pass = scan.nextInt();
    Class cls = Class.forName("com.demo.New");
    ClassLoader cLoader = cls.getClassLoader();
    System.out.println(cLoader.getClass());
    URL url = cLoader.getResource("com/demo/Pass.txt");
    System.out.println("Value = " + url);

    if(un.equals("123") && pass==123){
        try {
            FileReader fileReader = new FileReader(new File(url.getFile()));
            BufferedReader bf = new BufferedReader(fileReader);
            while((line= bf.readLine()) !=null){
                System.out.println(line);
        }   

        // close file after reading
        bf.close();       

    }
    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");               
    }
    catch(IOException ex) {
        System.out.println("Error reading file '"+ fileName + "'");                   

    }
}
else {
    System.out.println("Dude, you ain't got rights to open this file");
  }
 }
}

<强> O / P:

  class sun.misc.Launcher$AppClassLoader
  Value = file:/D:/info/satya1/abc/bin/com/demo/Pass.txt