How do I move a file from one location to another in Java?

myFile.renameTo(new File(“/the/new/place/newName.file”)); File#renameTo does that (it can not only rename, but also move between directories, at least on the same file system). Renames the file denoted by this abstract pathname. Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem … Read more

how to read all files inside particular folder

using System.IO; … foreach (string file in Directory.EnumerateFiles(folderPath, “*.xml”)) { string contents = File.ReadAllText(file); } Note the above uses a .NET 4.0 feature; in previous versions replace EnumerateFiles with GetFiles). Also, replace File.ReadAllText with your preferred way of reading xml files – perhaps XDocument, XmlDocument or an XmlReader.

How to RSYNC a single file?

You do it the same way as you would a directory, but you specify the full path to the filename as the source. In your example: rsync -avz –progress /var/www/public_html/.htaccess root@<remote-ip>:/var/www/public_html/ As mentioned in the comments: since -a includes recurse, one little typo can make it kick off a full directory tree transfer, so a … Read more

Copy multiple files in Python

You can use os.listdir() to get the files in the source directory, os.path.isfile() to see if they are regular files (including symbolic links on *nix systems), and shutil.copy to do the copying. The following code copies only the regular files from the source directory into the destination directory (I’m assuming you don’t want any sub-directories … Read more

Define all functions in one .R file, call them from another .R file. How, if possible?

You can call source(“abc.R”) followed by source(“xyz.R”) (assuming that both these files are in your current working directory. If abc.R is: fooABC <- function(x) { k <- x+1 return(k) } and xyz.R is: fooXYZ <- function(x) { k <- fooABC(x)+1 return(k) } then this will work: > source(“abc.R”) > source(“xyz.R”) > fooXYZ(3) [1] 5 > … Read more

How do I wrap a string in a file in Python?

For Python 2.x, use the StringIO module. For example: >>> from cStringIO import StringIO >>> f = StringIO(‘foo’) >>> f.read() ‘foo’ I use cStringIO (which is faster), but note that it doesn’t accept Unicode strings that cannot be encoded as plain ASCII strings. (You can switch to StringIO by changing “from cStringIO” to “from StringIO”.) … Read more

How can I detect if a file is binary (non-text) in Python?

Yet another method based on file(1) behavior: >>> textchars = bytearray({7,8,9,10,12,13,27} | set(range(0x20, 0x100)) – {0x7f}) >>> is_binary_string = lambda bytes: bool(bytes.translate(None, textchars)) Example: >>> is_binary_string(open(‘/usr/bin/python’, ‘rb’).read(1024)) True >>> is_binary_string(open(‘/usr/bin/dh_python3’, ‘rb’).read(1024)) False