jQuery in Action, 2nd edition*
The moose likes I/O and Streams and the fly likes write into a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "write into a file?" Watch "write into a file?" New topic
Author

write into a file?

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
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

Joined: Sep 11, 2006
Posts: 959

Many ways to do it. Take a look at FileOutputStream, FileWriter, PrintWriter, etc API for more information.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks and that was a good advice for a beginner.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: write into a file?
 
Similar Threads
How To Write a new Element to XML file
IllegalStateException
My Design Choices!
hashtable to file
Saving to a text file!