Format Specifiers

I set up my output data stream with this code. It works without any errors indicated:

 try
{	
	println( "Path: " +  selection.getCanonicalPath() );
	println( "ext: \""+ getFileExtension(selection.getCanonicalPath() ) + "\"" );
		    	
	if( sDataFileExt. compareTo( getFileExtension( selection.getCanonicalPath() ) ) != 0 )
	{	fDataFile = new File( selection.getCanonicalPath() + ".csv"  );
	}else
	{	fDataFile = new File( selection.getCanonicalPath());
	}
		    	
	pwDataFileStream = new PrintWriter( fDataFile );
		    	
	if( !fDataFile.isFile() )
	{ 	sDataFileMsg = "Cannot create file: \"" + fDataFile.getName() + "\"";
		pwDataFileStream.close();
	}else if( !fDataFile.canWrite() )
	{	sDataFileMsg = "Cannot write to file: \"" + fDataFile.getName() +  "\"";
		pwDataFileStream.close();
	}else if( pwDataFileStream.checkError() )
	{
		sDataFileMsg = "Trouble creating output stream";
		pwDataFileStream.close();
	}
		    	
	else
	{	sDataFileMsg = fDataFile.getName();
		println( sDataFileMsg );
			    	
		fStartingDataFilePath = new File( selection.getCanonicalPath() );
		ini.put( sIniSectFile, sIniKeyDataFilePath, fStartingDataFilePath.getCanonicalPath() );
		ini.store();
	}

}catch(Exception e)   
{	System.err.println(e.getMessage());
}

Elsewhere in the code I attempt write data using the code below. The output to the console shows that the asc[] int array is valid. In the try-catch block, only the first attempt to use pwDataFileStream.println() works. The four below it cause pwDataFileStream.checkError() to return positive.

println (asc[0],"  ",asc[1],"  ",asc[2],"  ",asc[3]); // Works, outputs to console.
try
{
        pwDataFileStream.println( "Something" ); // Works
        pwDataFileStream.println( asc[0] + "," + asc[1] + "," + asc[2] + "," + asc[3] ); // .checkError()  true
        pwDataFileStream.printf( "%X,%X,%X,%X%n", asc[0],asc[1],asc[2],asc[3]); // .checkError()  true
        pwDataFileStream.printf( "%x,%x,%x,%x%n", asc[0],asc[1],asc[2],asc[3]); // .checkError()  true
        pwDataFileStream.printf( "%d,%d,%d,%d%n",asc[0],asc[1],asc[2],asc[3]); // .checkError()  true

if( pwDataFileStream.checkError() )
{	pwDataFileStream.close();
	sDataFileMsg = "File stream post-error";
	println( sDataFileMsg );
}
}catch( Exception e )
{	System.err.println(e.getMessage());
	pwDataFileStream.flush();
	pwDataFileStream.close();
}

I know that these methods do not throw errors. I nevertheless used the try-catch block in case I am wrong.
It looks to me like something is wrong in the format specifiers. What?

1 Like

Were you able to resolve this issue?

My first guess would be that %n is the problem, and that you should use \n instead. I haven’t actually used %n before, but I thought that printf matched % terms with arguments – so you have five terms, but only four arguments. Just an initial guess without testing.

https://www.tutorialspoint.com/java/io/printwriter_printf_string.htm

This problem is solved. The format string is valid. The file was not written to because pwDataFileStream had been closed elsewhere in the program. This is what caused pwDataFileStream.checkError() to return true, because pwDataFileStream.out was set null.

2 Likes