954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to delete files stored in a stirng after its use

[I]I want to delete the strings file1 and file2 after its merged to a single pdf merge12.Please suggest your valuable ideas..Thanks in advance/I]

package com;

import java.io.FileOutputStream;
import java.util.ArrayList;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;

/*
* author
* @282532
*/


public class PdfMerger {
public static void main(String args[]){
String file1 = "d:\\PR00002.PDF";
String file2 = "d:\\PR00003.PDF";
String mergedFileLocation = "d:\\merge12.pdf";

String filesTobeMerges[] = new String[] { file1, file2 };
mergeMyFiles(filesTobeMerges, mergedFileLocation);
}

public static void mergeMyFiles(String filesTobeMerged[],String mergedFileLocation){
System.out.println("Starting to merge files...");
System.out.println("total number of files to be merged"+filesTobeMerged.length +"\n");
try{
int pageOffset = 0;
ArrayList masterBookMarkList = new ArrayList();

String outFile = mergedFileLocation;
Document document = null;
PdfCopy writer = null;
PdfReader reader = null;
for (int fileIndex = 0; fileIndex< filesTobeMerged.length; fileIndex++) {
reader = new PdfReader(filesTobeMerged[fileIndex]);
System.out.println("Reading file" +filesTobeMerged[fileIndex]);

/** * Replace all the local named links with the actual destinations.
* *
*/
reader.consolidateNamedDestinations();
int totalPages = reader.getNumberOfPages();

/** * Merging the files to the first file.
* * If we are passing file1, file2 and file3,
* * we will merge file2 and file3 to file1.
* */
if (fileIndex == 0) {
/** * Create the document object from the reader */
document = new Document(reader.getPageSizeWithRotation(1));

/** * Create a pdf write that listens to this document.
* * Any changes to this document will be written the file
* * * outFile is a location where the final merged document * will be written to. */

System.out.println("Creating an empty PDF...");
writer = new PdfCopy(document, new FileOutputStream(outFile));
/** * Open this document */
document.open();
}

/** * Add the conent of the file into this document (writer).
* * Loop through multiple Pages */
System.out.println("Merging File: "+filesTobeMerged[fileIndex]);
PdfImportedPage page;
for (int currentPage = 1; currentPage <= totalPages; currentPage++) {
page = writer.getImportedPage(reader, currentPage);
writer.addPage(page);
}
}
document.close();
System.out.println("File has been merged and written to-"+mergedFileLocation);

}catch(Exception e){
e.printStackTrace();
}
}
}

arathy nair
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

file1 = null;
file2 = null;
?

what exactly do you mean by 'delete the strings'?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

you can simply assign null value to the variable string variable = ""; after the data has been merged.

47pirates
Junior Poster in Training
88 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
 

do keep in mind though,
String a = "";
is not the same as:
String a = null;

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Thanks for your reply.What I mean by delting is that I dont want these files in the folder after these files are merged to a single pdf merge12.

file1 = null; file2 = null; ?

what exactly do you mean by 'delete the strings'?

arathy nair
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

this has nothing to do with the String objects.
you should check out the delete method of the File class.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Hi Thanks for your reply.I have tried by assigning string file1 ="";
But its not working.

you can simply assign null value to the variable string variable = ""; after the data has been merged.
arathy nair
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
Hi Thanks for your reply.I have tried by assigning string file1 =""; But its not working.


that's because manipulating a String object has nothing to do with what you try to do.
read my last post.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

but the problem is that am storing these file names in a string.R u telling me to change this and store it in a file

this has nothing to do with the String objects. you should check out the delete method of the File class.
arathy nair
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

no, I'm saying whether and how you have a filename stored has nothing to do with whether there (still) is a physical file on your hard drive.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Thanks a lot.I got now what u meant.I was able to delete the files after merging. Thank You stultuske

no, I'm saying whether and how you have a filename stored has nothing to do with whether there (still) is a physical file on your hard drive.
arathy nair
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: