我需要帮助让程序以特定格式输出文本文件。

时间:2015-03-24 15:57:06

标签: java eclipse io bluej

这是我到目前为止的程序。它需要输出到特定格式的文本文件,但我有麻烦请帮忙。它没有向测试文件输出任何内容。

import java.util.*;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class InputStream 
{ 
public static void main(String[] args){
FileInputStream filestream;
BufferedReader reader;
FileOutputStream output;
PrintWriter writer;
String firstline;
String body = "";
String line;

try{
filestream = new FileInputStream("salary.txt");
reader = new BufferedReader(new InputStreamReader(filestream));
output = new FileOutputStream("test.txt");
PrintWriter writer1 = new PrintWriter("test.txt");
firstline = reader.readLine();




 String result = firstline + body;
 writer1.write(result);
 reader.close();
 reader = null;
 filestream = null;

   }
      catch(IOException e)
     {
     System.out.println(e);

  }

 } 

}

我无法正确输出,但这应该是它的外观。

Example Input   file
Ralph Kramden associate 70817.75
Jon T. Doe assistant 79174.73
Ed Norton associate 69619.0
Tom Foolery full 116992.43


Sample  Output  file
Doe, Jon T. assistant 79174.73
-------
Total Salary: $79174.73
Average Salary: $79174.73
Kramden, Ralph associate 70817.75
Norton, Ed associate 69619.0
 -------
 Total Salary: $140436.75
 Average Salary: $79174.73

1 个答案:

答案 0 :(得分:1)

首先,当您使用文件时,请务必在写入文件后关闭以进行保存。 你关闭了“读者”,但没有关闭“作家”。

以下是输出所需内容的示例:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class InputStream {

private static final String ASSISTANT = "assistant";
private static final String ASSOCIATE = "associate";
private static final String FULL = "full";

public static void main(String[] args) {
    FileInputStream filestream;
    BufferedReader reader;
    PrintWriter writer;
    String line;

    double totalAssistant = 0;
    double totalAssociate = 0;
    double totalFull = 0;

    try {
        filestream = new FileInputStream("salary.txt");
        reader = new BufferedReader(new InputStreamReader(filestream));
        writer = new PrintWriter("test.txt");

        List<String> assistantList = new ArrayList<String>();
        List<String> associateList = new ArrayList<String>();
        List<String> fullList = new ArrayList<String>();

        while ((line = reader.readLine()) != null) {

            String[] split = line.split(" ");

            // Value
            double value = Double.parseDouble(split[split.length - 1]);

            // Type
            String type = split[split.length - 2];

            if (ASSISTANT.equals(type)) {
                totalAssistant += value;
                assistantList.add(line);

            } else if (ASSOCIATE.equals(type)) {
                totalAssociate += value;
                associateList.add(line);

            } else if (FULL.equals(type)) {
                totalFull += value;
                fullList.add(line);
            }
        }

        writeInFileOutput(writer, totalAssistant, assistantList);
        writeInFileOutput(writer, totalAssociate, associateList);
        writeInFileOutput(writer, totalFull, fullList);

        reader.close();

        writer.close();
    } catch (IOException e) {
        System.out.println(e);

    } finally {
        reader = null;
        filestream = null;
        writer = null;
    }

}

private static void writeInFileOutput(PrintWriter writer, double total,
        List<String> listLines) {
    for (String assistant : listLines) {
        writer.append(assistant).append("\n");
    }

    writer.append("-------\n");
    writer.append("Total Salary: $").append(String.valueOf(total)).append("\n");
    writer.append("Average Salary: $")
            .append(String.valueOf(total / listLines.size())).append(" \n\n");
}

}

输出:

Jon T. Doe assistant 79174.73
-------
Total Salary: $79174.73
Average Salary: $79174.73 

Ralph Kramden associate 70817.75
Ed Norton associate 69619.0
-------
Total Salary: $257429.18
Average Salary: $128714.59 

Tom Foolery full 116992.43
-------
Total Salary: $116992.43
Average Salary: $116992.43