Вопрос

I am trying to read a data from a file. I have following code.

  public void ReadFile()
  {
      File sdcard = android.os.Environment.getExternalStorageDirectory();  
      File directory = new File(sdcard.getAbsolutePath()+ "/MyDirectory");  
      File file = new File(directory,"textfile1.txt");  
      try (FileInputStream fis = new FileInputStream(file)) {
          char stringComma = new Character(',');
          System.out.println("Total file size to read (in bytes) : "+ fis.available());
          int content;
          while ((content = fis.read()) != -1) {
              // convert to char and display it
              Log.d(TAG, "reading a file");
              System.out.print((char) content);
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

I have file format as follows [textfile1.txt]

  [12],84359768069 //some numbers
  [34],56845745740
  [44],36344679992
  [99],46378467467

When i am reading this file each character will read at a time. I want to split this and store in different string arrays like

  str1 = [12]
  str2 = 84359768069

How i can achieve this?

Это было полезно?

Решение

You're currently reading a byte at a time, because you're using InputStream. That's the first thing to fix - you should be using a Reader for text data. The best approach is to wrap your InputStream in an InputStreamReader.

Next, it sounds like you want to read a line at a time rather than just a character at a time. The easiest way of doing that is to use a BufferedReader wrapping an InputStreamReader.

(If you were using Java 7+, all of this could be achieved very nicely using Files.newBufferedReader - you just need to supply the Path and the Charset. Until Android supports that, you'll need to just do the wrapping manually. It's not too painful though.)

One you're reading a line at a time, you then need to split the line by comma - look at using String.split for this. I would then suggest you create a class to store these two separate values. So each line will be transformed into an instance of your class.

Finally, create a List<YourCustomClass> and add to it as you read the file.

That's given an overview of how to achieve each step - hopefully enough detail to enable you to get going, but not spoon-feeding you enough to hamper you actually learning from the experience.

Другие советы

A simple solution would be to parse the readed characters:

public void ReadFile()
{
  File sdcard = android.os.Environment.getExternalStorageDirectory();  
  File directory = new File(sdcard.getAbsolutePath()+ "/MyDirectory");  
  File file = new File(directory,"textfile1.txt");  
  try (FileInputStream fis = new FileInputStream(file)) {
      char stringComma = new Character(',');
      System.out.println("Total file size to read (in bytes) : "+ fis.available());
      int content;
      String str1="";
      String str2 = "";
      boolean commaFound=false;
      while ((content = fis.read()) != -1) {
          // convert to char and display it
          Log.d(TAG, "reading a file");
          if ((char)content==',')
          {
             commaFound = true;
          }
          else if ((char)content=="\n")
          {
              System.out.printlnt("str1="+str1+"\nstr2="+str2);
              commaFound = false;
              str1 = "";
              str2 = "";
          }
          else
          {
             if (commaFound)
             {
               str2 += (char)content;
             }
             else
             {
               str1 += (char)content;
             }
          }
          System.out.print((char) content);
      }
  } catch (IOException e) {
      e.printStackTrace();
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top