0

I have some programming experience, but I have no experience in working with text files in android. I am using android studio, and making an app which gets a String from a file, and than displays it.

I did my research, and according to android.developer.com I need to use FileReader, but I still have some questions:

1.In which folder do I put the .txt files in?

2.How do I make a FileReader reference to the .txt file?

I have searched this site and other sites, and there are a lot of questions like mine, but none answer the questions I have..

EDIT: Can someone please give me the whole code/ step by step instructions on how to do this. From where to place the file, to how to access and make a string out of it

Eddie
  • 31
  • 8

2 Answers2

0

In which folder do I put the text files in?

Well, it is really up to you. You can even create one and customise your file tree. Note that root directories may vary in different devices so it is highly recommended that you should reference from Environment.getExternalStorageDirectory()

For example,

File dir = new File(Environment.getExternalStorageDirectory()+"yourCustomFolder");
if(!dir.exists())
        fileDir.mkdirs();

by that, you can see on your phone that the folder has been created.

For further information, like creating files to your custom folder, you can reference from this answer.

Community
  • 1
  • 1
Rick Royd Aban
  • 904
  • 6
  • 33
  • You can also put your text file in asset folder and then read data from it. http://stackoverflow.com/questions/9544737/read-file-from-assets or https://www.javacodegeeks.com/2012/02/android-read-file-from-assets.html – Nrup Parikh Jan 02 '17 at 04:56
  • I made an texts folder, so would I do `File file = new File(Enviorment.getExternalStorageDirecory() + "texts\file_name.txt"); – Eddie Jan 02 '17 at 06:29
  • That would do. Please refer to the link to set string on your file – Rick Royd Aban Jan 02 '17 at 06:40
  • Does this seem right `File file = new File(Environment.getExternalStorageDirectory() + "assets/1.txt"); FileReader read = new FileReader(file); String str = read.toString();` – Eddie Jan 02 '17 at 06:48
0

compile :

compile group: 'org.apache.poi', name: 'poi', version: '3.0.1-FINAL'

Below is the code to read file from .xls file. -this code snippets is for reading only one field customername, you can add more cells to it.

 /**
 * Method to Import XLS file to Database
 *
 * @param uriFile
 */
private void importCsvFileToDatabase(Uri uriFile) {
    try {
        InputStream is = getContentResolver().openInputStream(uriFile);
        POIFSFileSystem myFileSystem = new POIFSFileSystem(is);
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        boolean isFirstRow = true;
        int i = 1;

        while (rowIter.hasNext()) {
            HSSFRow rowhead = mySheet.getRow((short) i++);
            if (rowhead == null) {
                break;
            }
            String customerName = "";

            CustomerInfo customerInfodb = new CustomerInfo();
            try {
                customerName = rowhead.getCell((short) 0).toString();
                customerInfodb.setCustomerName(customerName);
            } catch (Exception e) {
                customerInfodb.setCustomerName("");
                Log.e("TAG", "err" + e.getMessage());
            }

                CustomerInfo.saveCustomerInfo(context, customerInfodb);

    } catch (Exception e) {
        Log.e("LOG", "err" + e.getMessage());
    }
}
Raut Darpan
  • 1,520
  • 9
  • 14