Home Answers Viewqa Java-Beginners This Question was UnComplete

 
 


alhisnawy
This Question was UnComplete
2 Answer(s)      3 years and 4 months ago
Posted in : Java Beginners

Implement a standalone procedure to read in a file containing words and white space and produce a compressed version of the file in an output file. The compressed version should contain all of the words in the input file and none of the white space, except that it should preserve lines.
View Answers

February 18, 2010 at 9:33 PM


Hi Alhisnawy,
Below is my version of code. Hope it fulfills your need.Create a sourceFile and goahead.

/**
* Program to take an single input file of text and create an zipped(compressed) file which contains the contents of the input file without any white spaces.
* reference: http://java.sun.com/developer/technicalArticles/Programming/compression/
*/

import java.io.*;
import java.util.StringTokenizer;
import java.util.zip.*;

public class ZipFile {

/**
* @variable inputFileName: Input File to be zipped.
* @variable tempInputFile: Temporary file created to hold the contents of the input file without any white spaces.
*/
static final int BUFFER = 2048;
public static void main(String[] args) {
String inputFileName = "sourceFile";
String destination = "C:\\JavaZip\\zippedFile.zip";
ZipFile zipObject = new ZipFile();
try{
BufferedInputStream origin = null;
byte data[] = new byte[BUFFER];
int count;
FileOutputStream fos = new FileOutputStream(destination);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
/** Call truncateWhiteSpaces(String name) on the input file to truncate white spaces and
* get a temporary file which has all the words of the original file without any white spaces */
String tempInputFile = zipObject.truncateWhiteSpaces(inputFileName);
/** Now file to be zipped is ->tempInputFile. */
FileInputStream fis = new FileInputStream(tempInputFile);
origin = new BufferedInputStream(fis,BUFFER);
/** Zip it **/
ZipEntry entry = new ZipEntry(tempInputFile);
zos.putNextEntry(entry);
while((count = origin.read(data, 0, BUFFER))!= -1) {
zos.write(data, 0, count);
}
origin.close();
zos.close();
/** Delete the temporary file created **/
zipObject.deleteFile(tempInputFile);
}catch(Exception e){
System.err.print("Error Occured");
e.printStackTrace();
}
}

/** This method reads the input file and writes a file which has all the words/content of the input file without any white spaces. **/
public String truncateWhiteSpaces(String fileName){
String tempInputFile = "OutputFile";
try{
File file =new File(tempInputFile);
FileWriter fileWriter = new FileWriter(file);
BufferedWriter writer = new BufferedWriter(fileWriter);
PrintWriter out = new PrintWriter(writer);
FileReader fileReader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(fileReader);
String line = null;
while((line = reader.readLine()) != null){
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens()){
String token = st.nextToken();
out.write(token, 0, token.length());
}
}
reader.close();
writer.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
return tempInputFile;
}

/** This method deletes a file(Used to delete the temporary file). **/
public void deleteFile(String fileName){
File file = new File(fileName);
file.delete();
}

}


Regards,
javaquest2010.

February 19, 2010 at 7:26 AM


Hey Alhisnawy,
Sorry the earlier code posted just removes spaces but NOT WHITE SPACES.
Some how I missed the point of WHITE spaces. Sorry about that.
Here is much more simplified logic. Change is just in while loop of truncateWhiteSpaces() method. Just replace it with following code.

public String truncateWhiteSpaces(String fileName){
String tempInputFile = "OutputFile";
try{
File file =new File(tempInputFile);
FileWriter fileWriter = new FileWriter(file);
BufferedWriter writer = new BufferedWriter(fileWriter);
PrintWriter out = new PrintWriter(writer);
FileReader fileReader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(fileReader);
String line = null;
String regex = "[\\s]";
while((line = reader.readLine()) != null){
String temp = line.replaceAll(regex,"");
out.write(temp,0, temp.length());
}
reader.close();
writer.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
return tempInputFile;
}

Hope this completely helps you.

Thanks & Regards,
javaquest2010.









Related Pages:
This Question was UnComplete - Java Beginners
This Question was UnComplete  Implement a standalone procedure to read in a file containing words and white space and produce a compressed version of the file in an output file. The compressed version should contain all
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
Question
Question   When there is an exception in my program how java runtime system handles
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
question
question  Dear sir i had some typing mistake at previous question so its my humble request to let me know the steps to start the tomcat6 under the tomcat directory
question
question  Gud morning sir, I have asked u some question regarding jsp in saturaday for that i didnot find any answere in which u send me the some of the links.U have asked me the specify some details. There is a entity name
Question?
Question?  My question is how to: Add a menu bar to the program with a File menu. In the File menu, add a submenu (JMenuItem) called About. When the user clicks on the About menu item, display a JOptionPane message dialog