Java读入并读出

时间:2017-09-24 06:03:44

标签: java

我正在尝试读取文本文件值。 - >

Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1

将它们保存在数组中。处理数据。

然后将其输出到新文件。

我只是无法让这个工作,它正在推动我上升。我知道我没有正确地做数组部分,但我只是不知道如何修复它。

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

public class Lab3 {

    public static void main(String[] args) throws FileNotFoundException
    {

        //Create and associate the objects
        File inputFile = new File("Data.txt");
        Scanner in = new Scanner(inputFile);
        //PrintWriter outFile = new PrintWriter("output.txt");

        //Code for data manipulation

        String[] last = new String[2];
        String[] first = new String[2];
        Double[] pay = new Double[2];
        Double[] rate = new Double[2];
        Double[] newPay = new Double[2];


        for(int i=0; i < 3; i++) {
        last[i] = in.next();
        first[i] = in.next();
        pay[i] = in.nextDouble();
        rate[i] = in.nextDouble();

        newPay[i] = (pay[i] * (rate[i]/100)) + pay[i];



        System.out.printf("Record: %s %s %.2f", first, last, newPay);

        }

        //Close file

        inFile.close();
        outFile.close();
    }
}

2 个答案:

答案 0 :(得分:0)

创建一个新的帮助器类,它将包含一行中的数据,如下所示:

public class Employee
{
    private String firstName;
    private String lastName;
    private double pay;
    private double rate;

   /* getter and setter here */
}

然后根据输入文件中的行创建Employee对象列表。

List<Employee> employees = new ArrayList<Employee>();
while (/* file end not reached */) {
    Employee employee = new Employee();
    employee.setFirstName(...);
    employee.setLastName(...);
    employee.setPay(...);
    employee.setRate(...);

    employees.add(employee);
}

当您阅读完所有数据后,请根据您所需的计算调整值:

for (Employee employee: employees) {
    double newPay = employee.getPay() + ...;
    employee.setPay(newPay);
}

然后将员工写回文件

Writer out = PrintWriter(...);
for (Employee employee: employees) {
    String lineToWrite = String.formatf(/* format and values from 'employee' */);
    out.write(lineToWrite);
}
out.close();

答案 1 :(得分:0)

试试这个。我创建了两个读取和写入文本文件的方法,以及一个Person类来更轻松地处理数据。

public class PayHandler {

  public static void main(String[] args) throws IOException {
    File inputFile = new File("data.txt");
    System.out.println(inputFile.getAbsolutePath()); // test to check the location matches
    List<Person> people = readFile(inputFile);
    System.out.println(people); // test to check if all people are read // Implement toString();

    for (Person person : people) {
      double newPay = (person.getPay() * (person.getRate() / 100)) + person.getPay();

      person.setPay(newPay);
    }

    File outputFile = new File("dataOutput.txt");
    writeFile(people, outputFile);
  }

  private static List<Person> readFile(File source) throws IOException {
    List<Person> people = new ArrayList<>();
    try (Scanner in = new Scanner(new BufferedReader(new FileReader(source)))) { // This seems verbose, and it is, but this is the recommended way to read and write
      while (in.hasNext()) {
        String firstName = in.next();
        String lastName = in.next();
        double pay = in.nextDouble();
        double rate = in.nextDouble();

        Person person = new Person(firstName, lastName, pay, rate);
        people.add(person);
      }
    }
    return people;
  }

  private static void writeFile(List<Person> people, File destination) throws IOException {
    try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(destination)))) {
      for (Person person : people) {
        out.write(person.getFirstName() + " ");
        out.write(person.getLastName() + " ");
        out.write(String.valueOf(person.getPay())); // convert double to String
        out.println();
      }
    }
  }
}

如果您有一个Person对象来保存数据,则更容易处理字段。

public class Person {
  private String firstName;
  private String lastName;
  private double pay;
  private double rate;

  public Person(String firstName, String lastName, double pay, double rate) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.pay = pay;
    this.rate = rate;
  }

  // getters & setters

  @Override
  public String toString() {
    return "Person{" +
        "firstName='" + firstName + '\'' +
        ", lastName='" + lastName + '\'' +
        ", pay=" + pay +
        ", rate=" + rate +
        '}';
  }
}