0

I have a textfile in /sdcard/applit/mytext.txt I want to push it to parse cloud.Googled a lot but No profit.Please explain completely.Thanx.

          private void txtPusher(File dir) throws IOException {
          File outputFile;
          outputFile=newFile(dir,"MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt");
          byte [] b;
          b=FileUtils.readFileToByteArray(outputFile);

         file=new ParseFile("MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt",b);
         file.saveInBackground();
         TextPusher Tpusher=new TextPusher(file);
         Tpusher.execute();

          }

Here dir is the directory I am passing to txtPusher function.I want to know wether output file is that file which I am going to push or another directory or it is creating a new file.but my file is not getting pushed.If i am wrong please share the right way to push the textfile

1 Answers1

0

You can read the contents of a text (.txt) file using the following:

private String readFile(String fileName) {
    //Find the directory for the SD Card using the API
    File sdcard = new File(Environment.getExternalStorageDirectory() + File.separator + "Inventory_Files/Version/");

    // Get the text file
    File file = new File(sdcard, fileName);

    // 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');
        }
    } catch (IOException e) {
        //You'll need to add proper error handling here
    }

    return text.toString();
}

As for sending this data to the cloud, this looks to be very well documented on their site.

Leigh
  • 1,495
  • 2
  • 20
  • 42
  • I want to push it to parse and it needs converting this file into byte array.I dont know how to do that – Pankaj Varma Feb 01 '15 at 11:18
  • @PankajVarma Google is your friend! A quick search turns up [this](http://stackoverflow.com/questions/6058003/elegant-way-to-read-file-into-byte-array-in-java) – Leigh Feb 01 '15 at 11:21
  • I tried but every tutorial is related to imagefiles.no tutorial for text file.can you tell how can i locate that file? – Pankaj Varma Feb 01 '15 at 11:22
  • It shouldn't make any difference which type of file you are attempting to read into a `byte` array. However if you're looking for a specific tutorial on how to read a `.txt` file, another quick Google search turns up [this](http://examples.javacodegeeks.com/core-java/io/fileinputstream/read-file-in-byte-array-with-fileinputstream/) – Leigh Feb 01 '15 at 11:26
  • Ok I edited the question you can see what I am doing. :) – Pankaj Varma Feb 01 '15 at 11:29