Android开发调试技巧

Android开发过程中会遇到不少需要调试的地方,常用的调试也很简单,这里总结一些不是很常规的调试技巧(持续更新)

1、写本地日志

利用BufferWriter类来实现,不多说废话,直接上代码,这个后面方便传代码到git的时候,放到git上吧,毕竟直接放博客排版太长了

public class LogsUtil {
    public static LogsUtil logsUtil = new LogsUtil();
    private File logFile;
    private static final String TAG = "LogsUtil";
    private LogsUtil(){
    }

    public static LogsUtil getInstance(){
        return logsUtil;
    }

	//please call this init function before use
    public void init(Context context,String fileName){
        logFile = new File(context.getExternalCacheDir(),fileName);
        if(!logFile.exists()){
            try{
                logFile.createNewFile();
            }catch (IOException e){
                Log.d(TAG,"init" + e.getMessage());
            }
        }
    }

    public void saveLog(String info){
        try{
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(logFile,true));
            bufferedWriter.write(info);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            bufferedWriter.close();
        }catch (IOException e){
            Log.d(TAG,"saveLog" +  e.getMessage());
        }
    }

    public void readLog(){
        try{
            BufferedReader bufferedReader = new BufferedReader(new FileReader(logFile));
            String line = bufferedReader.readLine();
            StringBuilder stringBuilder = new StringBuilder();
            while(line != null){
                stringBuilder.append(line);
                stringBuilder.append("\n");
                line = bufferedReader.readLine();
            }
            bufferedReader.close();
            Log.d(TAG,"readLog: \n" + stringBuilder.toString());
        }catch (IOException e){
            Log.d(TAG,"readLog " + e.getMessage());
        }
    }
}

2、获取本地存储的xml以及db文件

你可能感兴趣的:(android)