try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath()+ "/Sniffiing");
dir.mkdirs();
File file1 = new File(dir,"dump1.txt");
file1.createNewFile();
System.out.println(file1);
System.out.println("Hello Buddy what are upto.....##########################");
OutputStreamWriter os = new OutputStreamWriter(
new FileOutputStream(file1));
bw = new BufferedWriter(os);
Process process = Runtime.getRuntime().exec(
"logcat -v threadtime -s ServiceMode");
int length = process.toString().length();
System.out.println("Length" + length);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuffer output1 = new StringBuffer();
while ((line = reader.readLine()) != null) {
bw.write(line);
bw.newLine();
}
} catch (IOException e) {
}
这里是Logcat
D/action: 0
D/ACTION:: BOTH
D/Log3GState:: Accessing file
D/Create file: /data/user/0/com.services.snifspoof.snifspoof/C_packetCapture
D/Log3GState:: Write executable file
D/EGL_emulation: eglMakeCurrent: 0x9ef850c0: ver 2 0 (tinfo 0x9ef831f0)
D/EGL_emulation: eglMakeCurrent: 0x9ef850c0: ver 2 0 (tinfo 0x9ef831f0)
D/CLOSING: BOTH
I/System.out: *********#############
I/System.out: *********############# File Created :- /storage/emulated/0/Sniffing/dump1.txt
I/System.out: Length34
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: com.services.snifspoof.snifspoof, PID: 2389
java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference
at com.services.snifspoof.snifspoof.MainActivity$3$1.run(MainActivity.java:167)
at java.lang.Thread.run(Thread.java:761)
答案 0 :(得分:0)
希望这对你有用。
确保您已在清单文件中添加了读取外部存储空间的权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
写文件:
private void writeToFile(String data,Context context) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("dump1.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
阅读文件:
private String readFromFile(Context context) {
String ret = "";
try {
InputStream inputStream = context.openFileInput("dump1.txt");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}