2017-01-15 9 views
-1

IOクラスをEclipseのJavaからAndroid APIに変換したいと考えています。何らかの理由で、アンドロイドで動作しません!私のPrintlnメソッドでNullPointerExceptionを出しています。このクラスは、テキストファイルの作成、書き込み、読み込み、および開くために使用されます。私の目標は、これらのメソッドをすべてAndroidで読むことができるようにすることです。Java IOクラスをAndroid IOに変換する

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintStream; 
import java.io.PrintWriter; 
public class IO { 
    private static PrintWriter fileOut; 
    private static BufferedReader fileIn; 

    public static void createOutputFile(String fileName) { 
     try { 
      fileOut = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); 
     } catch (IOException e) { 
      System.out.println("*** Cannot create file: " + fileName + " ***"); 
     } 
    } 

    public static void openOutputFile(String fileName) { 
     try { 
      fileOut = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true))); 
     } catch (IOException e) { 
      System.out.println("*** Cannot open file: " + fileName + " ***"); 
     } 
    } 

    public static void openOutputFile2(String fileName) { 
     try { 
      fileOut = new PrintWriter(new BufferedWriter(new FileWriter(fileName, false))); 
     } catch (IOException e) { 
      System.out.println("*** Cannot open file: " + fileName + " ***"); 
     } 
    } 



    public static void print(String text) { 
     fileOut.print(text); 
    } 

    public static void println(String text) { 
     fileOut.println(text); 
     //System.out.println(text); 
    } 

    public static void closeOutputFile() { 
     fileOut.close(); 
    } 

    public static void openInputFile(String fileName) { 
     try { 
      fileIn = new BufferedReader(new FileReader(fileName)); 
      //System.out.println("opening " + fileName); 
     } catch (FileNotFoundException e) { 
      System.out.println("***Cannot open " + fileName + "***"); 
     } 
    } 

    public static String readLine() 
    // throws IOException 
    // Note: if there's an error in this method it will return IOException 
    { 
     try { 

      return fileIn.readLine(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
      return "errors"; 
     } 

    } 

    public static void closeInputFile() {// throws IOException 
     // Note: if there's an error in this method it will return IOException 
     try { 
      fileIn.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

「作業しない」という意味を説明してください。エラーメッセージを含めます。あなたのフレームワークから返されたエラーを無視しているなら、関連する情報を提供できるようにしてください。詳細については[ask]を参照してください。 –

+0

私のコードを実行するとnullポインタ例外が発生しています –

+0

この情報をあなたの質問に追加し、エラーが発生したコールスタックの***を***に含めてください。 –

答えて

0

あなたはprintlnでNullPointerExceptionが取得している場合は「fileOut」はnullであるため、そのは次のようになります。

public static void println(String text) { 
    fileOut.println(text); 
    //System.out.println(text); 
} 

最初のエラーを無視したため、実際には2番目のエラーに反応しています。 fileOutを設定するすべてのケースでは、エラーを飲み込んでいる(事実上隠れている)。例えば。

public static void openOutputFile(String fileName) { 
    try { 
     fileOut = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true))); 
    } catch (IOException e) { 
     //Don't do this, it makes debugging much more difficult. 
     //Because the root problem is hidden. 
     //So now you have 2 problems to solve. 
     //And you've thrown away the information that might have 
     //helped to solve the problem in the first place. 
     System.out.println("*** Cannot open file: " + fileName + " ***"); 
    } 
} 

エラーを隠すのをやめてください。何か間違ったことがある場合は、それについて調べる必要があります。常にエラーを無視すると、後でエラーが発生するため、より複雑なエラーが発生します。

あなたが悪い例外処理を修正した場合、ルートの問題(おそらくファイルが見つからないか、権限エラー)を追跡できます。

+0

ありがとうございました!!!!私はできるだけ早くそれを調べます:)。そして私はそれを決して知らなかった!私たちの学校は驚くほどその方法を教えてくれました。 –

+0

こんにちは私はちょうど知りたかったのですが、どうすれば良い例外処理ができますか? –

+0

他のものを使用してエラーが発生しているので、IOExceptionの他に何ができるのですか? –

関連する問題