【问题标题】:Converting Java IO Class to Android IO将 Java IO 类转换为 Android IO
【发布时间】:2017-01-15 06:36:23
【问题描述】:

我想将我的 IO 类从 Eclipse 上的 java 转换为 Android API。出于某种原因,它在 android 上不起作用!它在我的 Println 方法上给了我一个 NullPointerException。此类用于创建、写入、读取和打开文本文件。我的目标是让所有这些方法在 android 上可读。

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();
        }
    }
}

【问题讨论】:

  • 解释你所说的“不工作”是什么意思。包括错误消息。如果您忽略框架返回的错误,请停止这样做,以便提供相关信息。请参阅How to Ask 了解更多信息。
  • 运行代码时出现空指针异常
  • edit将该信息添加到您的问题中,并在发生错误的调用堆栈中包含 where
  • 当我在文本文件中创建或存储数据时使用这个类。这个类在 Java Eclipse 中非常适合我,但是它在 android 上给了我这个例外。我希望有人能理解为什么。我的方法是不言自明的,我希望有人能指导我了解它的语法是什么,以便它在 android api 的眼中是可读的。

标签: java android file io


【解决方案1】:

如果您在 println 中收到 NullPointerException,那是因为 'fileOut' 为空。

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

您实际上是在对第二个错误做出反应,因为您忽略了第一个错误。在您设置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 + " ***");
    }
}

停止隐藏错误,如果出现问题,您需要找出它。因为总是忽略错误会导致以后出现更复杂的错误。

修复你糟糕的异常处理,你将能够找到根本问题(可能是文件未找到或权限错误)。

【讨论】:

  • 谢谢!!!!我会尽快调查的:)。我从来不知道!我们学校教我们的方式令人惊讶。
  • 您好,我只是想知道,我将如何进行良好的异常处理?
  • 因为我在使用其他东西时遇到错误,除了 IOException 我还能做什么
  • 正确的异常处理是一个广泛的话题。但是有一些指导原则:在大多数情况下,让异常冒泡是可以的。在“外部”级别,它需要被适当地记录或报告(取决于应用程序的类型)。仅当您因异常发生而需要做某事时才对异常“做出反应”。并且通常重新抛出以确保调用堆栈的其余部分知道它。仅当失败对调用者无关紧要时才吞下异常。 (如果您提供的 API 通过其他方式暴露错误信息,您也可以吞下。)
  • 可能是要问自己的最重要的问题:调用此方法的客户是否可能关心我的方法的成功/失败?如果是,它需要能够以某种方式找出错误。例外是好的,但其他选项也适用(例如,大多数 Windows API 函数返回一个指示成功/失败的值,您可以调用另一个函数来获取有关错误的更多详细信息。这符合上述要求。但请注意,在许多情况下,这需要客户端上的代码更多,并且是客户端不进行错误检查时常见的错误来源。)
猜你喜欢
  • 2016-07-20
  • 2013-03-16
  • 1970-01-01
  • 2011-05-13
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多