Beispiel #1
0
        public static void WriteBytes(byte[] vs, string filePath, FileOperater operater = default)
        {
            FileOperater    fileOperater    = (operater == null) ? new FileOperater() : operater;
            FileCommandInfo fileCommandInfo = new FileCommandInfo
            {
                FileFullName = filePath,
                FileReadType = FileCommandInfo.FileCommandInfoReadType.Bytes,
                DataBytes    = vs
            };

            fileOperater.WriteFile(ref fileCommandInfo);
            fileCommandInfo.Dispose();
        }
Beispiel #2
0
        public static void WriteUTF8Text(string content, string filePath, FileOperater operater = default)
        {
            FileOperater    fileOperater    = (operater == null) ? new FileOperater() : operater;
            FileCommandInfo fileCommandInfo = new FileCommandInfo
            {
                FileFullName = filePath,
                FileReadType = FileCommandInfo.FileCommandInfoReadType.Bytes,
                DataBytes    = Encoding.UTF8.GetBytes(content)
            };

            fileOperater.WriteFile(ref fileCommandInfo);
            fileCommandInfo.Dispose();
        }
Beispiel #3
0
        public void ReadFile(ref FileCommandInfo fileInfo)
        {
            if (fileInfo == null)
            {
                return;
            }

            string fileFullName = fileInfo.FileFullName;

            IsDirectoryExistsOrCreateIt(ref fileFullName);

            if (!File.Exists(fileFullName))
            {
#if UNITY_EDITOR
                switch (fileInfo.FileReadType)
                {
                case FileCommandInfo.FileCommandInfoReadType.Chars:
                    WriteUTF8Text(string.Empty, fileFullName);
                    break;

                case FileCommandInfo.FileCommandInfoReadType.Bytes:
                    WriteBytes(new byte[] { }, fileFullName);
                    break;
                }
#else
                Debug.Log("error: File is not exists, path is ".Append(fileFullName));
                return;
#endif
            }

            using (FileStream fs = new FileStream(fileFullName, FileMode.Open))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    switch (fileInfo.FileReadType)
                    {
                    case FileCommandInfo.FileCommandInfoReadType.Chars:
                        char[] chars = new char[fs.Length];
                        br.Read(chars, 0, chars.Length);
                        fileInfo.DataChars = chars;
                        break;

                    case FileCommandInfo.FileCommandInfoReadType.Bytes:
                        byte[] bytes = new byte[fs.Length];
                        br.Read(bytes, 0, bytes.Length);
                        fileInfo.DataBytes = bytes;
                        break;
                    }
                }
            }
        }
Beispiel #4
0
        public static byte[] ReadBytes(string filePath, FileOperater operater = default)
        {
            FileOperater    fileOperater    = (operater == null) ? new FileOperater() : operater;
            FileCommandInfo fileCommandInfo = new FileCommandInfo
            {
                FileFullName = filePath,
                FileReadType = FileCommandInfo.FileCommandInfoReadType.Bytes
            };

            fileOperater.ReadFile(ref fileCommandInfo);

            byte[] bytes = fileCommandInfo.DataBytes;
            fileCommandInfo.Dispose();

            return(bytes);
        }
Beispiel #5
0
        public static string ReadUTF8Text(string filePath, FileOperater operater = null)
        {
            FileOperater    fileOperater    = (operater == null) ? new FileOperater() : operater;
            FileCommandInfo fileCommandInfo = new FileCommandInfo
            {
                FileFullName = filePath,
                FileReadType = FileCommandInfo.FileCommandInfoReadType.Bytes
            };

            fileOperater.ReadFile(ref fileCommandInfo);

            byte[] bytes  = fileCommandInfo.DataBytes;
            string result = (bytes == null) ? string.Empty : Encoding.UTF8.GetString(bytes);

            fileCommandInfo.Dispose();

            return(result);
        }
Beispiel #6
0
        /// <summary>保存数据</summary>
        public void WriteFile(ref FileCommandInfo fileInfo)
        {
            if (fileInfo == null)
            {
                return;
            }

            string fileFullName = fileInfo.FileFullName;

            IsDirectoryExistsOrCreateIt(ref fileFullName);

            using (FileStream fs = new FileStream(fileFullName, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    Debug.Log("Writing......");
                    bw.Write(fileInfo.DataBytes); //写入
                    Debug.Log("Writing Finished. File name is ".Append(fileFullName));
                    bw.Flush();                   //清空缓冲区
                    bw.Close();                   //关闭流
                }
                fs.Close();
            }
        }