• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

write into a file?

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to write into a file under linux environment? The file is going to be written under /home/testing/temp
with a file as "newFile.txt"
How do I proceed?
[ April 27, 2008: Message edited by: Gopu Akraju ]
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many ways to do it. Take a look at FileOutputStream, FileWriter, PrintWriter, etc API for more information.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried as below: It works perfectly fine under windows environment.
How do I it in Linux.

Below is the code which works under windows:
outDir is from a Config file



The above code is working perfectly fine in windows and outFile gets written under c:\testing\temp with a file name for eg 2008-04-20_11-41-06.txt

In the linux machine if I change the config file as below:
But a file name becomes \home\testing\temp\2008-04-27_09-39-48.txt under \home\testing\temp directory. And tehre is an error in opening too. What is wrong with hte approach? I have not worked with Linus before. Thanks.
[ April 28, 2008: Message edited by: Gopu Akraju ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Linux, and all other Unix-like operating systems, use / instead of \

In Windows, it is illegal to have a \ or / in a file name. In Linux, you can have \ in a file name without any problems.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Writing to a file in Java is the same in Windows, Linux or any other operating system on which you can run Java; the only difference is the way in which names of files and directories are specified.

To make your program work on Windows as well as Linux, you should use the constant File.separator instead of hard-coding / or \ in your program:
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks and that was a good advice for a beginner.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:

To make your program work on Windows as well as Linux, you should use the constant File.separator instead of hard-coding / or \ in your program



Or always use a '/', which works under Windows, too. Could fail under some other arcane operating system, of course.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic