GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 添加file工具类 sp工具类 toast工具类 · spysoos/AndroidUtils@a1a433f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main/java/wanglijun/vip/androidutils
    • FileUtil.java
    • SPUtil.java
    • ToastUtil.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/src/main/java/wanglijun/vip/androidutils/FileUtil.java‎

Copy file name to clipboard
+158Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package wanglijun.vip.androidutils;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.InputStream;
7+
8+
/**
9+
* @author: wlj
10+
* @Date: 2017-03-28
11+
* @email: [email protected]
12+
* @desc: 文件工具类
13+
*/
14+
public class FileUtil {
15+
/**
16+
* 在指定的位置创建指定的文件
17+
*
18+
* @param filePath 完整的文件路径
19+
* @param mkdir 是否创建相关的文件夹
20+
* @throws Exception
21+
*/
22+
public static void mkFile(String filePath, boolean mkdir) throws Exception {
23+
File file = new File(filePath);
24+
file.getParentFile().mkdirs();
25+
file.createNewFile();
26+
file = null;
27+
}
28+
29+
/**
30+
* 在指定的位置创建文件夹
31+
*
32+
* @param dirPath 文件夹路径
33+
* @return 若创建成功,则返回True;反之,则返回False
34+
*/
35+
public static boolean mkDir(String dirPath) {
36+
return new File(dirPath).mkdirs();
37+
}
38+
39+
/**
40+
* 删除指定的文件
41+
*
42+
* @param filePath 文件路径
43+
*
44+
* @return 若删除成功,则返回True;反之,则返回False
45+
*
46+
*/
47+
public static boolean delFile(String filePath) {
48+
return new File(filePath).delete();
49+
}
50+
51+
/**
52+
* 删除指定的文件夹
53+
*
54+
* @param dirPath 文件夹路径
55+
* @param delFile 文件夹中是否包含文件
56+
* @return 若删除成功,则返回True;反之,则返回False
57+
*
58+
*/
59+
public static boolean delDir(String dirPath, boolean delFile) {
60+
if (delFile) {
61+
File file = new File(dirPath);
62+
if (file.isFile()) {
63+
return file.delete();
64+
} else if (file.isDirectory()) {
65+
if (file.listFiles().length == 0) {
66+
return file.delete();
67+
} else {
68+
int zfiles = file.listFiles().length;
69+
File[] delfile = file.listFiles();
70+
for (int i = 0; i < zfiles; i++) {
71+
if (delfile[i].isDirectory()) {
72+
delDir(delfile[i].getAbsolutePath(), true);
73+
}
74+
delfile[i].delete();
75+
}
76+
return file.delete();
77+
}
78+
} else {
79+
return false;
80+
}
81+
} else {
82+
return new File(dirPath).delete();
83+
}
84+
}
85+
86+
/**
87+
* 复制文件/文件夹 若要进行文件夹复制,请勿将目标文件夹置于源文件夹中
88+
* @param source 源文件(夹)
89+
* @param target 目标文件(夹)
90+
* @param isFolder 若进行文件夹复制,则为True;反之为False
91+
* @throws Exception
92+
*/
93+
public static void copy(String source, String target, boolean isFolder)
94+
throws Exception {
95+
if (isFolder) {
96+
(new File(target)).mkdirs();
97+
File a = new File(source);
98+
String[] file = a.list();
99+
File temp = null;
100+
for (int i = 0; i < file.length; i++) {
101+
if (source.endsWith(File.separator)) {
102+
temp = new File(source + file[i]);
103+
} else {
104+
temp = new File(source + File.separator + file[i]);
105+
}
106+
if (temp.isFile()) {
107+
FileInputStream input = new FileInputStream(temp);
108+
FileOutputStream output = new FileOutputStream(target + "/" + (temp.getName()).toString());
109+
byte[] b = new byte[1024];
110+
int len;
111+
while ((len = input.read(b)) != -1) {
112+
output.write(b, 0, len);
113+
}
114+
output.flush();
115+
output.close();
116+
input.close();
117+
}
118+
if (temp.isDirectory()) {
119+
copy(source + "/" + file[i], target + "/" + file[i], true);
120+
}
121+
}
122+
} else {
123+
int byteread = 0;
124+
File oldfile = new File(source);
125+
if (oldfile.exists()) {
126+
InputStream inStream = new FileInputStream(source);
127+
File file = new File(target);
128+
file.getParentFile().mkdirs();
129+
file.createNewFile();
130+
FileOutputStream fs = new FileOutputStream(file);
131+
byte[] buffer = new byte[1024];
132+
while ((byteread = inStream.read(buffer)) != -1) {
133+
fs.write(buffer, 0, byteread);
134+
}
135+
inStream.close();
136+
fs.close();
137+
}
138+
}
139+
}
140+
141+
/**
142+
* 移动指定的文件(夹)到目标文件(夹)
143+
* @param source 源文件(夹)
144+
* @param target 目标文件(夹)
145+
* @param isFolder 若为文件夹,则为True;反之为False
146+
* @return
147+
* @throws Exception
148+
*/
149+
public static boolean move(String source, String target, boolean isFolder)
150+
throws Exception {
151+
copy(source, target, isFolder);
152+
if (isFolder) {
153+
return delDir(source, true);
154+
} else {
155+
return delFile(source);
156+
}
157+
}
158+
}
Collapse file

‎app/src/main/java/wanglijun/vip/androidutils/SPUtil.java‎

Copy file name to clipboard
+172Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package wanglijun.vip.androidutils;
2+
3+
/**
4+
* @author: wlj
5+
* @Date: 2017-03-28
6+
* @email: [email protected]
7+
* @desc: SharedPreferences统一管理类
8+
*/
9+
10+
import android.content.Context;
11+
import android.content.SharedPreferences;
12+
13+
import java.lang.reflect.InvocationTargetException;
14+
import java.lang.reflect.Method;
15+
import java.util.Map;
16+
17+
18+
public class SPUtil {
19+
/**
20+
* 保存在手机里面的文件名
21+
*/
22+
public static final String FILE_NAME = "share_data";
23+
24+
/**
25+
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
26+
*
27+
* @param context
28+
* @param key
29+
* @param object
30+
*/
31+
public static void put(Context context, String key, Object object) {
32+
33+
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
34+
Context.MODE_PRIVATE);
35+
SharedPreferences.Editor editor = sp.edit();
36+
37+
if (object instanceof String) {
38+
editor.putString(key, (String) object);
39+
} else if (object instanceof Integer) {
40+
editor.putInt(key, (Integer) object);
41+
} else if (object instanceof Boolean) {
42+
editor.putBoolean(key, (Boolean) object);
43+
} else if (object instanceof Float) {
44+
editor.putFloat(key, (Float) object);
45+
} else if (object instanceof Long) {
46+
editor.putLong(key, (Long) object);
47+
} else {
48+
editor.putString(key, object.toString());
49+
}
50+
SharedPreferencesCompat.apply(editor);
51+
}
52+
53+
54+
/**
55+
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
56+
*
57+
* @param context
58+
* @param key
59+
* @param defaultObject
60+
* @return
61+
*/
62+
public static Object get(Context context, String key, Object defaultObject)
63+
{
64+
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
65+
Context.MODE_PRIVATE);
66+
67+
if (defaultObject instanceof String) {
68+
return sp.getString(key, (String) defaultObject);
69+
} else if (defaultObject instanceof Integer) {
70+
return sp.getInt(key, (Integer) defaultObject);
71+
} else if (defaultObject instanceof Boolean) {
72+
return sp.getBoolean(key, (Boolean) defaultObject);
73+
} else if (defaultObject instanceof Float) {
74+
return sp.getFloat(key, (Float) defaultObject);
75+
} else if (defaultObject instanceof Long) {
76+
return sp.getLong(key, (Long) defaultObject);
77+
}
78+
return null;
79+
}
80+
81+
/**
82+
* 移除某个key值已经对应的值
83+
* @param context
84+
* @param key
85+
*/
86+
public static void remove(Context context, String key) {
87+
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
88+
Context.MODE_PRIVATE);
89+
SharedPreferences.Editor editor = sp.edit();
90+
editor.remove(key);
91+
SharedPreferencesCompat.apply(editor);
92+
}
93+
94+
/**
95+
* 清除所有数据
96+
* @param context
97+
*/
98+
public static void clear(Context context) {
99+
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
100+
Context.MODE_PRIVATE);
101+
SharedPreferences.Editor editor = sp.edit();
102+
editor.clear();
103+
SharedPreferencesCompat.apply(editor);
104+
}
105+
106+
/**
107+
* 查询某个key是否已经存在
108+
* @param context
109+
* @param key
110+
* @return
111+
*/
112+
public static boolean contains(Context context, String key) {
113+
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
114+
Context.MODE_PRIVATE);
115+
return sp.contains(key);
116+
}
117+
118+
/**
119+
* 返回所有的键值对
120+
*
121+
* @param context
122+
* @return
123+
*/
124+
public static Map<String, ?> getAll(Context context) {
125+
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
126+
Context.MODE_PRIVATE);
127+
return sp.getAll();
128+
}
129+
130+
/**
131+
* 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
132+
*
133+
* @author zhy
134+
*
135+
*/
136+
private static class SharedPreferencesCompat {
137+
private static final Method sApplyMethod = findApplyMethod();
138+
139+
/**
140+
* 反射查找apply的方法
141+
*
142+
* @return
143+
*/
144+
@SuppressWarnings({ "unchecked", "rawtypes" })
145+
private static Method findApplyMethod() {
146+
try {
147+
Class clz = SharedPreferences.Editor.class;
148+
return clz.getMethod("apply");
149+
} catch (NoSuchMethodException e) {
150+
}
151+
return null;
152+
}
153+
154+
/**
155+
* 如果找到则使用apply执行,否则使用commit
156+
*
157+
* @param editor
158+
*/
159+
public static void apply(SharedPreferences.Editor editor) {
160+
try {
161+
if (sApplyMethod != null) {
162+
sApplyMethod.invoke(editor);
163+
return;
164+
}
165+
} catch (IllegalArgumentException e) {
166+
} catch (IllegalAccessException e) {
167+
} catch (InvocationTargetException e) {
168+
}
169+
editor.commit();
170+
}
171+
}
172+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.