如何使用java.util.Formatter类将数据插入到Java文件中?

时间:2015-07-30 07:06:05

标签: java io

尝试使用Formatter类将数据插入文件,但在编译和插入输入后,文件仍为空。

以下是代码:

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

public class OutputFile {


    private Formatter output;
    private InputStreamReader isr = new InputStreamReader( System.in );;
    private BufferedReader br = new BufferedReader( isr );;

    public OutputFile()
    {

    }

    public void openFile( String file )
    {
        try{
          output = new Formatter( file );  
        }catch( SecurityException e )
        {
            e.printStackTrace();
            System.exit( 1 );
        }catch( FileNotFoundException e  )
        {
            System.err.println( file+" not found" );
            System.exit( 1 );
        }
    }
    public void addRecord()
    {
        AccountRecord record = new AccountRecord();

        Scanner input = new Scanner( System.in );



        while( true )
        {
            try{

                System.out.println( "\nEnter name:" );
                record.setName( br.readLine() );

                System.out.println( "\nEnter account number(>0):" );
                record.setAccNum( input.nextInt() );

                if( record.getAccNum()>0 )
                {
                    output.format("\n%d %s \n", record.getAccNum(),record.getName() );
                }else{
                    System.out.println( "Account number must be greater than zero" );

                }
            }catch( FormatterClosedException e )
            {
                System.err.println( "Formatter is closed" );
                System.exit( 1 );
            }
            catch( NoSuchElementException e )
            {
                System.err.println( "Invalid input" );
                System.exit( 1 );
            }
             catch( IOException e )
             {
                 System.err.println( "Invalid input" ); 
                 System.exit( 1 );

             }

            System.out.printf("%s\n%s", "Press t to terminate","Press any character to continue:" );
            if( input.next().equals( "t" ) )
            {
                System.exit( 1 );
            }

        }//end of while loop..
    }//end of addRecord method

    public void closeFile()
    {
        try{
        if( output != null )
        {
            br.close();
            isr.close();
            output.close();
        }}catch( IOException e )
        {

        }
    }
}//end of class

2 个答案:

答案 0 :(得分:1)

基本上...

System.out.printf("%s\n%s", "Press t to terminate","Press any character to continue:" );
if( input.next().equals( "t" ) )
{
    System.exit( 1 );
}

终止JVM而不刷新Formatter

的内容

相反,您应该使用退出条件退出循环

boolean keepEntering = true;
do {
    //...
    System.out.printf("%s\n%s", "Press t to terminate", "Press any character to continue:");
    String text = input.nextLine();
    if (text.equals("t")) {
        keepEntering = false;
    }

} while (keepEntering);

退出addRecord后,您应该调用closeFile方法

OutputFile of = new OutputFile();
of.openFile("Banana.txt");
of.addRecord();
of.closeFile();

作为旁注,您不要InputStreamReaderBufferedReader Scanner#nextLine将执行相同的工作

您还可以重新访问终止提示,因为它可以在缓冲区中留下一个新的行字符,可以被其他输入请求选中,跳过输入提示

答案 1 :(得分:0)

退出之前关闭Formatter。

 if( input.next().equals( "t" ) )
{
            output.close();
           System.exit( 1 );
}