每天一点小知识001--unity在ios系统的文件读取

通过http://blog.youkuaiyun.com/joyhen/article/details/8572094这个博客修改的代码,unity在ios的文件操作必须通过流来读取写入。否则文件放在沙盒位置也是读不出来的

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;

public class FileHandle {

private FileHandle(){}
public static readonly FileHandle instance = new FileHandle();

//the filepath if there is a file
public bool isExistFile(string filepath){
return File.Exists(filepath);
}

public bool IsExistDirectory(string directorypath){
return Directory.Exists(directorypath);
}

public bool Contains(string Path,string seachpattern){
try{
string[] fileNames = GetFilenNames(Path,seachpattern,false);
return fileNames.Length!=0;
}
catch{
return false;
}
}

//return a file all rows
public static int GetLineCount(string filepath){
string[] rows = File.ReadAllLines(filepath);
return rows.Length;
}

public bool CreateFile(string filepath){
try{
if(!isExistFile(filepath)){
StreamWriter sw;
FileInfo file = new FileInfo(filepath);
//FileStream fs = file.Create();
//fs.Close();
sw = file.CreateText();
sw.Close();
}
}
catch{
return false;
}
return true;
}

public string[] GetFilenNames(string directorypath,string searchpattern,bool isSearchChild){
if(!IsExistDirectory(directorypath)){
throw new FileNotFoundException();
}
try{

return Directory.GetFiles(directorypath,searchpattern,isSearchChild?SearchOption.AllDirectories:SearchOption.TopDirectoryOnly);
}
catch{
return null;
}

}

public void WriteText(string filepath,string content)
{
//File.WriteAllText(filepath,content);
FileStream fs = new FileStream(filepath,FileMode.Append);
StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
sw.WriteLine(content);
sw.Close();
fs.Close();
Debug.Log("write: filepath: "+filepath);
Debug.Log("write: content: "+content);
Debug.Log("写入完毕");
}

public void AppendText(string filepath,string content){
File.AppendAllText(filepath,content);
}

public string FileToString(string filepath,Encoding encoding){
FileStream fs = new FileStream(filepath,FileMode.Open,FileAccess.Read);
StreamReader reader = new StreamReader(fs,encoding);
try{
return reader.ReadToEnd();
}
catch{
return string.Empty;
}
finally{
fs.Close();
reader.Close();
Debug.Log("读取完毕");
}
}

public void ClearFile(string filepath){
File.Delete(filepath);
CreateFile(filepath);
}

}

 

转载于:https://www.cnblogs.com/AXIA-zy/p/5101467.html

### Spring Boot 整合 MyBatis不到数据库的原因分析 在 Spring Boot 中整合 MyBatis 并配置数据库连接时,如果遇到无法到数据库的情况,可能是由于以下几个原因引起的: #### 1. **数据源配置错误** 如果 `application.properties` 或 `application.yml` 文件中的数据源配置项填写错误,则可能导致程序无法正确连接到目标数据库。常见的错误包括但不限于: - 数据库 URL 错误(例如拼写错误或端口号不对)[^2]。 - 用户名或密码错误。 正确的数据源配置应类似于以下形式: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` #### 2. **依赖版本冲突** 如果项目中引入的 MyBatisSpring Boot 的版本不兼容,可能会导致初始化失败或者无法正常加载数据库资源。建议检查项目的 `pom.xml` 文件,确保使用的依赖版本匹配。例如: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.0</version> </dependency> ``` #### 3. **未启用 Mapper 接口扫描** 如果没有正确配置 `@MapperScan` 注解来指定 Mapper 接口所在的包路径,MyBatis 将无法识别这些接口,从而引发异常。可以通过以下方式解决此问题[^3]: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际的 mapper 路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 4. **数据库服务不可用** 当本地开发环境下的 MySQL 或其他数据库服务未启动时,也会报出类似的 “不到数据库” 错误。可以尝试通过命令行或其他工具手动测试数据库连通性,确认其状态是否正常运行。 #### 5. **IDEA 缺乏 SQL 提示支持** 若开发者希望获得更友好的编码体验,在 IDEA 上完成相应设置后能够享受智能补全功能带来的便利。具体做法可参照文档说明调整插件选项以便增强用户体验效果[^4]。 --- ### 解决方案总结 针对上述可能存在的各类状况逐一排查即可定位根本所在;同时注意保持各组件间良好的协作关系以减少不必要的麻烦发生几率。 ```java // 示例代码片段展示如何定义一个简单的 DAO 层类 public interface UserMapper extends BaseMapper<UserEntity> {} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值