• Post Reply Bookmark Topic Watch Topic
  • New Topic

Move file  RSS feed

 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to process list of files in unix server using my Java program. my question is  how to move a file from one location to another location when their is an exception in the file at runtime  and proceed further with next file.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you understand what is the exception about and you know how to deal with it. So, the idea how to move on when it happens:
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it is a legacy code. If there is any exception we need to move the file at that time itself then only the rest of the process will resume. Next file processing will resume
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. Actually it is a legacy code. If there is any exception we need to move the file at that time itself then only the rest of the process will resume. Next file processing will resume.
Please let me know your thoughts.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what kind of extra thoughts you're expecting. I just showed above one simple way of doing it and that would do exactly what you described, well, you still need to add code line to copy the file from location x to y. For that you can look for a class Files in Java API and there is possibly useful method for you, which reads as move(Path source, Path target, CopyOption... options).
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what kind of exception i may get in the exception block. Can i call the method which will move the file in exception black ??
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Sankar wrote:Not sure what kind of exception i may get in the exception block. Can i call the method which will move the file in exception black ??
1. Shouldn't be that way that you don't know what likely can happen. That is not good. But if we were to talk about such scenario for any reasons, then you can specify most general exception type, which is Exception in the catch block, that means catch block will take control for any occurred exception.
2. You don't call method to move file to exception block (which is called catch block). When the exception being thrown, your code handler will be there automagically if I can express myself that way. Then you just need to move that file to whenever location you want.

Worth to note that we're talking in very vague scenarios. I have no clue what kind of exceptions there might be as well as situations in general. It seems you don't know too. That is why probably nobody else participate in this thread as here is little to participate without knowing more about the task.

Need more details.
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Step 1 - its my standalone application which will be deployed into Unix server.
Step 2 - Every file will be processed and data will updated in the database based on the file format.
Step 3 - Due to bad data or length issue and so on ....the process will fail.
Step 4 - at that time i need to move the file into another location in unix server and process the next file.
Step 5- I will fix the error in the file and re drop the file for processing.

I need to right the code for this scenario. Please let me know if you need any more information.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to right the code for this scenario. Please let me know if you need any more information.
Idea is clear. So my described approach could work.

1. Read the directory where the files are
2. Iterate over the files
3. Read files one by one
3.1. Do whatever you need to do with that file
3.1.1 In case something goes wrong due to your described possible issues - move the file from x -> y location
4. Go back to 3
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. Read files one by one
3.1. Do whatever you need to do with that file
3.1.1 In case something goes wrong due to your described possible issues - move the file from x -> y location

In this point (3.1.1) if exception happens it will stop the process right ? how could i handle that ?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is of course very small part of your application. I imagine there are other parts in your application for data fields mappings, normalization, records standardization.
If you'd take further, not all files you'd need to discard right away, some of those files fields could be fixed on the fly before insertion to db. But that would require to invest way more time at the beginning instead of fixing files manually.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run that code snippet on your machine and make sure you understand it. Tell us if you get an idea what is happening there.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you talk about "the process fails", what do you mean?
Is this the app that fails?

Are you able to change the app?

I'm trying to get an idea of what it is you can change, and what it is you can't.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I understood OP is saying that exception could be thrown during file read due to data inconsistency as different amount of delimiters per record, other similar kind of issues. Basically data related issues, but nothing above that.

But yeah, you're right - let OP clarify it if you raised that question.
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my sample code.
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:When you talk about "the process fails", what do you mean?
Is this the app that fails?

Are you able to change the app?

I'm trying to get an idea of what it is you can change, and what it is you can't.



basically i am calling the java thread (file process method ) from unix server for ever 2 mins . while processing the file if there is any exception happens then the if i didn't move the file from that place again it will fail at the same point.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Log whatever you want to log in the read() method's catch block and then re-throw exception so you could handle it one level above, where you'd move the file and afterwards would call read() method again by supplying other file as an input.
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Log whatever you want to log in the read() method's catch block and then re-throw exception so you could handle it one level above, where you'd move the file and afterwards would call read() method again by supplying other file as an input.



catch(Exception e)
{
Logger.log(Logger.RECEIVER,"Problem with reading payment file" + e,"");
throw new ProcessTaskException("Problem with reading payment file",e);
}

But still it is stopping the process. whatever the reason exception arise i need to move the file from that place and continue the process.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you run the code snipped I posted twice and understood how it works?

Where I marked '1', there should be your read() method call. Where I marked '3' there should be file 'moving' logic. Ignore '2'.
From the read() method itself log only the details about the file internals and re-throw an exception.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example:
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:An example:



Sorry i forgot to post the reply for your comment. I have executed with your code. It is not throwing any exception , at the same time it is not processing the next file too.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to go back to basics. I'll copy your topic to Beginning Java. I hope you're fine with that. All this app is about the loop and handling exception when it occurs. There (in Beginning Java forum) you'll get more responses probably.
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:You need to go back to basics. I'll copy your topic to Beginning Java. I hope you're fine with that. All this app is about the loop and handling exception when it occurs. There (in Beginning Java forum) you'll get more responses probably.


I cant post exact code here. Its data valuation. I hope we are not in the same page.

really appreciate your effort  thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Sankar wrote:I hope we are not in the same page.

That's not a good thing; usually people hope for the opposite.

To "be on the same page" means that you and whoever you are talking to have the same understanding. If you and the other person(s) are not on the same page, then that means that there is still some confusion/disagreement/misunderstanding between parties.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still a bit confused about the actual issue.

You say this is legacy code.
As I asked before, are you able to modify the code to do what you want?

Normally when people say "this is legacy code" it means they can't actually touch it, usually for fear they'll break it.

And I still don't quite understand your use of the term "process".
Is that just a method in this legacy code, or is it a Process, as in something launched on the underlying OS?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Sankar wrote:
Sorry i forgot to post the reply for your comment. I have executed with your code. It is not throwing any exception , at the same time it is not processing the next file too.


Yeah, considering that Liutauras mentioned "example" in the response, that you never told us what the processing is, that the code provided is really short, and that the whole extent of the processing of the file is this line ...
I think that it is safe to say that the code, was an example as part of a discussion. And even perhaps, some cut-n-paste-able, that you can add to your program. Did you really run it to see if it would process your file?

Henry
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Vinoth Sankar wrote:I hope we are not in the same page.

That's not a good thing; usually people hope for the opposite.

To "be on the same page" means that you and whoever you are talking to have the same understanding. If you and the other person(s) are not on the same page, then that means that there is still some confusion/disagreement/misunderstanding between parties.


Yes you are correct i might be wrong. Let me try explaining more as much as i can.



This method will be called every 3 mins in Unix server. when ever we exception arise at any where i need to move that file in another location in Unix server itself and proceed with the next file processing.

please let me know if you need any more information.


[HENRY: Added Code Tags]
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Vinoth Sankar wrote:
Sorry i forgot to post the reply for your comment. I have executed with your code. It is not throwing any exception , at the same time it is not processing the next file too.


Yeah, considering that Liutauras mentioned "example" in the response, that you never told us what the processing is, that the code provided is really short, and that the whole extent of the processing of the file is this line ...
I think that it is safe to say that the code, was an example as part of a discussion. And even perhaps, some cut-n-paste-able, that you can add to your program. Did you really run it to see if it would process your file?

Henry


File will be proceed every row (content validation ) then i will insert into DB.
 
Vinoth Sankar
Greenhorn
Posts: 18
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Sankar wrote:
Henry Wong wrote:
Vinoth Sankar wrote:
Sorry i forgot to post the reply for your comment. I have executed with your code. It is not throwing any exception , at the same time it is not processing the next file too.


Yeah, considering that Liutauras mentioned "example" in the response, that you never told us what the processing is, that the code provided is really short, and that the whole extent of the processing of the file is this line ...
I think that it is safe to say that the code, was an example as part of a discussion. And even perhaps, some cut-n-paste-able, that you can add to your program. Did you really run it to see if it would process your file?

Henry


File will be proceed every row (content validation ) then i will insert into DB.
.

Yes i have tried that approach. Its not working i might be missing something .
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Sankar wrote:please let me know if you need any more information.

Sorry mate, you give us some bits and pieces (if we can call them like that) and asking if we got enough information. Enough information for what? I'm too puzzled now as do not understand what you're expecting from us.

Don't incorporate where all this bit runs - UNIX, Windows... It is irrelevant here.

Start by telling us relevant details. I might will need to move this topic back to Java in General by looking to some of the keywords you revealed, but probably that is not important for now as I still believe the problems you're struggling with are from easier part of the Java.

You have said you can't show the files content - that is fine, it isn't relevant here too. Show us the code you have by now and lets start from there.

As far as I understand (my understanding about your current problem still remains the same as before):
1. You're having files in location x
2. You want to go through location x
3. Read the files one by one
4. "Process" them, I have putted in quotes as we don't know what you mean by that
5. In case processing part fails due to data (presumably delimited raw data files as *.txt; *.csv; ...) records inconsistencies and since your program works on a fixed file format only and it is not configurable during data source set up, so it can fail at unpredictable times which you're not aware of
5.1. If/when that happens, you want to move such file to location y and continue on other file
6. Repeat from 3

Is it what we are talking about here? If it is - don't tell us anything about database and unix, these are different parts and possibly tasks as insertion of processed records to db and other stuff we don't know yet.

Can you show us the code what you have by now? That might would be easier for everybody to understand what it actually does and what is likely missing there.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Sankar wrote:... my question is  how to move a file from one location to another location ...

I see lots of discussion, but nothing specifically about how to move a file.  Is that what you need help with?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:... 3. Read the files one by one

I don't see a list or array of files nor a loop to process them one by one. Is this list/loop inside the execute()?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Liutauras Vilda wrote:... 3. Read the files one by one

I don't see a list or array of files nor a loop to process them one by one. Is this list/loop inside the execute()?

Reading the comment next to it "// this method will internally call the read method" there should be something what iterates over the directory and reads one file at a time. But probably only OP can answer that.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have expected something more like this:

If this loop is inside of execute() you're out of luck.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I would have expected something more like this

I understood that OP needs to move file only if the exception being thrown during file parsing.
In different words - to move file out from directory x when the file is not valid in terms of structure or similar such kind of issue.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was having in mind this, what OP wants (slightly amended Carey's version):
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was assuming that execute() was invoking some UNIX (non-Java?) process and that my loop might have been inside this UNIX process where Java is unable to intervene.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking about this topic all the way back home. Probably I've gone too far with suggestions as these thinking now could have been more of a speculation. It appears we don't know anything about the app in general and what it is about.

Dave was right asking at the beginning if OP is able to modify the current app in the first place? (OP hasn't answered yet).

Vinoth Sankar, could you tell a bit about the app itself, what this app is meant to do in general? Process some kind of incoming sales data in real time? Any kind other data processing? Is there any client side? Why the data processing happens on unix, do you have access to it? Is UNIX used just for stability purpose for processing, are there any other OS based processes are involved in the app?

Vinoth Sankar wrote:This method will be called every 3 mins in Unix server
Why 3 minutes? How you know that method run finishes in 3 minutes?
Vinoth Sankar wrote:i am calling the java thread (file process method ) from unix server for ever 2 mins
Are these two the same or different methods? (here and above mentioned) One is called every 3 minutes, other every 2 minutes..
Vinoth Sankar wrote:when ever we exception arise at any where i need to move that file in another location in Unix server itself and proceed with the next file processing.
Once you read file once successfully, what happens with a file in location x? The next time method read is called, how will it know the file was read already and don't need to read it again, or need to read again?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!