编写一个从List.txt文件中读取列表的java程序,程序应该将已排序的数组输出到SortedList.txt文件

时间:2017-12-12 16:59:54

标签: java

以下代码编译但是它给了我一个错误。

  

线程中的异常" main" java.io.FileNotFoundException:       \ Users \ name \ Documents \ HeapProject \ Employee.txt(没有这样的文件或       目录)
      在java.base / java.io.FileInputStream.open0(本机方法)
      在java.base / java.io.FileInputStream.open(FileInputStream.java:196)
      在java.base / java.io.FileInputStream。(FileInputStream.java:139)
      在java.base / java.util.Scanner。(Scanner.java:611)
      在EmployeeDemo.main(EmployeeDemo.java:18)
   ---- jGRASP wedge:进程的退出代码是1.
   ---- jGRASP:操作完成。

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.*;

public class EmployeeDemo
{
   public static void main(String[] args)throws IOException 
   {
    //list of employees in the file 
      Employee[] list = new Employee[5];
      Scanner keyboard = new Scanner(System.in);

      System.out.println("Enter the text file: ");
      String fileName = keyboard.nextLine();
    //read the file from the same folder
      File myFile = new File(fileName);
      Scanner inputFile = new Scanner(myFile);

      String id = inputFile.nextLine();
      String name = inputFile.nextLine();
      double salary = inputFile.nextDouble();
      String department = inputFile.nextLine();
      String position = inputFile.nextLine();
      int yearsOfService = inputFile.nextInt();
  //there are 6 items for all 5 employees
      list[0] = new Employee(id, name, salary, department, position, 
      yearsOfService);

      System.out.println("Enter the sorted text file: ");
      //String fileName = keyboard.nextLine();
      PrintWriter outputFile = new PrintWriter("outputFile.txt");

      HeapSort.heapSort(list);

      while(inputFile.hasNext())
      {
         outputFile.println(list[0].toString());
      }

      outputFile.close();
      inputFile.close();
      System.out.print("File is sorted into the sorted file");
   }
}

the output should print the data from the file to the sorted file. 

1 个答案:

答案 0 :(得分:0)

第一

检查\ Users \ name \ Documents \ HeapProject \中是否有Employee.txt 并且请确保,在.txt

之后,您不会输入任何空格或类似内容

您的输入应该是“Employee.txt”,而不是“Employee”,您的输入文件名应该是“Employee.txt”。

第二: 你可以尝试:

导入这些;

import java.io.BufferedReader;
import java.io.FileReader;

然后,试试这个:

FileReader fReader = new FileReader(inputFile);
BufferedReader bReader = new BufferedReader(fReader);

String id = bReader.readLine();
String name = bReader.readLine();
double salary = Double.parseDouble(bReader.readLine());
String department = bReader.readLine();
String position = bReader.readLine();
int yearsOfService = Integer.parseInt(bReader.readLine());