File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes File sepearator for unix/windows problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File sepearator for unix/windows problem" Watch "File sepearator for unix/windows problem" New topic
Author

File sepearator for unix/windows problem

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi,

I have following code which copies files from one directory to another.

private void backUpLogFile() {

final File dir = new File("." + File.separator + "log");
final String[] subdir = dir.list();
if (!logDirectory.equals(subdir)) {
final String nameOfArchiveDir = logDirectory;
final boolean isDircreated = new File(nameOfArchiveDir).mkdir();
LOG.info("archive directory created :" + isDircreated);
}

final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss", Locale.US);

FileInputStream sourceStream = null;
FileOutputStream targetStream = null;
FileChannel sourceFileChannel = null;
FileChannel targetFileChannel = null;

String logBackupFolder = "." + File.separator + logDirectory + File.separator + dateFormat.format(new Date());
final boolean newLogDir = new File(logBackupFolder).mkdir();
LOG.debug("Backup directory for log files created : " + newLogDir);
final File logDir = new File("." + File.separator + "log");

final File[] listOfFiles = logDir.listFiles();
try {
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
sourceStream = new FileInputStream("." + File.separator + "log" + File.separator + listOfFiles[i].getName());
targetStream = new FileOutputStream(logBackupFolder + File.separator + listOfFiles[i].getName());
sourceFileChannel = sourceStream.getChannel();
targetFileChannel = targetStream.getChannel();
final long size = sourceFileChannel.size();
sourceFileChannel.transferTo(0, size, targetFileChannel);
}
}
} catch (IOException e) {
LOG.error("Channel or stream is still in use, so cann't close." + e.getMessage());
}

For windows system it's fine but it's not working for unix/linux.

Can you people please tell me where is the problem.

Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Don't know. We would need more details about the error messages, etc. And use the CODE button; your quoted code is hard to read like that.

Also that question is too difficult for the beginner's forum. Moving
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Kayaanat,

How do you set the File Separator ? try like this

System.out.println("file separator check " +System.getProperty("file.separator"));

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi Balu Sadhasivam ,

Thanks for your quick reply.

Well my code is running fine on windows.

I tried your suggested line of code and yes, again running fine on windows.

But when i am trying to to run it from "cygwin" or linux system, then nothing is happening.

If in "logBackupFolder" i remove this date, then it's giving desired result from cygwin.

Can you please help me out.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

But when i am trying to to run it from "cygwin" or linux system, then nothing is happening.



I have not used "cygwin" , is it installed on top of Windows ? System.property usually works the way where (OS)have installed Java ...

To add , this will work perfectly fine in most used Unix flavours like solaris , AIX..


If in "logBackupFolder" i remove this date, then it's giving desired result from cygwin.


Does it mean it works.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Balu Sadhasivam wrote:I have not used "cygwin" , is it installed on top of Windows ? System.property usually works the way where (OS)have installed Java ...

Cygwin is indeed a sort of Unix emulator that runs in Windows, and that may be the problem.

Kayanaat, what is the output of My guess is that the system properties, including the file separator, are set from your actual operating system, which is Windows. And that of course means that File.separator, which is (indirectly) set from the "file.separator" system property, will still be \.


If you want to do proper testing on a Linux environment, I suggest using a virtual machine. VMware Player is 100% free, and using VMX Builder you can create your own virtual machines. Then install Linux (e.g. Ubuntu) on it.


Also a note about File: if you always use / it should also work. It is the path separator for Linux, but in Windows the File API will handle them just fine too.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Thank you Rob Prime.

Yes you are right.
In the code i put


and i got \.

I will try to install what you suggested and then i will tell you the result.

Once again thank you and also thanks to all those people who helped me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

As I said, if the only OS dependant piece of code is File.separator, you could simply use "/" hard coded. File will solve this for you in Windows.
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi all,

Well i tried to run my above program on linux.

On the server jdk version is : 1.4.2

As you can see that i am using "FileChannel".

I am getting exception like this--

class not found during resolving this class : Myclass name
// lots of exception..

cause by : Java Builder class : No Class Def found error..

is it the problem of using jdk version lower than 1.5?

please help me.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Your server JDK/JRE needs to be a compatible version yes.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File sepearator for unix/windows problem