package com.mafaz.csvcreatefile;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File folder = new File(Environment.getExternalStorageDirectory()+"/sensordata.csv");
//String location = "/InternalStrorage/newCsvFile.csv";
generateCsvFile(folder.toString());
}
private static void generateCsvFile(String fileName) {
FileWriter writer = null;
try {
writer = new FileWriter(fileName);
writer.append("Name");
writer.append(',');
writer.append("Number");
writer.append('\n');
writer.append("interview questions");
writer.append(',');
writer.append("001");
writer.append('\n');
writer.append("interview programs");
writer.append(',');
writer.append("004");
writer.append('\n');
System.out.println("CSV file is created...");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
No comments:
Post a Comment