import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class WriteFileExample
{
private static String filename = "temp.txt";
public static void main( String args[] )
{
try {
FileWriter outFile = new FileWriter( filename );
PrintWriter out = new PrintWriter(outFile); //gets access to the printXXX methods (like println(int)), BUT swallows exceptions
out.println("First line of text");
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}//end class
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*
private void writeToFileSystem( String fullpath, String filename , String message ) throws IOException
{
FileWriter outFile = new FileWriter( fullpath + filename , true ); //append
BufferedWriter out = new BufferedWriter( outFile ); //advisable to wrap a BufferedWriter around costly FileWriters, OutputStreamWriters
out.write( message );
out.newLine();
out.close();
}
// ALTERNATE PrintWriter writer = new PrintWriter( new BufferedWriter( new FileWriter( "filename.txt") ) );
private String getServletFileSystemPath()
{
ServletContext myServletContext = getServletContext() ;
String fileseperatorsymbol = System.getProperty( "file.separator" );
String realpath = myServletContext.getRealPath( fileseperatorsymbol );
String fullpath = realpath + fileseperatorsymbol;
return fullpath;
}
private void logMessage( String str )
{
context.log( str );
String path = getServletFileSystemPath();
try{
writeToFileSystem( path , "licenselog.txt" , str );
}catch( IOException e )
{ e.printStackTrace();
}
}
*/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
long startTime = System.currentTimeMillis();
InputStream localInputStream = new FileInputStream( localPath + filename );
InputStream remoteInputStream = connector.getFile(filename, sessionToken);
FileOutputStream output = new FileOutputStream(localPath + filename + "-validation");
byte[] remoteBuffer = new byte[10240]; // 1MB buffer
byte[] localBuffer = new byte[10240]; // 1MB buffer
int bytesRead;
while ((bytesRead = remoteInputStream.read(remoteBuffer)) != -1)
{
localInputStream.read(localBuffer);
if( ! Arrays.equals( remoteBuffer, localBuffer ))
{
getFileBackSuccess = false;
break;
}
output.write(remoteBuffer);
}
if( bytesRead == fileSize && getFileBackSuccess )
{
canProceedToNextStep = processTimingResult(maxTimePermittedInMilliseconds, startTime, System.currentTimeMillis(), testName);
// Examples of getting streams, converting to byte arrays, calculating sha1
InputStream sha1 = main.testerDefault.getStream( "qa-gateway", "00000ce0-3da2-4a42-a28a-54ee354ffe36.0.sha1" );
String sha1String = convertStreamToString( sha1 );
System.out.println( sha1String + " Remote sha1" );
sha1.close();
byte[] dataArray;
dataArray = main.testerDefault.getObject( "qa-gateway", "00000ce0-3da2-4a42-a28a-54ee354ffe36.0" );
System.out.println( CalculateSha1.computeSHA1Hash( dataArray ) + " Remote getObject to sha1" );
InputStream data = main.testerDefault.getStream( "qa-gateway", "00000ce0-3da2-4a42-a28a-54ee354ffe36.0" );
String tempFileName = "c:\\Users\\John\\Desktop\\test.txt";
FileOutputStream fileOut = new FileOutputStream( tempFileName );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
final int ONE_KILOBYTE = 1024;
byte[] buffer = new byte[64 * ONE_KILOBYTE];
int bytesRead = 0;
while( true ) { // read until end
bytesRead = data.read( buffer );
if( bytesRead > 0 ) {
bos.write( buffer, 0, bytesRead );
fileOut.write( buffer , 0 , bytesRead );
fileOut.flush();
} else {
break; // no more data to write
}
}
fileOut.close();
data.close();
System.out.println( CalculateSha1.computeSHA1Hash( bos.toByteArray() ) + " Remote data stream to byte array calc sha1" );
bos.close();
CalculateSha1 fsha1 = new CalculateSha1( tempFileName );
System.out.println( fsha1.getSha1() + " Remote data to file generated Sha1 " );
}// end main
public static String convertStreamToString( java.io.InputStream is ) {
java.util.Scanner s = new java.util.Scanner( is ).useDelimiter( "\\A" );
return s.hasNext() ? s.next() : "";
}