|
|
Subject:
JAVA
Category: Computers > Programming Asked by: axmedeey-ga List Price: $30.00 |
Posted:
14 Mar 2004 02:41 PST
Expires: 31 Mar 2004 03:01 PST Question ID: 316542 |
1) This part requires you to write two versions of a Java program that performs a standard data-processing task, namely, to read and modify the contents of one file, writing the results to a second file. In particular: · The user on the command line provides the names of the input file and output file when the program is run. Your program should check that these two arguments are provided, that the first is a file that is not a directory, and that the second is not an existing file. Hint: the File class provides suitable methods. · The contents of the input file should be read in one byte at a time. Hint: read () either returns an integer between 0 and 255 (the value of an unsigned byte) or -1 (upon reaching the end of the input stream). · Each byte read should be written to the output file as soon as it is available, except for the following three transformations: a) The value 36 should be changed to the value 163, b) The value 10 should be inserted after the value 12, and c) If the value 111 is immediately followed by the value 101, they should be replaced by the single value 248. Can you make the first version of your program single-threaded, and the second version of your program multi-threaded: | |
| |
|
|
There is no answer at this time. |
|
Subject:
Re: JAVA
From: morkeld-ga on 21 Mar 2004 14:44 PST |
// Single threaded one first package ga; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class STFileTranslator { protected boolean initialized = false; protected File inputFile = null; protected File outputFile = null; public STFileTranslator() { } public void init( String[] args ) { if ( args.length != 2 ) { throw new RuntimeException( "Syntax: java ga.FileTranslator <input file> <output file>"); } String inputFileName = args[0]; inputFile = new File(inputFileName); if ( !inputFile.exists() ) throw new RuntimeException("Specified inputFile '" + inputFileName + "' does not exist."); if ( !inputFile.canRead() ) throw new RuntimeException("Specified inputFile '" + inputFileName + "' cannot be read."); if ( inputFile.isDirectory() ) throw new RuntimeException("Specified inputFile '" + inputFileName + "' is a directory, not a file."); String outputFileName = args[1]; outputFile = new File(args[1]); if ( outputFile.exists() ) throw new RuntimeException("Specified outputFile '" + inputFileName + "' should not exist."); initialized = true; } public void process() throws IOException { if ( !initialized ) { throw new RuntimeException( "FileTranslator object not properly initialized!"); } int data, previousData = -1; BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(outputFile)); BufferedInputStream in = new BufferedInputStream(new FileInputStream( inputFile)); while ( (data = in.read()) != -1 ) { if ( data == 36 ) { out.write(163); } else if ( data == 12 ) { out.write(12); out.write(10); } else if ( data == 111 && in.available() >= 1 ) { // wait until next loop, if more can be read } else if ( previousData == 111 ) { if ( data == 101 ) { out.write(248); } else { out.write(111); out.write(data); } } else { out.write(data); } previousData = data; } out.close(); in.close(); } public static void main( String[] args ) throws IOException { STFileTranslator fileTranslator = new STFileTranslator(); fileTranslator.init(args); fileTranslator.process(); } } // Multi-threaded implementation second package ga; import java.io.IOException; public class MTFileTranslator extends STFileTranslator implements Runnable { public void run() { try { process(); } catch ( IOException ex ) { System.err.println("Error while processing"); } } public static void main( String[] args ) throws IOException { MTFileTranslator fileTranslator = new MTFileTranslator(); fileTranslator.init(args); // Perform the processing in its own Thread Thread t = new Thread(fileTranslator); t.start(); } } |
Subject:
Re: JAVA
From: axmedeey-ga on 23 Mar 2004 10:54 PST |
hi is this answer or comment. thank you |
Subject:
Re: JAVA
From: axmedeey-ga on 26 Mar 2004 09:18 PST |
hi there the program did not run for me , so please can you make it clear that every thing working properly then i my understand the whole package. whenever i try to run the program the gives me alot of errors, i don not now what is going on, please help again thank you. |
Subject:
Re: JAVA
From: axmedeey-ga on 29 Mar 2004 05:41 PST |
hi maniac the whole program did not work for me , so please can do it again with a different stall, it may work, and i,m sure i,m using everything wright. thanks man |
Subject:
Re: JAVA
From: axmedeey-ga on 31 Mar 2004 03:01 PST |
it did not work for me yet bu thanks for you help, it maybe my problem |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |