wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Copy name of file from input to output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Copy name of file from input to output" Watch "Copy name of file from input to output" New topic
Author

Copy name of file from input to output

Vieckov Vieckovsky
Greenhorn

Joined: Jul 11, 2012
Posts: 7
Hi all !
I have problem. I want to get in output file part of name of inputfile. Here is my code:


I know it is mess up but I dont know how to do it so please help me. For example in terminal I write java <inputfile.csv>, java X0000001.csv and generate for me a outputfile: wynik1.csv all the time. It should be Y00001.csv so Y - 00001, this second part should be copied from input and paste after Y in output file. It is possible in java ? Thanks for any help.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Welcome to the Ranch, Vieckov.

You name the output file in the below line of code -


So if you need to change the name of the output file depending on the input file, you need to get the input file name first. To get the input file name, you can use the getName() method in the File class on the input file object f.

After you have the input file name, form a desired file name which should be used for the output file.
Vieckov Vieckovsky
Greenhorn

Joined: Jul 11, 2012
Posts: 7
Thanks, but how to do it. I have input file in main. How to change it. I am very begginer in java so forgive me for stupid questions.
Vieckov Vieckovsky
Greenhorn

Joined: Jul 11, 2012
Posts: 7
Just cut the


in void insertData ? Can I do it ?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
This is probably where you have to start if you are a beginner - Oracle Java tutorials

Just cut the

Please elaborate what are you asking here.
Vieckov Vieckovsky
Greenhorn

Joined: Jul 11, 2012
Posts: 7
Ok, I modified my code a little but still don't know how to assign a name of the input to output. I create String name (method getName) but how to type instead of wynik.csv this name. I would be very grateful for any help. This is my code, I know that is a total mess... :
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
First and most importantly, get rid of all those catch blocks that don't do anything. They are inherently evil and will make it harder for you to solve any other problems. I would suggest that you should not catch a NullPointerException at all - instead, figure out why something is null, and fix that instead. For the others, if you must put in a catch statement, AT LEAST PRINT A MESSAGE TELLING YOU WHAT HAPPENED. The simplest way to do this is with exception.printStackTrace():
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Also, the random changes in indentation in your code make it unnecessarily hard to read - I don't even want to figure out what it's doing. Code indentation should not jump around randomly. It should be consistent, indenting more each time you introduce a new {, and less each time you use a }.
Vieckov Vieckovsky
Greenhorn

Joined: Jul 11, 2012
Posts: 7
Ok, NullPointer is gone but main problem is how to How do I get the name of the input to output. I tried to replace "wynik.csv" to name. Something like this

But I can't do this.
Here is without NullPointer
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Copy name of file from input to output