Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

want to write file at specified location  RSS feed

 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I am able to generate a pdf file with some text. however i want to save this file at some specified location like " D:/new files/mypdf.pdf" so that all my pdfs generated can be saved at D:/new files but when i am trying to do this, it gives me error in reading the file. Can you suggest?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to Tell The Details.
How are you generating this PDF? How do you specify where the file is created now? What error do you get when you try to change the directory?
 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, instead of D:/lucky.pdf , if i write D:/new files/lucky.pdf then it gives me error : file not found exception, when i try to read it.

the code is

[code]
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("D:/lucky204.pdf"));
document.open();
PdfPTable table = new PdfPTable(rsmd.getColumnCount());


for (iTemp = 1; iTemp <= rsmd.getColumnCount(); iTemp++) {

column = rsmd.getColumnLabel(iTemp);

table.addCell(column);


// table.addCell(column);

// document.add(new Paragraph(column + "\t\t\t\t"));

}

while (rs.next()) {

System.out.print("This is column count" + rsmd.getColumnCount());

//document.add(new Paragraph("\n"));
for (iTemp = 1; iTemp <= rsmd.getColumnCount(); iTemp++) {


row = rs.getString(iTemp);


System.out.println("this is the row value "+ row);

// str+=rs.getString(iTemp);

table.addCell(row);
// table.addCell(row);

// document.add(new Paragraph(row + "\t\t\t\t"));
}


}
document.add(table);
document.close();
[\code]
 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, instead of D:/lucky.pdf , if i write D:/new files/lucky.pdf then it gives me error : file not found exception, when i try to read it.

the code is

 
Muhammad Ijaz
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the file "D:/new files/lucky.pdf" exist at the given location when you try to read the mentioned file? What I got from your question is that you are writing the file at "D:/lucky204.pdf" and trying to read it from "D:/new files/lucky.pdf". If this is the case then please read from the same location where you are writing it.

Regards,
Pomy
 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean to say, I am creating a pdf at D: with name lucky.pdf. But I want to create a pdf file as D:/new files/lucky.pdf but when i try to do it using this line
then it do not find the file if i try to read it(the file from the specified location D:/new file/lucky.pdf ).
Please suggest now.

 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lokesh jain wrote:hi, instead of D:/lucky.pdf , if i write D:/new files/lucky.pdf then it gives me error : file not found exception, when i try to read it.


Do you check to see if the file exists after writing it/before trying to read it?
 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not make the file even see the error message i get :
Exception java.io.FileNotFoundException: D:\new files\lucky204.pdf (The system cannot find the path specified)
 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not make the file even see the error message i get :



 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the path "D:\new files" exist?
 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , the issue is resolved. A million thanks
 
sudhir kurakula
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

Could you please tell me how you resolved the issue? I am having the same problem.

Your help is much appreciated.

regards

Kursy003
 
lokesh jain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
sorry for late reply, I saw it today only. I just made the file path part as constant and it worked. Just take D: as constant drive, new files as file and lucky.pdf as filename. In short, we are combining the file name as drive+file+filename.
Hope this may help you
 
Programmer For Life
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java won't let you (the programmer) write to a file type that is in any other format except for notepad's .txt format

File nameofFile = new File ("directory")

"directory" = where the file that needs to be written into is saved

for example, say it is saved in the C drive and it is called test

instead of "directory", you would put "C:\\test"

the double backslashes are required since a single backslash (\) is a reserved term

\ can be used to do several basic things:

\n creates a new line
\t creates "tabs"
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch. Please check your private messages for an important administrative message.

Programmer For Life wrote:Java won't let you (the programmer) write to a file type that is in any other format except for notepad's .txt format


Java can be used to read and write many file types. Binary files (as opposed to text files) can be read and written via the various *putStream classes. There are many third party API's available to manipulate popular file types, like iText for PDF and Apache POI for Excel.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!