Ejemplo n.º 1
0
 /// <summary>
 /// Read a file with given path and return a string array with it's entire contents.
 /// </summary>
 public static string[] ReadAllLines(string path)
 {
     if (path == null)
         throw new ArgumentNullException("path");
     var file = new JFile(path);
     if (!file.Exists() || !file.IsFile())
         throw new FileNotFoundException(path);
     if (!file.CanRead())
         throw new UnauthorizedAccessException(path);
     var reader = new BufferedReader(new FileReader(file));
     try
     {
         var list = new ArrayList<string>();
         string line;
          while ((line = reader.ReadLine()) != null)
          {
              list.Add(line);
          }
         return list.ToArray(new string[list.Count]);
     }
     finally
     {
         reader.Close();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Write all lines to a file with given path.
        /// </summary>
        public static void WriteAllLines(string path, IEnumerable <string> lines)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (lines == null)
            {
                throw new ArgumentNullException("lines");
            }
            var file   = new JFile(path);
            var writer = new FileWriter(file);

            try
            {
                var nl = JSystem.GetProperty("line.separator");
                foreach (var line in lines)
                {
                    if (line != null)
                    {
                        writer.Write(line, 0, line.Length);
                    }
                    writer.Write(nl, 0, nl.Length);
                }
            }
            finally
            {
                writer.Close();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read a file with given path and return a byte-array with it's entire contents.
        /// </summary>
        public static byte[] ReadAllBytes(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            var file = new JFile(path);

            if (!file.Exists() || !file.IsFile())
            {
                throw new FileNotFoundException(path);
            }
            if (!file.CanRead())
            {
                throw new UnauthorizedAccessException(path);
            }
            var stream = new FileInputStream(file);

            try
            {
                var array = new byte[file.Length()];
                stream.Read(array, 0, array.Length);
                return(array);
            }
            finally
            {
                stream.Close();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Read a file with given path and return a string array with it's entire contents.
        /// </summary>
        public static string[] ReadAllLines(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            var file = new JFile(path);

            if (!file.Exists() || !file.IsFile())
            {
                throw new FileNotFoundException(path);
            }
            if (!file.CanRead())
            {
                throw new UnauthorizedAccessException(path);
            }
            var reader = new BufferedReader(new FileReader(file));

            try
            {
                var    list = new ArrayList <string>();
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    list.Add(line);
                }
                return(list.ToArray(new string[list.Count]));
            }
            finally
            {
                reader.Close();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Read a file with given path and return a string with it's entire contents.
        /// </summary>
        public static string ReadAllText(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            var file = new JFile(path);

            if (!file.Exists() || !file.IsFile())
            {
                throw new FileNotFoundException(path);
            }
            if (!file.CanRead())
            {
                throw new UnauthorizedAccessException(path);
            }
            var reader = new FileReader(file);

            try
            {
                var array  = new char[4096];
                var buffer = new StringBuffer();
                int len;
                while ((len = reader.Read(array, 0, array.Length)) > 0)
                {
                    buffer.Append(array, 0, len);
                }
                return(buffer.ToString());
            }
            finally
            {
                reader.Close();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Delete the file with the given path.
        /// </summary>
        public static void Delete(string path)
        {
            var file = new JFile(path);

            if (file.IsFile())
            {
                file.Delete();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Open a random access file for the given path, more and access.
        /// </summary>
        private static RandomAccessFile Open(string path, FileMode mode, FileAccess access)
        {
            var file = new Java.Io.File(path);

            switch (mode)
            {
            case FileMode.CreateNew:
                if (file.Exists())
                {
                    throw new IOException("File already exists");
                }
                break;

            case FileMode.Open:
                if (!file.Exists())
                {
                    throw new FileNotFoundException(path);
                }
                break;

            case FileMode.Append:
                access = FileAccess.Write;
                break;
            }

            switch (mode)
            {
            case FileMode.Create:
            case FileMode.CreateNew:
            case FileMode.OpenOrCreate:
                if (access == FileAccess.Read)
                {
                    //create empty file, so it can be opened again with read only right,
                    //otherwise an FilNotFoundException is thrown.
                    var additinalAccessFile = new RandomAccessFile(file, "rw");
                }
                break;
            }

            var jMode            = (access == FileAccess.Read) ? "r" : "rw";
            var randomAccessFile = new RandomAccessFile(file, jMode);

            switch (mode)
            {
            case FileMode.Truncate:
                randomAccessFile.SetLength(0);
                break;

            case FileMode.Append:
                randomAccessFile.Seek(randomAccessFile.Length());
                break;
            }

            return(randomAccessFile);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Read a file with given path and return a byte-array with it's entire contents.
 /// </summary>
 public static byte[] ReadAllBytes(string path)
 {
     if (path == null)
         throw new ArgumentNullException("path");
     var file = new JFile(path);
     if (!file.Exists() || !file.IsFile())
         throw new FileNotFoundException(path);
     if (!file.CanRead())
         throw new UnauthorizedAccessException(path);
     var stream = new FileInputStream(file);
     try
     {
         var array = new byte[file.Length()];
         stream.Read(array, 0, array.Length);
         return array;
     }
     finally
     {
         stream.Close();
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Write all given bytes to a file with given path.
        /// </summary>
        public static void WriteAllBytes(string path, byte[] bytes)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            var file   = new JFile(path);
            var stream = new FileOutputStream(file);

            try
            {
                stream.Write(bytes, 0, bytes.Length);
            }
            finally
            {
                stream.Close();
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Write all given text to a file with given path.
        /// </summary>
        public static void WriteAllText(string path, string text)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            var file   = new JFile(path);
            var writer = new FileWriter(file);

            try
            {
                writer.Write(text, 0, text.Length);
            }
            finally
            {
                writer.Close();
            }
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Read a file with given path and return a string with it's entire contents.
 /// </summary>
 public static string ReadAllText(string path)
 {
     if (path == null)
         throw new ArgumentNullException("path");
     var file = new JFile(path);
     if (!file.Exists() || !file.IsFile())
         throw new FileNotFoundException(path);
     if (!file.CanRead())
         throw new UnauthorizedAccessException(path);
     var reader = new FileReader(file);
     try
     {
         var array = new char[4096];
         var buffer = new StringBuffer();
         int len;
         while ((len = reader.Read(array, 0, array.Length)) > 0)
         {
             buffer.Append(array, 0, len);
         }
         return buffer.ToString();
     }
     finally
     {
         reader.Close();
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Open a random access file for the given path, more and access.
        /// </summary>
        private static RandomAccessFile Open(string path, FileMode mode, FileAccess access)
        {
            var file = new Java.Io.File(path);
            switch (mode)
            {
                case FileMode.CreateNew:
                    if (file.Exists())
                        throw new IOException("File already exists");
                    break;
                case FileMode.Open:
                    if (!file.Exists())
                        throw new FileNotFoundException(path);
                    break;
                case FileMode.Append:
                    access = FileAccess.Write;
                    break;
            }

            switch (mode)
            {
                case FileMode.Create:
                case FileMode.CreateNew:
                case FileMode.OpenOrCreate:
                    if (access == FileAccess.Read)
                    {
                        //create empty file, so it can be opened again with read only right, 
                        //otherwise an FilNotFoundException is thrown.
                        var additinalAccessFile = new RandomAccessFile(file, "rw");
                    }
                    break;
            }

            var jMode = (access == FileAccess.Read) ? "r" : "rw";
            var randomAccessFile = new RandomAccessFile(file, jMode);
            switch (mode)
            {
                case FileMode.Truncate:
                    randomAccessFile.SetLength(0);
                    break;
                case FileMode.Append:
                    randomAccessFile.Seek(randomAccessFile.Length());
                    break;
            }

            return randomAccessFile;
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Write all lines to a file with given path.
 /// </summary>
 public static void WriteAllLines(string path, IEnumerable<string> lines)
 {
     if (path == null)
         throw new ArgumentNullException("path");
     if (lines == null)
         throw new ArgumentNullException("lines");
     var file = new JFile(path);
     var writer = new FileWriter(file);
     try
     {
         var nl = JSystem.GetProperty("line.separator");
         foreach (var line in lines)
         {
             if (line != null)
                 writer.Write(line, 0, line.Length);
             writer.Write(nl, 0, nl.Length);
         }
     }
     finally
     {
         writer.Close();
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Write all given text to a file with given path.
 /// </summary>
 public static void WriteAllText(string path, string text)
 {
     if (path == null)
         throw new ArgumentNullException("path");
     if (text == null)
         throw new ArgumentNullException("text");
     var file = new JFile(path);
     var writer = new FileWriter(file);
     try
     {
         writer.Write(text, 0, text.Length);
     }
     finally
     {
         writer.Close();
     }
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Does a file with given path exist on the filesystem?
        /// </summary>
        public static bool Exists(string path)
        {
            var file = new JFile(path);

            return(file.IsFile() && file.Exists());
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Delete the file with the given path.
 /// </summary>
 public static void Delete(string path)
 {
     var file = new JFile(path);
     if (file.IsFile())
         file.Delete();
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Does a file with given path exist on the filesystem?
 /// </summary>
 public static bool Exists(string path)
 {
     var file = new JFile(path);
     return file.IsFile() && file.Exists();
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Write all given bytes to a file with given path.
 /// </summary>
 public static void WriteAllBytes(string path, byte[] bytes)
 {
     if (path == null)
         throw new ArgumentNullException("path");
     if (bytes == null)
         throw new ArgumentNullException("bytes");
     var file = new JFile(path);
     var stream = new FileOutputStream(file);
     try
     {
         stream.Write(bytes, 0, bytes.Length);
     }
     finally
     {
         stream.Close();
     }
 }