public static Task <T> DeserializerAsync <T>(Java.IO.File file) #endif { return(Task.Run(async() => { // Wait for file to be free while (FileUtils.IsFileLocked(file)) { await Task.Delay(100).ConfigureAwait(false); } Stream fStream = null; StreamReader sReader = null; JsonReader reader = null; var obj = default(T); #if __ANDROID__ var mFile = new AtomicFile(file); try { fStream = mFile.OpenRead(); #else try { fStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read); #endif sReader = new StreamReader(fStream); reader = new JsonTextReader(sReader); var serializer = JsonSerializer.Create(DefaultSettings); obj = serializer.Deserialize <T>(reader); } catch (Exception ex) { Logger.WriteLine(LoggerLevel.Error, ex, "SimpleWeather: JSONParser: error deserializing or with file"); obj = default(T); } finally { reader?.Close(); #if __ANDROID__ mFile?.Dispose(); #endif } return obj; })); }
public async static Task <String> ReadFile(File file) { using (AtomicFile mFile = new AtomicFile(file)) { // Wait for file to be free while (IsFileLocked(file)) { await Task.Delay(100); } String data; using (BufferedReader reader = new BufferedReader(new InputStreamReader(mFile.OpenRead()))) { String line = await reader.ReadLineAsync(); StringBuilder sBuilder = new StringBuilder(); while (line != null) { sBuilder.Append(line).Append("\n"); line = await reader.ReadLineAsync(); } reader.Dispose(); data = sBuilder.ToString(); } return(data); } }