Android studio 将姓名、学号和语文、数学成绩4个信息存入内部文件、SD卡以及SharedPerences完整代码
时间: 2024-01-30 15:03:30 浏览: 23
以下是将姓名、学号和语文、数学成绩4个信息存入内部文件、SD卡以及SharedPerences的完整代码:
1. 存入内部文件
```java
private void saveToInternalStorage(String name, String id, int chineseScore, int mathScore) {
String filename = "student_info.txt";
String fileContents = name + "," + id + "," + chineseScore + "," + mathScore;
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(fileContents.getBytes());
fos.close();
Toast.makeText(this, "Saved to internal storage", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error saving to internal storage", Toast.LENGTH_SHORT).show();
}
}
```
2. 存入SD卡
```java
private void saveToExternalStorage(String name, String id, int chineseScore, int mathScore) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/MyApp");
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, "student_info.txt");
String fileContents = name + "," + id + "," + chineseScore + "," + mathScore;
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(fileContents.getBytes());
fos.close();
Toast.makeText(this, "Saved to SD card", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error saving to SD card", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "SD card not available", Toast.LENGTH_SHORT).show();
}
}
```
3. 存入SharedPreferences
```java
private void saveToSharedPreferences(String name, String id, int chineseScore, int mathScore) {
SharedPreferences sharedPreferences = getSharedPreferences("student_info", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", name);
editor.putString("id", id);
editor.putInt("chineseScore", chineseScore);
editor.putInt("mathScore", mathScore);
editor.apply();
Toast.makeText(this, "Saved to SharedPreferences", Toast.LENGTH_SHORT).show();
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)