分享Java小白的第一个实用程序,其中因为android权限问题差点挠破头,最后通过查找各类教程终于成功。下面是源代码:
package com.example.timemachine;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private String fileName;
// 记录目录路径
private String fileDir;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android 6.0以上
//进入程序,申请读写权限
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
}
}
public void TimeC1(View view){
@SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
String y= format.format(new Date());
TextView textView1=(TextView) findViewById(R.id.TimeY);
textView1.setText(y);
}
public void TimeC2(View view){
@SuppressLint("SimpleDateFormat") SimpleDateFormat format1=new SimpleDateFormat("ddHHmm");
String d=format1.format(new Date());
TextView textView2=(TextView) findViewById(R.id.TimeD);
textView2.setText(d);
}
public void TimeC(View view){
String y1;
String d1;
String date;
TextView textView1=(TextView) findViewById(R.id.TimeY);
TextView textView2=(TextView) findViewById(R.id.TimeD);
y1=textView1.getText().toString();
d1=textView2.getText().toString();
String yd;
yd=(y1+"\ndayTime "+d1);
String head;
{head=("default 0\nmres 24\nphoto_size 0\nsplit 222\nyearMonth");}
date=(head+" "+yd);
TextView textView3=(TextView) findViewById(R.id.textView);
textView3.setText(date);
}
private void createFile(){
String mStrPath = Environment.getExternalStorageDirectory().getPath() + "/default.ini";
//String SYS_savePath=Environment.getExternalStorageDirectory().toString()+"/SYS";
//File SFile=new File(SYS_savePath);
File mFile=new File(mStrPath);
TextView textView=(TextView) findViewById(R.id.textView2);
//if (SFile.exists()){
// SFile.delete();
//}
//try {
// SFile.createNewFile();
// textView.setText("文件创建成功");
//}catch (IOException e){
// e.printStackTrace();
// textView.setText("文件创建失败");
//}
if (mFile.exists()){
mFile.delete();
}
try {
mFile.createNewFile();
textView.setText("文件创建成功");
}catch (IOException e){
e.printStackTrace();
textView.setText("文件创建失败");
}
OutputStream ou = null;
String y1;
String d1;
String date;
TextView textView1=(TextView) findViewById(R.id.TimeY);
TextView textView2=(TextView) findViewById(R.id.TimeD);
y1=textView1.getText().toString();
d1=textView2.getText().toString();
String yd;
yd=(y1+"\ndayTime "+d1);
String head;
{head=("default 0\nmres 24\nphoto_size 0\nsplit 222\nyearMonth");}
date=(head+" "+yd);
try {
ou = new FileOutputStream(mFile);
byte[] buffer = date.getBytes();
ou.write(buffer);
ou.flush();
Toast.makeText(this,"写入成功",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ou != null) {
ou.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void YingY(View view){
createFile();
}
}
读写文件要在AndroidManifest.xml里加入权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 允许创建和删除外部存储设备的文件 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<application
android:requestLegacyExternalStorage="true"
最重要一点是在build.gradle里更改targetSdk版本为28:
targetSdk 28
下面是功能截图:
![](https://i-blog.csdnimg.cn/blog_migrate/085d3957f45c5548b1c542bfccaac39d.png)
本app生成的default.ini文件用于更改华容录像手电日期时间,可获取系统当前时间也可自定义时间。