为什么此代码在txt文件中的每个条目后输入一个空行?

时间:2013-04-02 03:19:36

标签: java io

为什么此代码在txt文件中的每个条目后输入一个空行?

<00> 000 ,, 00.00
000 ,, 00.00
5,金,6.50

000 ,, 00.00
000 ,, 00.00

import java.nio.file.*;

import java.io.*;

import java.nio.channels.FileChannel;

import java.nio.ByteBuffer;

import static java.nio.file.StandardOpenOption.*;

import java.util.Scanner;

public class CreateEmployeesRandomFile

{

    public static void main(String[] args)

    {

        Scanner input = new Scanner(System.in);

        Path file = Paths.get("RandomEmployees.txt");

        String s = "000, ,00.00" + System.getProperty("line.separator");

        final int RECSIZE = s.length();

        FileChannel fc = null;

        String delimiter = ",";

        String idString;

        int id;

        String name;

        String payRate;

        final String QUIT = "999";

        try

        {

                fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);

                System.out.print("Enter employee ID number >> ");

                idString = input.nextLine();

                while(!(idString.equals(QUIT)))

                {

                    id = Integer.parseInt(idString);

                    System.out.print("Enter name for employee #" + id + " >> ");

                    name = input.nextLine();

                    System.out.print("Enter pay rate >> ");

                    payRate = input.nextLine();

                    s = idString + delimiter + name + delimiter + payRate + System.getProperty("line.separator");

                    byte[] data = s.getBytes();

                    ByteBuffer buffer = ByteBuffer.wrap(data);

                    fc.position(id * RECSIZE);

                    fc.write(buffer);

                    System.out.print("Enter next ID number or " + QUIT + " to quit >> ");

                    idString = input.nextLine();

                }

                fc.close();

        }

        catch (Exception e)

        {

            System.out.println("Error message: " + e);

        }

    }

}

也是我的第一篇文章,如果我做错了,请告诉我,谢谢,

0 个答案:

没有答案