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 OverflowTo 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(";");
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
java - Reading CSV File In Android App - Stack Overflow
How to create CSV file in Android?
How to read csv file in android? - Stack Overflow
How to export data to csv file in Android? - Stack Overflow
Videos
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);
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();
}
}
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()
There are file explorer style apps that will open it. Or you could download Csv Viewer app from Play Store.
Blurb from it.
CSV File Viewer For Android | CSV Reader - view small and large-sized CSV files.
CSV Reader - Simple, fast, and powerful tool for reading csv files.
I am not associated with it.
Any office suite should be able to open it. I recommend using CollaboraOffice (https://play.google.com/store/apps/details?id=com.collabora.libreoffice&hl=en_US), I successfully opened .csv files with it in table format and can also edit it. The app has no ads and it is open source.
(I am not promoting/endorsing the app)