• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

write into a file?

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • 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 ]
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks and that was a good advice for a beginner.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic