写入文件时出现空指针异常[重复]

问题描述 投票:0回答:1

嘿,当我创建文件时,日志显示该文件已创建,但系统中不存在,并且在写入该文件时,它显示空指针异常,有人可以帮助我吗

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)
java android nullpointerexception
1个回答
0
投票

参见https://stackoverflow.com/a/20064021/

确保您已在清单文件中添加了读取外部存储的权限。

<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;
}
© www.soinside.com 2019 - 2024. All rights reserved.