To get the filename you can use this:

EditText fileNameEdit= (EditText) getActivity().findViewById(R.id.fileName);
String fileName = fileNameEdit.getText().toString();

Then write the file on disk:

try {
    String content = "Separe here integers by semi-colon";
    File file = new File(fileName +".csv");
    // if file doesnt exists, then create it
    if (!file.exists()) {
       file.createNewFile();
    }

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();

} catch (IOException e) {
    e.printStackTrace();
}

To read the file:

BufferedReader br = null; 
try {
  String sCurrentLine;
  br = new BufferedReader(new FileReader(fileName+".csv"));
  while ((sCurrentLine = br.readLine()) != null) {
    System.out.println(sCurrentLine);
  }
} catch (IOException e) {
    e.printStackTrace();
} finally {
  try {
     if (br != null)br.close();
  } catch (IOException ex) {
     ex.printStackTrace();
  }
}

Then to have the Integers you can use split function:

String[] intArray = sCurrentLine.split(";");
Answer from João Marcos on Stack Overflow
Top answer
1 of 3
5

To get the filename you can use this:

EditText fileNameEdit= (EditText) getActivity().findViewById(R.id.fileName);
String fileName = fileNameEdit.getText().toString();

Then write the file on disk:

try {
    String content = "Separe here integers by semi-colon";
    File file = new File(fileName +".csv");
    // if file doesnt exists, then create it
    if (!file.exists()) {
       file.createNewFile();
    }

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();

} catch (IOException e) {
    e.printStackTrace();
}

To read the file:

BufferedReader br = null; 
try {
  String sCurrentLine;
  br = new BufferedReader(new FileReader(fileName+".csv"));
  while ((sCurrentLine = br.readLine()) != null) {
    System.out.println(sCurrentLine);
  }
} catch (IOException e) {
    e.printStackTrace();
} finally {
  try {
     if (br != null)br.close();
  } catch (IOException ex) {
     ex.printStackTrace();
  }
}

Then to have the Integers you can use split function:

String[] intArray = sCurrentLine.split(";");
2 of 3
1

Opencsv doesn't work on Android due to JDK issue.

If you see simular to the following:

05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pk.opencsvpoc, PID: 25829 java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;

It is because Android only ported over a subset of java.beans into its version of java. You can see a list of what is support at https://developer.android.com/reference/java/beans/package-summary

There are plans to switch over to reflection in opencsv 5.0 but I doubt it will remove all our dependencies on java.beans. For now the best suggestion for opencsv is to steer clear of the com.opencsv.bean classes until Android fully supports java.beans or we are successful in removing java.beans.

Another possibility is to try another csv library. I checked apache commons csv and super-csv and apache does not convert to beans but super-csv does using only reflection.

Source: https://sourceforge.net/p/opencsv/wiki/FAQ/#getting-noclassdeffounderror-when-using-android

🌐
Javapapers
javapapers.com › android › android-read-csv-file
Android Read CSV File - Javapapers
There is nothing fancy and is a simple tutorial for beginners. ... Create a folder named “raw” inside the “res” folder and put the CSV file in it. Here I am not discussing about loading a CSV file from an external memory card.
Discussions

java - Reading CSV File In Android App - Stack Overflow
I'm working on a proof-of-concept app so that I can implement the feature in a larger app I'm making. I'm a bit new to Java and Android Dev but hopefully this shouldn't be too simple or complex of a question. Basically, I'm trying to read in a list of strings from a CSV file and make it usable ... More on stackoverflow.com
🌐 stackoverflow.com
How to create CSV file in Android?
I fixed it by getting permission in my phone. I needed to give a permission to app. More on reddit.com
🌐 r/godot
1
1
October 29, 2023
How to read csv file in android? - Stack Overflow
Possible Duplicate: Get and Parse CSV file in android I would like to store a csv file in android app itself & it should be called for read & later print the values according to the More on stackoverflow.com
🌐 stackoverflow.com
How to export data to csv file in Android? - Stack Overflow
I created a csv file with the following format which I'm aiming to output to the device's sd card: Ship Name,Scientist Name,Scientist Email,Sample Volume,Sample Colour,Longitude,Latitude,Material,... More on stackoverflow.com
🌐 stackoverflow.com
🌐
en.proft.me
en.proft.me › 2017 › 07 › 6 › how-read-csv-file-android
How to read from CSV file in Android | en.proft.me
public class MainActivity extends AppCompatActivity { RatingBar ratingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<String[]> rows = new ArrayList<>(); CSVReader csvReader = new CSVReader(RatingActivity.this, "movies.csv"); try { rows = csvReader.readCSV(); } catch (IOException e) { e.printStackTrace(); } for (int i = 0; i < rows.size(); i++) { Log.d(Constants.TAG, String.format("row %s: %s, %s", i, rows.get(i)[0], rows.get(i)[1])); } } }
🌐
YouTube
youtube.com › brian fraser
Read CSV Resource File: Android Programming - YouTube
Import a CSV file into an Android Application from the raw resource folder and store the contents in custom data objects for use in Java. Steps: 1. Have data...
Published   February 26, 2017
Views   64K
Top answer
1 of 7
49

Try OpenCSV - it will make your life easier.

First, add this package to your gradle dependencies as follows

implementation 'com.opencsv:opencsv:4.6'

Then you can either do

import com.opencsv.CSVReader;
import java.io.IOException;
import java.io.FileReader;


...

try {
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + nextLine[1] + "etc...");
    }
} catch (IOException e) {

}

or

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();

Edit after comment

try {
    File csvfile = new File(Environment.getExternalStorageDirectory() + "/csvfile.csv");
    CSVReader reader = new CSVReader(new FileReader(csvfile.getAbsolutePath()));
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + nextLine[1] + "etc...");
    }
} catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show();
}

If you want to package the .csv file with the application and have it install on the internal storage when the app installs, create an assets folder in your project src/main folder (e.g., c:\myapp\app\src\main\assets\), and put the .csv file in there, then reference it like this in your activity:

String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "csvfile.csv"
File csvfile = new File(csvfileString);
2 of 7
11

The following snippet reads a CSV file from the raw resources folder (which will be packed into your .apk file upon compilation).

Android by default does not create the raw folder. Create a raw folder under res/raw in your project and copy your CSV File into it. Keep the name of the CSV file lower case and convert it into text format when asked. My CSV file name is welldata.csv.

In the snippet, WellData is the model class (with constructor, getter and setter) and wellDataList is the ArrayList to store the data.

private void readData() {
    InputStream is = getResources().openRawResource(R.raw.welldata);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8")));
    String line = "";

    try {
        while ((line = reader.readLine()) != null) {
           // Split the line into different tokens (using the comma as a separator).
            String[] tokens = line.split(",");

            // Read the data and store it in the WellData POJO.
            WellData wellData = new WellData();
            wellData.setOwner(tokens[0]);
            wellData.setApi(tokens[1]);
            wellData.setLongitude(tokens[2]);
            wellData.setLatitude(tokens[3]);
            wellData.setProperty(tokens[4]);
            wellData.setWellName(tokens[5]);
            wellDataList.add(wellData);

            Log.d("MainActivity" ,"Just Created " + wellData);
        }
    } catch (IOException e1) {
        Log.e("MainActivity", "Error" + line, e1);
        e1.printStackTrace();
    }
}
🌐
Reddit
reddit.com › r/godot › how to create csv file in android?
r/godot on Reddit: How to create CSV file in Android?
October 29, 2023 -

Hello. I'm new here and programming.

I am trying to make simple android acceleration recording app in godot.

I want to save recordings in csv so I coded as below.

However, it seems like that creating a folder in directory works but creating file does not work.

Same code with changed path (/storage/emulated/0/->C:/) works in my PC.

Should I set permission to get when I am exporting project to android?

Many thanks.

func _ready():

# curr_drive=DirAccess.get_drive_name(0)

csv\_path="/storage/emulated/0/Android/data/org.godotengine.gravaxel/csv"

func _on_record_button_toggled(button_pressed):

if button\_pressed==true:

	is\_recording=true

	file\_name=Time.get\_datetime\_string\_from\_system()+".csv"

	file\_name=file\_name.replace(":", "\_")

	file\_name=file\_name.replace("T", "\_")

	$UI\_Canvas/Record\_Control/file\_path\_label.text=str(csv\_path+"/"+file\_name)

elif button\_pressed==false:

	DirAccess.make\_dir\_recursive\_absolute(csv\_path)

	var [file=FileAccess.open](https://file=FileAccess.open)(csv\_path+"/"+file\_name, FileAccess.WRITE\_READ)

	file.store\_csv\_line(header\_array)

	\*\*\* writing functions \*\*\*\*\*\*\*\*\*

	file.flush()

	file.close()

Find elsewhere
🌐
Aknay
aknay.github.io › 2018 › 09 › 28 › how-to-open-a-csv-file-from-android.html
How to open a CSV file from Android App
Once the string data is extracted from the CSV file, we use joinToString function to display on text view mTextViewCsvResult. You can assign any number to READ_REQUEST_CODE. Doesn’t has to be 123 in this example. But when you have multiple intents (eg. open image files, open PDF files) then you need to assign a unique number to each intent and act on it based on that number. We don’t have much to explain here. Just take note of the android:id for MainActivity.
🌐
GitHub
github.com › lbferreira › Android-CSV-Writer
GitHub - lbferreira/Android-CSV-Writer: A simple code to help to write a csv file in an android application.
A simple code to help create a csv file in an android application ... DON'T FORGET TO ADD THIS PERMISSION TO ANDROID MANIFEST <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> private void createCsvFileExample1() { String foldeName = "MyAppFolder"; String fileName = "myFile"; WriteCSVHelper writeCSVHelper = new WriteCSVHelper(foldeName, fileName, WriteCSVHelper.SEMICOLON_SEPARATOR); String[] header = {"first col", "second col", "third col"}; String[] textCSV = {"first cell", "second cell", "third cell"}; writeCSVHelper.writeLine(header); writeCSVHelper.writeLine(textC
Author   lbferreira
🌐
GitHub
github.com › Hafiz-Waleed-Hussain › CSV-Writer-For-Android › blob › master › src › com › csvwritter › MainActivity.java
CSV-Writer-For-Android/src/com/csvwritter/MainActivity.java at master · Hafiz-Waleed-Hussain/CSV-Writer-For-Android
+ "/" + "Android/data/" + getPackageName().toString()); · // If Directory not exist then create · if (!mainDirect.exists()) if (mainDirect.mkdir()) ; · // Here we are creating CSV file on SD Card · desFile = new File(mainDirect + "/" + "CSV.csv"); · if (!desFile.exists()) { // Here only i check if the file is already exist than we not write ·
Author   Hafiz-Waleed-Hussain
🌐
Gadgets To Use
gadgetstouse.com › home › 3 ways to open and edit csv files on android phone
3 Ways to Open and Edit CSV Files on Android Phone - Gadgets To Use
October 4, 2022 - Follow these easy steps to read and edit your CSV file using this app from your phone. Open Google Play Store, search for the CSV Viewer app and install it. Provide the necessary access permissions required for the app.
🌐
Blogger
sachindroiddeveloper.blogspot.com › 2013 › 08 › how-to-read-and-write-csv-file-in.html
Droid Development Tips And Tricks: How To Read And Write CSV File In Android.
August 2, 2013 - Hello Droid lover today we are going to solve a problem related file read and write there are following step to read and write a Csv F...
🌐
YouTube
youtube.com › watch
Read CSV File From assets in Android Kotlin || In Just 3 Steps. - YouTube
#android #androidtutorialsHello Everyone I hope you all are doing good.Today we are doing to explore Pinterest Bottom Navigation UI in Android. Watch the Vid...
Published   April 28, 2021
🌐
MESCIUS
developer.mescius.com › blogs › how-to-read-and-write-to-csv-files-using-xuni-flexgrid-for-android
How to read and write to CSV files using Xuni FlexGrid for Android
October 7, 2015 - Saving the data from our grid back to CSV is essentially a reversal of the earlier process of reading the CSV. First, we provide a location to write the file. For this example, we're going to save the file to external storage, since this allows our saved CSV to potentially be used by other applications. (Internal storage is also be a possibility, but since this limits access to the saved CSV to our application, we won't pursue that option here.) Android requires that you give it permission to write to external storage by adding a line to the manifest:
🌐
YouTube
youtube.com › dave jones
Android Mobile Development - Lecture 13 - Saving Log data to CSV file - YouTube
Lectures from my CIS 218 - Mobile II class at Spokane Community College.Some of the code can be found at : https://github.com/lockersoft/cis218Subscribe for ...
Published   December 2, 2013
Views   16K
🌐
YouTube
youtube.com › watch
Read/Write file in Android Studio - YouTube
Video shows you how to read and write file in your Android app. You can use this to create simple app for creating notes and memos. This method creates hidde...
Published   September 19, 2016