• 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

File sepearator for unix/windows problem

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your server JDK/JRE needs to be a compatible version yes.
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic