Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中创建和读取文本文件_Java_Android_Eclipse - Fatal编程技术网

如何在Java中创建和读取文本文件

如何在Java中创建和读取文本文件,java,android,eclipse,Java,Android,Eclipse,我正在使用Eclipse进行一个大学项目,制作一个android应用程序。 我有一个问题: 我创建了一个名为TextFileHandler的类。 这个想法是应用程序实例化TextFileHandler并使用这些方法 使用get方法读取和写入应用程序中的文本文件 返回字符串值并设置将新值写入文本文件的方法 当这个类被实例化时,构造函数创建一个文本文件。 这些方法应该访问所创建的文本文件并读写它们 但问题是,一旦它创建了文本文件,它似乎就无法访问它们 应用程序崩溃了 我已经包含了构造函数,然后直接在

我正在使用Eclipse进行一个大学项目,制作一个android应用程序。
我有一个问题:

我创建了一个名为
TextFileHandler
的类。 这个想法是应用程序实例化
TextFileHandler
并使用这些方法 使用get方法读取和写入应用程序中的文本文件 返回字符串值并设置将新值写入文本文件的方法

当这个类被实例化时,构造函数创建一个文本文件。 这些方法应该访问所创建的文本文件并读写它们 但问题是,一旦它创建了文本文件,它似乎就无法访问它们 应用程序崩溃了

我已经包含了构造函数,然后直接在方法
getMeds()之后
每次都会崩溃。我不知道为什么它找不到文本文件

任何帮助都将不胜感激

构造函数:

public TextFileHandler(Context ctx){
    this.context = ctx;
    //create the medicine text file
    String medtext = "1#Med*2#Med*3#Med*4#Med*5#Med*";
    try {
          File file = new File("Medicine.txt");
          //if the file doesn't already exist then create it
          //this is to make sure the app saves state
          if(!file.exists()){
              BufferedWriter output = new BufferedWriter(new FileWriter(file));
              output.write(medtext);
              output.close();                     
          }
        } catch ( IOException e ) {
           e.printStackTrace();
        }


    String remtext = "1a#000000*1b#000000*1c#000000*1d#000000*1e#000000*";
    remtext += "2a#000000*2b#000000*2c#000000*2d#000000*2e#000000*";
    remtext += "3a#000000*3b#000000*3c#000000*3d#000000*3e#000000*";
    remtext += "4a#000000*4b#000000*4c#000000*4d#000000*4e#000000*";
    remtext += "5a#000000*5b#000000*5c#000000*5d#000000*5e#000000*";
    try {
          File file = new File("Reminder.txt");
          //if the file doesn't already exist then create it
          //this is to make sure the app saves state
          if(!file.exists()){
              BufferedWriter output = new BufferedWriter(new FileWriter(file));
              output.write(remtext);
              output.close();                     
          }
        } catch ( IOException e ) {
           e.printStackTrace();
        }
}
public String getMeds() throws IOException{
    FileInputStream in = openFileInput("Medicine.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }
    String ret = sb.toString();
    return ret;
}
获取方法:

public TextFileHandler(Context ctx){
    this.context = ctx;
    //create the medicine text file
    String medtext = "1#Med*2#Med*3#Med*4#Med*5#Med*";
    try {
          File file = new File("Medicine.txt");
          //if the file doesn't already exist then create it
          //this is to make sure the app saves state
          if(!file.exists()){
              BufferedWriter output = new BufferedWriter(new FileWriter(file));
              output.write(medtext);
              output.close();                     
          }
        } catch ( IOException e ) {
           e.printStackTrace();
        }


    String remtext = "1a#000000*1b#000000*1c#000000*1d#000000*1e#000000*";
    remtext += "2a#000000*2b#000000*2c#000000*2d#000000*2e#000000*";
    remtext += "3a#000000*3b#000000*3c#000000*3d#000000*3e#000000*";
    remtext += "4a#000000*4b#000000*4c#000000*4d#000000*4e#000000*";
    remtext += "5a#000000*5b#000000*5c#000000*5d#000000*5e#000000*";
    try {
          File file = new File("Reminder.txt");
          //if the file doesn't already exist then create it
          //this is to make sure the app saves state
          if(!file.exists()){
              BufferedWriter output = new BufferedWriter(new FileWriter(file));
              output.write(remtext);
              output.close();                     
          }
        } catch ( IOException e ) {
           e.printStackTrace();
        }
}
public String getMeds() throws IOException{
    FileInputStream in = openFileInput("Medicine.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }
    String ret = sb.toString();
    return ret;
}

使用
ctx.openFileOutput(“Medicine.txt”,MODE\u PRIVATE)
私下创建文件(只能由应用程序读取),或公开使用
新文件(Environment.getExternalStorageDirectory(),“Medicine.txt”)

要读取私有文件,请使用
Context.openFileInput(字符串名称)

1)请编辑您的问题,以包含应用程序崩溃时出现的错误的全文。2) 您是否验证了该文件实际上是在应用程序启动时创建的?