以下是mainActivity:
package com.example.dell_pc.myapplication;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
Button but_write;
Button but_read;
TextView tx;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
but_write=(Button)super.findViewById(R.id.but_write);
but_read=(Button)super.findViewById(R.id.but_read);
tx=(TextView)super.findViewById(R.id.print);
try {
createFile();
but_write.setOnClickListener(new WriteOnClickListener());
but_read.setOnClickListener(new ReadOnClickListener());
}
catch(Exception e){
tx.setText("创建文件出错");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class ReadOnClickListener implements View.OnClickListener
{
String str;
public void onClick(View v)
{
try {
File f1 = new File(str);
FileInputStream fIn = new FileInputStream(f1);
Scanner s = new Scanner(f1);
String str2 = "";
if (s.hasNext()) str2 = s.next();
tx.setText(str2);
s.close();
}
catch(Exception e)
{
tx.setText("创建输入流的时候出了问题");
}
}
}
class WriteOnClickListener implements View.OnClickListener
{
public void onClick(View v)
{
try {
File f2 = new File(str);
FileOutputStream fOut = new FileOutputStream(f2);
PrintWriter printWriter = new PrintWriter(fOut);
printWriter.println("this is a test");
printWriter.close();
}
catch(Exception e)
{
tx.setText("创建输出流的时候出了问题");
}
}
}
public void createFile() throws
IOException
{
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/t";
File file=new File(path);
if(!file.exists()) file.mkdirs();
str=path+"/test.txt";
file=new File(str);
if(!file.exists()) file.createNewFile();
}
}
}
下面贴张图:
从图中看出,创建文件那里就出错了,希望大神解答,指出不正确之处
这个路径: String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/t"; 无效吧
你把这个路径通过 CatLog 输出看看
单步调试下,是不是文件写入的错误
请问你在manifest.xml中添加了文件读写权限了吗?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
模拟器的sd卡功能没开吧?
1) 如果可以的话可以在异常处理中把你的异常信息打出来,一查异常信息就差不多了。
catch(Exception e){
tx.setText("创建文件出错");
}
2) 调试文件模拟器可能不一定好用,可以找个手机试试。
错误信息打印出来看看。
catch(Exception e){
tx.setText("创建文件出错"+e.getMessage());
}
请做如下几件事情:
1,确认权限-存储设备读写权限。
//
//
2,对存储是否挂载的判断情况做log输出
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//Log.d(.......);
} else {
//Log.d(........)
}
3,log打印你的文件路径,判断是否有问题
4,打印e信息:
catch(Exception e){
tx.setText("创建文件出错");
//TODO print your message in e
}
以下与当前错误无关:
5,文件读写都不要放在主线程,养成好习惯
6,不要硬编码