public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 写入私有文件 会在当前应用程序包下创建一个 file 文件夹 并在此文件夹下创建一个zhang.txt文件
*
* @param view
*/
public void writerPrivate(View view) {
try {
OutputStream out = openFileOutput("zhang.txt", Context.MODE_APPEND);
String info = "中国人";
byte[] bytes = info.getBytes();
out.write(bytes, 0, bytes.length);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取私有文件内容 读取应用程序包下的 file文件夹下的指定文件 ,这里为 zhang.txt
*
* @param view
*/
public void readSDK(View view) {
try {
InputStream in = openFileInput("zhang.txt");
StringBuffer sb = new StringBuffer();
byte[] bytes = new byte[100];
int len = -1;
while ((len = in.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len));
}
in.close();
Toast.makeText(this, sb, Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/***
* 读取 raw 文件,自已在res 文件夹下 创建的 raw文件夹下的文件 名称为固定写法
*
* @param view
*/
public void readRawFile(View view) {
InputStream in = getResources().openRawResource(R.raw.zhang);
StringBuffer sb = new StringBuffer();
byte[] bytes = new byte[100];
int len = -1;
try {
while ((len = in.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len));
}
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
Toast.makeText(this, sb, Toast.LENGTH_SHORT).show();
}
/**
* 创建缓存文件 在当前应用程序包下的 cache 文件夹下创建一个以 .tmp结尾的文件 并写入缓存内容
*
* @param view
*/
public void readCacheFile(View view) {
try {
File tem = File.createTempFile("temp", null, getCacheDir());
FileOutputStream out = new FileOutputStream(tem);
PrintStream ps = new PrintStream(out);
ps.print("我是第一噢!");
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/***
* 判断外部是否有sdCard 存储卡
*
* @param view
*/
public void isExtSdk(View view) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "sdk", Toast.LENGTH_SHORT).show();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
Toast.makeText(this, "sd只读", Toast.LENGTH_SHORT).show();
} else {
Log.i("msg", Environment.getExternalStorageDirectory().getPath());
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
} else {
Toast.makeText(this, " no sdk", Toast.LENGTH_SHORT).show();
}
}
/***
* 写入外部存储的私有文件 4.4以后才能使用
* <p/>
* //在本应用程序在外部存储卡上的 storage/sdcard/android/data/应用程序包/file文件夹下写入文件,
* //只有本应用程序才能访问这个 文件
*
* @param view
*/
public void writerSDK(View view) {
File file = getExternalFilesDir(null);
if (file != null) {
try {
FileOutputStream out = new FileOutputStream(file + "/zhangzhang.txt");
Log.i("msg", file.getPath());
PrintStream ps = new PrintStream(out);
ps.print("zhang zhang");
ps.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
/***
* 写入外部存储的私有缓存文件 4.4以后才能使用
*
* 路径 storage/sdcard/android/data/应用程序包/cache文件夹下写入缓存文件,
*
* @param view
*/
public void readprivateSDKtmp(View view) {
try {
File tempFile = File.createTempFile("zhan", null, getExternalCacheDir());
if (tempFile != null) {
FileOutputStream out = new FileOutputStream(tempFile);
PrintStream ps = new PrintStream(out);
ps.print("good is good");
ps.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}