• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Best way to name a file based on logged in user  RSS feed

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the proper way to generate user specific csv files so that I should be able to grab these files from the UI for a user when s/he logs back next time?

For example:

I have 3 files getting generated when a user clicks on a button in the UI :

File #1:



File #2:



File #3:



So, right now I can't differentiate which user has generated which files. Also, the files will be overwritten every time user clicks on a button. Some of the possibilities I see are as follows:

1)I have username available at the time of generating the file. So not sure if I should add the username of the user to the file? Something like FirstCsv_username.csv, SecondCsv_username.csv,ThirdCsv_username.csv ? But if the same user logs back in next time, same files will be overwritten by him.I might need timestamp in this scenario in addition to the username?


2) Should I include timestamp with the file name?

OR any other better ideas on this?

Thanks in advance !
 
Ranch Hand
Posts: 358
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only have a few people using the servuce maybe you can put it all in the file name.  It seems better to me to organize the files into folders based on username.
If you dont want the files to be overwritten then you can organize those into time folders like by date or whatever.
 
Marshal
Posts: 64089
215
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go and look through the System class, which contains “useful … methods” (sic). Some of those methods will give you properties, including user name. So you can use that name to find/create a directory to put the file in.Yes, you can create a LocalDateTime object and add its value to the file name. Do it in year‑month‑day‑hour‑minute‑second(?mllisecond) format, then the file explorer program will show the files in chronological order.
 
Campbell Ritchie
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Al Hobbs and I are thinking of similar solutions.
 
Jack Tauson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:If you only have a few people using the servuce maybe you can put it all in the file name.  It seems better to me to organize the files into folders based on username.
If you dont want the files to be overwritten then you can organize those into time folders like by date or whatever.




Ok, so something like this I believe :




where, username1, username2 and username3 are different folders.

Could you tell me if I don't know the number of users in advance, and let's say if a new user comes in and his location would be in username4 folder. Is this new folder going to get created automatically or does it needs to exist before I could attempt to save his files in his folder? thanks
 
Sheriff
Posts: 21716
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that the user that is running your program needs to have access to the C: root this way. That could not be allowed, and if it is allowed it can be a big security risk. It's better to use a different root folder. The folder per user is a good idea though.
 
Marshal
Posts: 6684
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Keep in mind that the user that is running your program needs to have access to the C: root this way.


In addition to what Rob said, there would be also a limitation on what OS your program may run. Windows presumably. Other operating systems may have different file system structure, so the organization of it would look different.
 
Sheriff
Posts: 24366
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me like putting those files in the user's home directory might be a good idea. You can find out that directory like this:

 
Campbell Ritchie
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . the user's home directory . . .

Would the pogram have access to people's home directories?
 
Al Hobbs
Ranch Hand
Posts: 358
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to check if the directory exists and create it if it doesnt exist otherwise an exception will be thrown.  Also make sure you setup read/write permissions etc on the machine.  That will also throw an exception
 
Paul Clapham
Sheriff
Posts: 24366
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . the user's home directory . . .

Would the pogram have access to people's home directories?



Of course it would. When a person signs on they automatically have access to their home directory (assuming the absence of an idiotic administrator) and so when they run the Java app, it will have access to that directory as well.
 
Jack Tauson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Sounds to me like putting those files in the user's home directory might be a good idea. You can find out that directory like this:



Thanks Paul and others. Eventually, the code is going to sit on the RHEL server, so I changed the path to    /srv/all_users/JACK/    . so, now my files are getting saved inside JACK folder. I read the Filesystem Hierarchy Standard and it says that srv is the correct folder to have these things.

One more question, What unique ID I should add to the name of the file so that each time the newest files can be accessed from the User Interface? I was thinking of adding a current timestamp and hence wondering if this is a better approach to do so?




Thanks
 
Jack Tauson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Go and look through the System class, which contains “useful … methods” (sic). Some of those methods will give you properties, including user name. So you can use that name to find/create a directory to put the file in.Yes, you can create a LocalDateTime object and add its value to the file name. Do it in year‑month‑day‑hour‑minute‑second(?mllisecond) format, then the file explorer program will show the files in chronological order.



Thanks Campbell. Eventually, the code is going to sit on the RHEL server, so I changed the path to    /srv/all_users/JACK/    . so, now my files are getting saved inside JACK folder. I read the Filesystem Hierarchy Standard and it says that srv is the correct folder to have these things.


So, are the system.class things that you mentioned going to help me in Unix file system as well, I mean on the RHEL server? Thanks
 
Campbell Ritchie
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Tauson wrote:. . . So, are the system.class things that you mentioned going to help me in Unix file system as well, I mean on the RHEL server? Thanks

Don't know, but I don't know a reason why it shouldn't work.
 
Paul Clapham
Sheriff
Posts: 24366
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Tauson wrote:One more question, What unique ID I should add to the name of the file so that each time the newest files can be accessed from the User Interface? I was thinking of adding a current timestamp and hence wondering if this is a better approach to do so?



A timestamp is a pretty good choice for a "unique" file name, and you can use a prefix for that if you like. Just a warning from personal experience: if you start producing the files fast enough then you might produce two files with the same name, which naturally is a bad thing. You might think "More than one file per second?" and choose YMD-HMS for the "unique" file name. Then the volume of files increases radically, or the speed of creating the files increases radically, and the next thing you know you've hit the duplicate-name problem. Yes, I did get burnt by that. More than once if I recall correctly. So including milliseconds in the unique file name will postpone that problem for a while longer.
 
Campbell Ritchie
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . including milliseconds in the unique file name . . .

Would it be necessary to include something smaller, e.g. nanoseconds?
 
Paul Clapham
Sheriff
Posts: 24366
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . including milliseconds in the unique file name . . .

Would it be necessary to include something smaller, e.g. nanoseconds?



As I said, it depends on how quickly you expect those files to be created.

However... there's also a static method java.nio.files.Files.createTempFile(directory, prefix, suffix) which will create a randomly-named empty file in a specified directory. Its documentation guarantees that it won't produce duplicate names, which is the requirement here. And despite its name suggesting that it creates some kind of a special "Temp" file, it in fact just creates an ordinary file which the application can then write to. So that's another option to produce a unique ID.
 
Jack Tauson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

A timestamp is a pretty good choice for a "unique" file name, and you can use a prefix for that if you like.



Here you are saying that I could use something like this for generating 3 files ? YMD-HMS_myfirstfile.csv,YMD-HMS_mysecondfile.csv, YMD-HMS_mythirdfile.csv


And I am pretty sure my files will take more than a sec to generate. Few mins at least minimum. Most of the files I am expecting could take an hour or so.
 
Jack Tauson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Tauson wrote:

Paul Clapham wrote:

A timestamp is a pretty good choice for a "unique" file name, and you can use a prefix for that if you like.



Here you are saying that I could use something like this for generating 3 files ? YMD-HMS_myfirstfile.csv,YMD-HMS_mysecondfile.csv, YMD-HMS_mythirdfile.csv


And I am pretty sure my files will take more than a sec to generate. Few mins at least minimum. Most of the files I am expecting could take an hour or so.



Also, if it's possible,  you think that I should put these files in a zip format only instead of 3 separate files? In this manner, from the UI, I would just have to specify the URL of the zip file for download.
 
Paul Clapham
Sheriff
Posts: 24366
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Tauson wrote:Here you are saying that I could use something like this for generating 3 files ? YMD-HMS_myfirstfile.csv,YMD-HMS_mysecondfile.csv, YMD-HMS_mythirdfile.csv



Well yeah, but I would probably put the timestamp at the end, like so: myfirstfile-20190318223817.csv. That way if you look at a directory listing sorted alphabetically, you see all of the "myfirstfile" files grouped together in chronological order.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!