【问题标题】:Android/java Cant read from .txt file in local storageAndroid/java 无法读取本地存储中的 .txt 文件
【发布时间】:2015-12-29 15:04:19
【问题描述】:

所以我在本地存储中有一个 .txt 文件,它是一个简单的文本文件。文本基本上只是一系列的行。

我正在使用下面的代码尝试读取文本文件(我在调用此方法之前验证文件是否存在)。

public static String GetLocalMasterFileStream(String Operation) throws Exception {

//Get the text file
    File file = new File("sdcard/CM3/advices/advice_master.txt");
    if (file.canRead() == true) {System.out.println("-----Determined that file is readable");}

    //Read text from file
    StringBuilder text = new StringBuilder();
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        System.out.println("-----" + line);   //for producing test output
        text.append('\n');

    }
    br.close();
    System.out.print(text.toString());
    return text.toString();

}

代码在日志中产生

----确定文件可读 但那是文件数据不写入日志的唯一输出

我还尝试在 while 循环之前插入以下内容以尝试仅读取第一行

 line = br.readLine();
 System.out.println("-----" + line);

产生以下输出:

-----null

【问题讨论】:

    标签: java android file-io bufferedreader internal-storage


    【解决方案1】:

    看看getExternalStorage

         File path = Environment.getExternalStorageDirectory();
         File file = new File(path, "textfile.txt");
    //text file is copied in sdcard for example
    

    【讨论】:

      【解决方案2】:

      尝试在文件路径 /sdcard/CM3/advices/advice_master.txt 中添加前导斜杠

       File file = new File("/sdcard/CM3/advices/advice_master.txt");
      

      【讨论】:

      • 添加了前导斜杠,它没有任何区别
      【解决方案3】:

      试试这个。只需将txt文件名作为参数传递...

      public void readFromFile(String fileName){
          /*
          InputStream ips;
          ips = getClass().getResourceAsStream(fileName);
      
          //reading
          try{
              InputStreamReader ipsr = new InputStreamReader(ips);
              BufferedReader br = new BufferedReader(ipsr);
              String line;
      
              while ((line = br.readLine())!=null){
                  //reading goes here ;)
              }
              br.close();
          }
          catch (Exception e){
              System.out.println(e.toString());
          }
         */
      // or try this
      File sdcard = Environment.getExternalStorageDirectory();
      
      //Get the text file
      File file = new File(sdcard,"file.txt");
      
      //Read text from file
      StringBuilder text = new StringBuilder();
      
      try {
          BufferedReader br = new BufferedReader(new FileReader(file));
          String line;
      
          while ((line = br.readLine()) != null) {
              text.append(line);
              text.append('\n');
          }
          br.close();
      }
      catch (IOException e) {
          //You'll need to add proper error handling here
         }
      
      
      }
      

      【讨论】:

      • 对我有用,但您可以尝试其他变体,我也添加了其他变体
      • 我试过你的变种,但没有用。但我看到你的评论对你有用。所以我创建了一个名为 test.txt 的不同文件,其中包含 3 行,将它放在 sd 卡和本地存储的根目录下,它就可以工作了。我做了一些测试,发现 Environment.getExternalStorageDirectory();实际上指向设备的内部存储而不是 SD 卡。无论如何,我然后用旧文件替换了新文件,但它没有用。然后我回到我的原始代码,将它指向新的 txt 文件并且它工作。问题是 .txt 文件本身
      • txt 文件是使用此处的代码生成的:stackoverflow.com/questions/34504024/…,但 bw.write(inputLine.toString()); 除外改为 bw.write(inputLine + "\r\n");如果您在写字板(视觉)中打开文件,它会提供正确的输出,但无论出于何种原因,文件在回读时出现问题
      【解决方案4】:

      让我完善我的答案。您可以尝试另一种方法来读取advice_master.txt 中的所有行,看看会发生什么。它确保可以读取所有文件内容。

      Charset charset = Charset.forName("ISO-8859-1");
      try {
        List<String> lines = Files.readAllLines(Paths.get(YOUR_PATH), charset);
      
        for (String line : lines) {
          System.out.println(line);
        }
      } catch (IOException e) {
        System.out.println(e);
      }
      

      【讨论】:

      • 好的,我尝试了你的建议,我得到了以下 java.io.FileNotFoundException:/system/storage/sdcard/CM3/advices/advice_master.txt:打开失败:ENOENT(没有这样的文件或目录)文件路径是正确的,因为当我将其更改为其他内容时,它确实会抛出我使用的文件路径不存在的文件未找到错误
      猜你喜欢
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多