繁体   English   中英

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

[英]How to insert data into a file in Java using java.util.Formatter class?

我正在尝试使用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

基本上...

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将完成相同的工作

您还可以重新访问终止提示,因为它可能会在缓冲区中留下换行符,而其他输入请求可能会选择该换行符,从而跳过输入提示

退出之前,请关闭格式化程序。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM