views:

103

answers:

2

Is there a library like Apache Commons IO for .Net?

I am looking for the following functions:

  • IOUtils.toByteArray(Stream);
  • IOUtils.toString(Stream);
  • FileUtils.write*();
  • DirectoryWalker.
A: 

functionality like that is A PART of .net. no need for a special library.

examples: http://www.xefteri.com/articles/show.cfm?id=8 http://aspnet.4guysfromrolla.com/articles/072303-1.aspx

b0x0rz
A: 

There's not a direct port, AFAIK, but Apache Commons IO includes a whole bunch of stuff:

  • Utility classes - with static methods to perform common tasks
  • Filters - various implementations of file filters
  • Comparators - various implementations of java.util.Comparator for files
  • Streams - useful stream, reader and writer implementations

Are you after something specific?

Update: for e.g. the functionality of IOUtils.toByteArray(Stream) you can use the equivalent

Stream stream;
byte[] bytes;
using (BinaryReader br = new BinaryReader(stream)) {
        bytes = br.ReadBytes(stream.Length);
}

and, of course, to get a string from the byte array, you simply need to decode it using the appropriate encoding:

String s = encoding.GetString(bytes)

where encoding is a System.Text.Encoding instance, for example System.Text.UTF8Encoding. I'm not sure of any libraries which provide the other functions you describe, but they're easy to write using TextWriter.WriteLine, BinaryWriter.Write, Directory.GetDirectories and Directory.GetFiles as in this example.

Vinay Sajip
Yeap, toByteArray(Stream), toString(Stream) from (IOUtils) and write* from FileUtils.
David Reis
I've updated the question.
David Reis