how to use org.apache.commons.io.FileUtils

1,626 views
Skip to first unread message

Michael Shields

unread,
Aug 1, 2013, 4:52:48 PM8/1/13
to scala-user

I’m still very new to scala and I’m trying to copy a word document from one directory to another. I did some research online and could find nothing on how to do this in scala. I found a couple of comments in 2009 that said something to like “A comprehensive IO library is being developed in the scala incubator” and since it’s now 2013 I thought I’d find something but didn’t. I found several recommendations for using the org.apache.commons.io.FileUtils with java, so I thought I’d try it, even though I’m not a java programmer. I want to do something like:

 

import java.io.File

import java.io.IOException

import org.apache.commons.io.FileUtils

 

val srcDocx = "Template.docx"

val destDocx = new File("August.docx")

 

try {

FileUtils.copyDirectory(srcDocx, destDocx);

}

catch (IOException e) {

                println("Failed to copy August.docx")

}

 

I could find nothing online about how to “install” the apache commons so that scala could find them.

 

Is there a better way to do this with scala than using the apache commons io fileutilities?

If not, can someone point me to what I need to do so the import org.apache.commons.io.FileUtils will work?

 

Also, I’m interested in making minor changes to my template.docx (which is a MS word document) and I see that working with xml is covered further along in Scala for the impatient. I did a quick search online, but didn’f find anthing. I was wondering if anyone can point me to some pointers there. I just want to do simple things like string replacement.

 

Thanks,

Mike Shields

 

 

 

 

 

Vlad Patryshev

unread,
Aug 1, 2013, 5:14:31 PM8/1/13
to Michael Shields, scala-user
The solution depends on whether you use mvn or sbt... or something else.

(Additionally, I can share my opinion that FileUtils is not a very high quality software... it may make sense not to use it at all.)

Thanks,
-Vlad


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Shields

unread,
Aug 1, 2013, 5:19:10 PM8/1/13
to Vlad Patryshev, scala-user

Vlad,

 

I’m not using either yet since I’m just writing scala scripts and simple single file applications. I will use sbt eventually, but I’m trying to learn scala first.

 

Mike

Rodrigo Cano

unread,
Aug 1, 2013, 5:35:26 PM8/1/13
to Michael Shields, Vlad Patryshev, scala-user
If you are using java 7, you have java.nio.file.Files.move(...). The Files api has all sort of file system related operations. Look into that.

Vlad Patryshev

unread,
Aug 1, 2013, 6:09:53 PM8/1/13
to Michael Shields, scala-user
Oh, scripts... so you need to have the jars accessible in the classpath, I think.

Thanks,
-Vlad

Michael Shields

unread,
Aug 1, 2013, 6:45:07 PM8/1/13
to Rodrigo Cano, Vlad Patryshev, scala-user

Rodrigo,

 

Thanks for the tip. I looked at the java.nio.file api and it looks like the copy method is what I need, but I can’t get it to work. Since I’m not coming to Scala from java, it’s probably something stupid, but don’t know how to cast a string into a path. Here is what I tried:

 

val srcDocx : Path = "Template.docx"

val destDocx : Path = "August.docx"

 

java.nio.file.copy(srcDocx,destDocx)

 

Thanks,

Mike

Rodrigo Cano

unread,
Aug 1, 2013, 6:52:12 PM8/1/13
to Michael Shields, Vlad Patryshev, scala-user
Ok, that is because they cannot be casted, instead you need to instantiate a Path object. To instantiate a Path for the default file system you should use the java.nio.file.Paths.get(...) factory method, like:

val srcDocs = java.nio.file.Paths.get("some", "path", "to", "your", "file", "Template.docx")

Also, I noticed that you used java.nio.file.copy(src, dest) when the correct object name is java.nio.file.Files.copy(src, dest)

Cheers.

Michael Shields

unread,
Aug 1, 2013, 6:56:24 PM8/1/13
to scala-user

In trying to find a solution to this, I ran across using sys.process._ to run system commands so I tried

import sys.process._

val srcDocx = "Template.docx"

val destDocx = "August.docx"

val result = ("copy " + srcDocx + " " + destDocx)!

 

But this doesn’t seem to work. In messing around in the REPL on windows 7 I can’t get any command to work and I get an error each time that the file can’t be found where the file is the command. When I make a script and use the –feature command, I get the warning that I need to enable the postfix operator !:

 

C:\scala\LearningScala\test.scala:9: warning: postfix operator ! should be enabled by making the implicit value scala.language.postfixOps visible. This can be achieved by adding the import clause 'import scala.language.postfixOps'

So I added that import to the script, but then I get an error that ! is not a member of String.

 

Any hints on what to do to execute system commands on windows 7?

 

Thanks,

Mike

Rodrigo Cano

unread,
Aug 1, 2013, 7:43:02 PM8/1/13
to Michael Shields, scala-user
I'm not a windows user, so I can't help you with your system commands. I can help with the error, which is not an error, but a warning, stating exactly what it says, that postfix operator should be explicitly enabled (although this won't stop them from working, but will issue a warning).

In your case the postfix operator that you are using is !. To use it "properly", you should do:

val result = ("copy " + srcDocx + " " + destDocx).!

(Notice the period there, denoting method invocation)

Nevertheless, I can't help you with windows commands, but it seems you are not familiar at all with scala and you are trying to stretch too far to make it work, which explains why you are having such difficult times. Is this needed? can't you just use a bat script?.

I'll provide here a full example that should work (I haven't tested this code right know, but I've done this before tons of times):

import java.nio.file._ //get into scope java.nio.file, so I can use Paths and Files so create Path instances and perform

val srcFile = Paths.get("Template.docx")
val dstFile = Paths.get("August.docx")

Files.copy(srcFile, dstFile)

Also notice that in order for this to run, your working directory must be the one that contains those files, you can tests that by putting a printn at the beggining of your file so you can see where you are located, like:

println(Paths.get("."))

Last point if all of this fails, its pretty easy to port code from java to scala (most of the cases you only have to change the variable declarations), so I'd suggest you google file copying with java 7 and try that too.

Cheers

Juha Heljoranta

unread,
Aug 3, 2013, 7:28:21 AM8/3/13
to scala...@googlegroups.com
Hi,

In Java 7 you can do this:
java.nio.file.Files.copy(java.nio.file.Paths.get("Template.docx"),
java.nio.file.Paths.get("August.docx"))

There is also Guava which contain lot of useful functionality. Including file
copy functionality:
https://code.google.com/p/guava-libraries/wiki/IOExplained

scala -cp path/to/guava.jar
Welcome to Scala version 2.10.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> com.google.common.io.Files.copy(new java.io.File("Template.docx"), new
java.io.File("August.docx"))


And for learning the Scala, I highly recommend Programming in Scala. See here:
http://www.scala-lang.org/documentation/books.html


Cheers,
Juha
Reply all
Reply to author
Forward
0 new messages