0

I am using this code to move files in row in my C# application.

public static bool IsFileReady(String sFilename)
{
    try
    {
        using (FileStream inputStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            if (inputStream.Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    catch (Exception)
    {
        return false;
    }
}

And to use it:

while (Checker.bFileIsFileReady(sFilename))
{
    //Do work here
    break;
}

Can you please tell me, how I can translate this code to java? Right now, my java application works with Thread.Sleep()...

kennyzx
  • 12,845
  • 6
  • 39
  • 83
StefanS
  • 211
  • 2
  • 3
  • 12
  • possible duplicate of [How do I check if a file exists? (Java on Windows)](http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-java-on-windows) – APerson Dec 23 '14 at 16:09
  • I want to check if file was completely moved. checking for existence is not the problem – StefanS Dec 23 '14 at 16:17
  • To see if it's moved, wouldn't you check if the file is in the new location and out of the old location? There is no way to look at a File object and see if it was once moved before. – Ascalonian Dec 23 '14 at 16:46
  • It should be checked, if the moved file in the new location is completely on the disc and e.g. writtable – StefanS Dec 23 '14 at 17:01
  • Please check if [that](http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java) question helps you. Regards – Willz Dec 23 '14 at 17:16
  • I don't know. The FileChannels look like they are only to copy files. – StefanS Dec 23 '14 at 18:59
  • possible duplicate of [How to find out if a file exists in C# / .NET?](http://stackoverflow.com/questions/38960/how-to-find-out-if-a-file-exists-in-c-sharp-net) – abatishchev Dec 24 '14 at 05:45

1 Answers1

0

if you want to copy file, you need input and output stream. but if you just want to move it, you can use File.renameTo

File from = new File(oldFile);
File to = new File(newFileName);
from.renameTo(to);
Adem
  • 9,402
  • 9
  • 43
  • 58