Java:无法读取工资单项目中的txt文件

时间:2017-12-11 20:11:19

标签: java eclipse

工资单项目示例,一旦我上传并在eclipse上构建它就会显示此错误。为什么我不能读取txt文件?

我应该阅读的Employee.txt文件

001
Employee A
Level 1
380

002
Employee B
Level 2
450

003
Employee C
Level 3
550

我收到的错误无法读取txt文件。

Exception in thread "main" java.io.FileNotFoundException: Employee.txt (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:196)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:139)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:94)
    at MainPackage.payroll.main(payroll.java:12)

java代码

package MainPackage;

import java.io.*;

public class payroll {
    public static void main(String args[]) throws IOException {

        boolean notExists = true;

        while (notExists == true) {
            // objects
            FileInputStream fstream = new FileInputStream("Employee.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader textReader = new BufferedReader(new InputStreamReader(in));
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            // Declarations
            String str = "", UserInput = "", tmp = "";
            String empcode = "", empname = "", emplevel = "";
            double emprate = 0;
            int ctr = 0;
            String timeIn = "", timeOut = "", holiday = "", overtin = "", overtout = "";
            double totalHours = 0, hours = 0, minutes = 0, tmpTime = 0, late = 0, undertime = 0, overHours = 0,
                    overMinutes = 0;
            double salary = 0, overSal = 0, tmpSal = 0, regular = 0;
            double tmpLate = 0, tmpHour = 0, tmpUndertime = 0;

            System.out.print("Enter Employee Code : ");
            UserInput = br.readLine();

            // loop through the text file
            while ((str = textReader.readLine()) != null) {
                if (str.equalsIgnoreCase(UserInput)) {
                    empcode = UserInput;
                    // found the userinput, store the details now
                    while ((tmp = textReader.readLine()) != null) {
                        // lets put everyline into a variable for reference use
                        switch (ctr) {
                        case 0:
                            empname = tmp;
                            break;
                        case 1:
                            emplevel = tmp;
                            break;
                        case 2:
                            emprate = Double.parseDouble(tmp);
                            break;
                        }
                        ctr++;

                    }

                }
                // System.out.println(str);
            }

            System.out.println();
            System.out.println();

            // check if record exists
            if (!(empcode.equalsIgnoreCase(""))) {
                // display the record if it exists
                System.out.println("-------------------1 Details---------------------------");
                System.out.println("Name of the Employee : " + empname);
                System.out.println("Employee Code : " + empcode);
                System.out.println("Employee Level : " + emplevel);
                System.out.println("Employee Rate : " + emprate);

                // enter for time in and time out
                String CurrentDay = "";
                int ctr2 = 1;
                while (ctr2 <= 5) {
                    switch (ctr2) {
                    case 1:
                        CurrentDay = "Monday";
                        break;
                    case 2:
                        CurrentDay = "Tuesday";
                        break;
                    case 3:
                        CurrentDay = "Wednesday";
                        break;
                    case 4:
                        CurrentDay = "Thursday";
                        break;
                    case 5:
                        CurrentDay = "Friday";
                        break;
                    }
                    System.out.println();
                    System.out.println();

                    // ask for time in and time out for regular and overtime
                    // also ask if today is holiday
                    System.out.print("Enter Time In for " + CurrentDay + " : ");
                    timeIn = br.readLine();
                    System.out.print("Enter Time Out for " + CurrentDay + " : ");
                    timeOut = br.readLine();
                    System.out.print("Is today holiday? [y/n] : ");
                    holiday = br.readLine();
                    System.out.print("Enter Time In for Over Time : ");
                    overtin = br.readLine();
                    System.out.print("Enter Time Out for Over Time : ");
                    overtout = br.readLine();

                    // split the hour and minute
                    String tin[] = timeIn.split(":");
                    String tout[] = timeOut.split(":");

                    // time calculation
                    hours += (Double.parseDouble(tout[0]) - Double.parseDouble(tin[0])) >= 9 ? 8
                            : Double.parseDouble(tout[0]) - Double.parseDouble(tin[0]);
                    tmpTime = Double.parseDouble(tout[0]) - Double.parseDouble(tin[0]);
                    tmpHour = (Double.parseDouble(tout[0]) - Double.parseDouble(tin[0])) >= 9 ? 8
                            : Double.parseDouble(tout[0]) - Double.parseDouble(tin[0]);

                    // minute late and under time calculation
                    if (tmpTime <= 9) {
                        late += Double.parseDouble(tin[1]);
                        tmpLate = Double.parseDouble(tin[1]);
                    }

                    if (Double.parseDouble(tout[0]) < 17) {
                        undertime += 60 - Double.parseDouble(tout[1]);
                        tmpUndertime = 60 - Double.parseDouble(tout[1]);
                    }

                    // salary rate calculation
                    if (holiday.equalsIgnoreCase("y")) {
                        tmpSal = (emprate / 8) * 1.1;
                        tmpTime = (((tmpHour * 60) - (tmpLate + tmpUndertime)) / 60);
                        hours -= tmpTime;
                        overHours += (((tmpHour * 60) - (tmpLate + tmpUndertime)) / 60);
                        overSal += tmpSal * tmpTime;
                    } else {
                        tmpSal = emprate / 8;
                        tmpTime = (((tmpHour * 60) - (tmpLate + tmpUndertime)) / 60);
                        salary += tmpSal * tmpTime;
                        // System.out.println(salary + " = " + tmpSal * tmpHour + " " + tmpLate + " " +
                        // tmpUndertime + " ");
                    }

                    // if there is overtime
                    if (!(overtin.equalsIgnoreCase("00:00") || overtin.equalsIgnoreCase(""))) {
                        String ovin[] = overtin.split(":");
                        String ovout[] = overtout.split(":");

                        double tmp1 = 0;
                        double tmp2 = 0;
                        double minTmp = 0;

                        tmp1 = Double.parseDouble(ovin[0]) <= 17 ? 17 : Double.parseDouble(ovin[0]);
                        tmp2 = Double.parseDouble(ovout[0]) <= 20 ? 20 : 20;
                        minTmp = Double.parseDouble(ovin[1]) + Double.parseDouble(ovout[1]);

                        if ((tmp2 - tmp1) <= 3 && tmp2 - tmp1 > 0) {
                            overMinutes = ((tmp2 - tmp1) * 60) - minTmp;
                            overHours += overMinutes / 60;
                            double overTmp = overMinutes / 60;
                            double overRate = (emprate / 8) * 1.1;
                            overSal += overRate * overTmp;
                        }
                    }

                    ctr2++;
                }

                System.out.print("Enter Date Covered : ");
                String covered = br.readLine();
                minutes += (hours * 60) - (late + undertime);
                totalHours = minutes / 60;

                System.out.println();

                System.out.println();
                System.out.println("-------------------------------------------");
                System.out.println("Name of the Employee : " + empname);
                System.out.println("Employee Code : " + empcode);
                System.out.println("Employee Level : " + emplevel);
                System.out.println("Employee Rate : " + emprate);
                System.out.println("-------------------------------------------");
                System.out.println("Date Covered : " + covered);
                System.out.println("Total Hours : " + totalHours);
                System.out.println("Overtime : " + overHours);
                System.out.println("Regular Income : " + salary);
                System.out.println("Overtime Income : " + overSal);
                System.out.println("Gross Income : " + (salary + overSal));
                System.out.println("Deductions");

                // tax and gsis deduction computation
                double tax = ((salary + overSal) * .10);
                double gsis = 0;

                if (emplevel.equalsIgnoreCase("Level 1")) {
                    gsis = (salary + overSal) * .01;
                } else if (emplevel.equalsIgnoreCase("Level 2")) {
                    gsis = (salary + overSal) * .015;
                } else {
                    gsis = (salary + overSal) * 0.02;
                }

                System.out.println(" * Tax : " + tax);
                System.out.println(" * GSIS : " + gsis);
                System.out.println("Net Income : " + (((salary + overSal) - (tax + gsis)) + 500));

                System.out.println("-------------------------------------------");

                notExists = false;

            } else {
                System.out.println("-------------------Record Doesn't Exists---------------------------");
                notExists = true;
            }

        }

    }

}

1 个答案:

答案 0 :(得分:0)

我建议您将Employee.txt移动到资源文件夹(src / main / resources),然后使用

File myFile = new File("c:\\full\\path\\Employee.txt")
FileInputStream fstream = new FileInputStream("myFile");

如果您无法移动文件,或者您真的需要它可以在任何地方使用:

// But are u denied access?
// well here is the solution.
public static void TheKing_DownloadFileFromURL(String search, String path) throws IOException {

    // This will get input data from the server
    InputStream inputStream = null;

    // This will read the data from the server;
    OutputStream outputStream = null;

    try {
        // This will open a socket from client to server
        URL url = new URL(search);

        // This user agent is for if the server wants real humans to visit
        String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";

        // This socket type will allow to set user_agent
        URLConnection con = url.openConnection();

        // Setting the user agent
        con.setRequestProperty("User-Agent", USER_AGENT);

        //Getting content Length
        int contentLength = con.getContentLength();
        System.out.println("File contentLength = " + contentLength + " bytes");


        // Requesting input data from server
        inputStream = con.getInputStream();

        // Open local file writer
        outputStream = new FileOutputStream(path);

        // Limiting byte written to file per loop
        byte[] buffer = new byte[2048];

        // Increments file size
        int length;
        int downloaded = 0; 

        // Looping until server finishes
        while ((length = inputStream.read(buffer)) != -1) 
        {
            // Writing data
            outputStream.write(buffer, 0, length);
            downloaded+=length;
            //System.out.println("Downlad Status: " + (downloaded * 100) / (contentLength * 1.0) + "%");


        }
    } catch (Exception ex) {
        //Logger.getLogger(WebCrawler.class.getName()).log(Level.SEVERE, null, ex);
    }

    // closing used resources
    // The computer will not be able to use the image
    // This is a must
    outputStream.close();
    inputStream.close();
}