-4

编辑我的代码和问题,我将 BufferedWriter 更改为类对象并初始化它

public static String filename = "frequencies.txt";
public static BufferedWriter bufferedWriter;
{
    try
    {
        bufferedWriter = new BufferedWriter( new FileWriter( filename ) );
    }
    catch ( IOException e )
    {
        e.printStackTrace();
    }
}


private void plot_Frequencies ( final ITraceDetails msg )
{
    byte[] signal = msg.getFieldValue( "_Decoder Message" ).data();
    int[] frequencies = new int[ 32 ];
    try
    {
        for ( int i = 0, j = 29; i < frequencies.length; i++, j += 2 )
        {
            int low = signal[ j ] & 0xFF;
            int high = signal[ j + 1 ] & 0xFF;
            frequencies[ i ] = low | ( high << 8 );
            this.Frequencies.add( createGTSXYDataItem( msg.getMsgIndex(), msg.getMsgIndex(), msg.getExtendedMid(), frequencies[ i ] ), false );
        }

        write_Frequencies( frequencies, band_number, bufferedWriter );
    }
    catch ( Exception e )
    {
        e.printStackTrace();
    }
}


private void write_Frequencies ( final int[] frequencies, final int band, final BufferedWriter writer ) throws IOException
{

    try
    {
        // Start writing to the output stream
        System.out.print( Arrays.toString( frequencies ) );
        System.out.print( "\n" );
        writer.write( "Hello World" );
        writer.write( Arrays.toString( frequencies ) );
        writer.newLine();
    }
    catch ( FileNotFoundException ex )
    {
        ex.printStackTrace();
    }
    catch ( IOException ex )
    {
        ex.printStackTrace();
    }
}

现在它不会向文件打印任何内容,它只是创建一个空的 frequencie.txt 文件

4

2 回答 2

3

您可以在 body 类上进行声明(带初始化),例如:

BufferedWriter bufferedWriter = new BufferedWriter( new FileWriter( filename ) );

但是你不能声明,然后在 body 类上执行“一些其他代码”

BufferedWriter bufferedWriter = null; 

bufferedWriter = new BufferedWriter( new FileWriter( filename ) ); // 'some other code'

除非你在一个块中这样做:

BufferedWriter bufferedWriter = null; 

{//begining of a block
    bufferedWriter = new BufferedWriter( new FileWriter( filename ) ); // 'some other code'
}//end of a block
于 2013-06-25T13:46:03.593 回答
3

我讨厌这样做......考虑到这个问题的框架有多糟糕。但是为了帮助你:

我假设您正在从其他方法或构造函数中调用 write_Frequencies 并且您知道如何编码,并且不知何故不愿意在SSCCE中共享它。

这是调用函数/构造函数:

BufferedWriter bufferedWriter = new BufferedWriter( new FileWriter( filename ) );

for (int i=0;  i< MAX; i++) { // some max 
int[] frequencies = getFrequenciesSomeHow(); //assume I already have the content which i want to print
  write_Frequencies( frequencies, bufferedWriter );
}

然后是更新后的 write_Frequencies 方法。

private void write_Frequencies ( final int[] frequencies, BufferedWriter writer ) throws IOException
{

    try
    {
        // Start writing to the output stream
        bufferedWriter.write( Arrays.toString( frequencies ) );
        bufferedWriter.newLine();
    }
    catch ( FileNotFoundException ex )
    {
        ex.printStackTrace();
    }
    catch ( IOException ex )
    {
        ex.printStackTrace();
    }

}

但是,下一次我们希望先看到一个更好的框架问题!

于 2013-06-25T13:47:30.880 回答