1、logcat
分5个等级,每个等级使用不同颜色
verbose
debug
info
warn
error
定义过滤器方便查看
System.out.print输出的日志级别是info,tag是System.out
Android提供的日志输出api
Log.v(TAG, "hello");
Log.d(TAG, "hello");
Log.i(TAG, "hello");
Log.w(TAG, "hello");
Log.e(TAG, "hello");
2、在内部存储中写文件
Ram内存:运行内存,相当于电脑的内存
Rom内存:内部存储空间,相当于电脑的硬盘
sd卡:外部存储空间,相当于电脑的移动硬盘
所有安装至手机的应用都会在data/data目录下生成一个包名文件夹,这个文件夹就是内部存储的路径
应用只能在自己的包名文件夹中读写文件
例子:
//返回一个File对象,封装的路径是data/data/com.example.test/files
//File file = new File(getFilesDir(), "info.txt");
//返回一个File对象,封装的路径是data/data/com.example.test/cache
File file = new File(getCacheDir(), "info.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
//把账号密码写入本地文件
fos.write((name + "--" + pass).getBytes());//将字符串转化为字节流写到文件中
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
3、在内部存储中读文件
例子:
private void readAccount() {
//读取文件,回显数据,文件info.txt的内容是 test--123
//File file = new File("data/data/com.example.test/info.txt"); //这个会保存在应用的目录files文件夹下,就算空间不够也不会删除,除非用户自己清理
File file = new File(getFilesDir(), "info.txt");//这个会保存在应用的目录files文件夹下,就算空间不够也不会删除,除非用户自己清理
//File file = new File(getCacheDir(), "info.txt");//这个会保存在应用的目录caches文件夹下,如果空间不够就会被清除掉
if(file.exists()){//判断文件是否存在
try {
FileInputStream fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读取文件中的文本
String text = br.readLine();
String s[] = text.split("--");
//给输入框设置文本
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、Toast显示对话框:
Toast t = Toast.makeText(this, "登录成功", 0);
Toast t = Toast.makeText(MainActivity.this, "登录成功", 1);
第一个参数是一个上下文,一个activity也是个上下文
第三个参数只能是 0 或 1 (0显示3s , 1显示 5s)
分5个等级,每个等级使用不同颜色
verbose
debug
info
warn
error
定义过滤器方便查看
System.out.print输出的日志级别是info,tag是System.out
Android提供的日志输出api
Log.v(TAG, "hello");
Log.d(TAG, "hello");
Log.i(TAG, "hello");
Log.w(TAG, "hello");
Log.e(TAG, "hello");
2、在内部存储中写文件
Ram内存:运行内存,相当于电脑的内存
Rom内存:内部存储空间,相当于电脑的硬盘
sd卡:外部存储空间,相当于电脑的移动硬盘
所有安装至手机的应用都会在data/data目录下生成一个包名文件夹,这个文件夹就是内部存储的路径
应用只能在自己的包名文件夹中读写文件
例子:
//返回一个File对象,封装的路径是data/data/com.example.test/files
//File file = new File(getFilesDir(), "info.txt");
//返回一个File对象,封装的路径是data/data/com.example.test/cache
File file = new File(getCacheDir(), "info.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
//把账号密码写入本地文件
fos.write((name + "--" + pass).getBytes());//将字符串转化为字节流写到文件中
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
3、在内部存储中读文件
例子:
private void readAccount() {
//读取文件,回显数据,文件info.txt的内容是 test--123
//File file = new File("data/data/com.example.test/info.txt"); //这个会保存在应用的目录files文件夹下,就算空间不够也不会删除,除非用户自己清理
File file = new File(getFilesDir(), "info.txt");//这个会保存在应用的目录files文件夹下,就算空间不够也不会删除,除非用户自己清理
//File file = new File(getCacheDir(), "info.txt");//这个会保存在应用的目录caches文件夹下,如果空间不够就会被清除掉
if(file.exists()){//判断文件是否存在
try {
FileInputStream fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读取文件中的文本
String text = br.readLine();
String s[] = text.split("--");
//给输入框设置文本
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、Toast显示对话框:
Toast t = Toast.makeText(this, "登录成功", 0);
Toast t = Toast.makeText(MainActivity.this, "登录成功", 1);
第一个参数是一个上下文,一个activity也是个上下文
第三个参数只能是 0 或 1 (0显示3s , 1显示 5s)