• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a file on Linux using Java code  RSS feed

 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a file on Linux machines using Java code.

Using the code I am able to create a file on Windows but the jar does not work on Linux platform.

Cant we create a flat file on Linux using the same code ?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more; we can only help if we know all the details. There should not be any difference between creating a file on Windows® and on Linux; the PATHs have a different format, obviously. Tell us how you are trying to create such a file. Also find out whether the location you are trying to create it is accessible for writing or not.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mandar Khire
Ranch Hand
Posts: 588
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
the jar does not work on Linux platform.

Does Linux machine configured properly to run simple java code/jar?

Thanks Campbell Ritchie for clarifying about Path.

Thanks Santosh Kumar Nayak for sharing small piece of code.

I write following code with the help of Google search:-

Might it helpful for newbies!
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linux machine is configured properly but the above code is not able to generate a *.txt file on Linux Box.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are the permissions on the directory set to allow writing to the dir?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/root/Desktop???

That is most unlikely to be accessible for writing. I suggest you change /root to a tilde and try writing it on your desktop ~/desktop/...
Please confirm whether it is desktop or Desktop; it matters on Linux.

That code needs a bit of editing. You don't need the two try blocks; apart from the names of the files they are identical. you should also close the writers in a finally (or use try‑with‑resources).
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the "tilde" thing is a shell artifact and it won't work properly within Java program code. However there's a java.lang.System Property that returns the path of the current user's home directory. It can be used to construct an appropriate File object. Something similar to:



Note, by the way that that particular code works equally well regardless of whether it's running on Windows or on Linux.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!