In all versions, i am able to get SdCard details like serial no., CID number .. except Nougot version.
I am using the below methods to get the SdCard details. Both are working fine all versions except Nougat. Those paths are returning null in nougat version
public void getCID() {
File input = new File("/sys/class/mmc_host/mmc1");
String cidDirectory = null;
File[] sid = input.listFiles();
for (int i = 0; i < sid.length; i++) {
if (sid[i].toString().contains("mmc1:")) {
cidDirectory = sid[i].toString();
String SID = (String) sid[i].toString().subSequence(cidDirectory.length() - 4, cidDirectory.length());
}
}
try {
BufferedReader CID = new BufferedReader(new FileReader(cidDirectory + "/cid"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String getSDCARDiD() {
try {
File file = new File("/sys/block/mmcblk1");
if (file.exists() && file.isDirectory()) {
memBlk = "mmcblk1";
} else {
//System.out.println("not a directory");
memBlk = "mmcblk0";
}
Process cmd = Runtime.getRuntime().exec("cat /sys/block/" + memBlk + "/device/cid");
BufferedReader br = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
sd_cid = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return sd_cid;
}
Why it is giving null i didn't understand.
Thanks in advance
Above 7.0 we have to use StorageVolume.getUuid() on StorageVolume which you get from StorageManager.
Related
Trying to create a csv file reader, but the reader gets an ArrayIndexOutOfBoundsException when it comes across an end of line blank.
Example: Name, Email, Age, City
John, johnsmith#email, 23, New York - This works and is standard
John, johnsmith#email, 23, - This fails
,,23,New York - This works
John, johnsmith#email,, - This fails
Any additional feedback is welcome as well!
Here's the code.
public class Main {
static String filepath = "filepath.csv";
public static void main(String[] args) {
SQLiteDB db = new SQLiteDB();
CSVReader(filepath);
}
public static void CSVReader(String filepath) {
List<User> users = new ArrayList<User>();
BufferedReader br = null;
String line;
int count = 1;
try {
br = new BufferedReader(new FileReader(filepath));
while ((line = br.readLine()) != null) {
if(line.trim().length() > 0) {
User user = new User();
String[] userinfo = line.split(",(?=([^\"]|\"[^\"]*\")*$)");
user.firstName = userinfo[0];
user.lastName = userinfo[1];
user.email = userinfo[2];
user.gender = userinfo[3];
user.image = userinfo[4];
user.bank = userinfo[5];
user.transaction = userinfo[6];
user.bool1 = userinfo[7];
user.bool2 = userinfo[8];
user.city = userinfo[9];
users.add(user);
System.out.println("Count:" + count + " " + user.getBool2());
count++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
For the city value, just use the ternary operator to check the array length before assignment:
user.city = userinfo.length == 10 ? userinfo[9] : "";
If this is not a homework project, why don't you use a library? There are several on GitHub, and here is one that seems easy to use - https://github.com/osiegmar/FastCSV.
I'm trying to read a .txt file called Heights.txt, which contains a string of numbers, each separated by a ":". The method produces one error that I can't seem to figure out.
It says that "the method must return a result of type int[]", at the very first line of this code.
I don't understand why it says this, as integerHeightDataPoints should be an integer array at that point, and should be able to be returned to a int[] method?
public static int[] readFile(){
BufferedReader br = null;
String dataPoints;
try {
br = new BufferedReader(new FileReader("Path\\Heights.txt"));
}
catch(IOException e) {
System.out.println("Please enter data first");
System.exit(0);
}
try {
while((dataPoints = br.readLine()) != null) {
if (dataPoints.contains(":")) {
String[] heightDataPoints = dataPoints.split(":");
int[] integerHeightDataPoints = new int[heightDataPoints.length];
for (int i = 0; i < integerHeightDataPoints.length; i++) {
integerHeightDataPoints[i] = Integer.parseInt(heightDataPoints[i]);
}
return integerHeightDataPoints;
}
}
}
catch (IOException e) {
System.out.println("Error reading file");
e.printStackTrace();
}
}
It's because you don't return anything in second IOException case or (as #Exception_al mentioned) when while never triggers.
public static int[] readFile() {
BufferedReader br = null;
String dataPoints;
try {
br = new BufferedReader(new FileReader("/tmp/file1"));
} catch (IOException e) {
System.out.println("Please enter data first");
System.exit(0);
}
int[] integerHeightDataPoints = new int[0];
try {
while ((dataPoints = br.readLine()) != null) {
if (dataPoints.contains(":")) {
String[] heightDataPoints = dataPoints.split(":");
integerHeightDataPoints = new int[heightDataPoints.length];
for (int i = 0; i < integerHeightDataPoints.length; i++) {
integerHeightDataPoints[i] = Integer.parseInt(heightDataPoints[i]);
}
return integerHeightDataPoints;
}
}
} catch (IOException e) {
System.out.println("Error reading file");
e.printStackTrace();
}
return integerHeightDataPoints;
}
Being a beginner in programing I'm actually writing a program to practice, wich use url.open to download pages on a forum but after 88-90 loops the url.openstream became very slow and slow down all the program's execution and i don't know why. i searched on internet the explanations but not found it.
private Set<utilisateur> arbretemp = new HashSet<utilisateur>();
private ArrayList<String> arbretempString = new ArrayList<String>();
private ArrayList<utilisateur> tempArray = new ArrayList<utilisateur>();
private String lienPartie1;
private String lienPartie2;
private int nbrePage;
private String nom;
--
String temporaire = "";
for (int i = 1; i <= nbrePage; i++) {
String line = "";
String lien = lienPartie1 + i + lienPartie2;
bar.setValue(i);
try {
url = new URL(lien);
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
System.out.println("avant while");
while (((line = br.readLine()) != null)) {
System.out.println("debut de while");
if (((line.contains("alt=")) && line.contains("class=\"user-avatar-msg\""))) {
line = line.trim();
String[] temp = line.split("\"");
temporaire = temp[5] + " \r";
temp = temporaire.split("\"");
byte[] test = temporaire.getBytes();
if (!(temporaire.equalsIgnoreCase(""))) {
utilisateur utiltemp = new utilisateur(temporaire);
if (arbretemp.add(utiltemp)) {
} else {
arbretempString.add(temporaire);
}
}
}
}
System.out.println("fin de while");
} catch (MalformedURLException mue) {
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
String s = "page " + i + "/" + nbrePage;
}
System.out.println("fin de boucle");
Iterator it = arbretemp.iterator();
for (utilisateur a : arbretemp) {
// System.out.println(a.nom);
a.ajouterNbrePost(arbretempString);
}
TreeSet<utilisateur> salComp = new TreeSet<utilisateur>(new utilisateurComp());
salComp.addAll(arbretemp);
tempArray.addAll(arbretemp);
tempArray.sort(new utilisateurComp());
}
as you see it's in french but i hope you will understand anyway.
thanks for your reply and reading
Part of my homework.
I have written a method to split all words to ArrayList. Words are taken from all files in given project directory.
Unfortunately sometimes lines are skipped... and I wish to find the bug. Please help.
To specify: files are of 7 "words" separated with tabs in each line.
public class TravelData {
static List<String> tour = new ArrayList<String>(); //lista zlokalizowana według nagłówka wiersza
public TravelData(File dataDir) {
String currentDirPath = new File(dataDir.toString()).getAbsolutePath();
File currentDir = new File(currentDirPath);
File[] listOfFiles = currentDir.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
try {
Scanner s = new Scanner(new File(listOfFiles[i].toString()));
while (s.hasNextLine()){
ArrayList<String> line = new ArrayList<String>();
for(String value: s.nextLine().split("\t"))
{
line.add(value);
}
lineConverter(line, dbDate); //do something with grabbed data
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
//[...]
}
I've personally not used Scanners much, so I can't immediately spot the issue. But here is some old code using buffered file input stream that I've added your specific bits to:
public TravelData(File dataDir) {
File[] listOfFiles = dataDir.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
InputStream inputStream = null;
BufferedReader buffReader = null;
try {
inputStream = new FileInputStream(listOfFiles[i]);
buffReader = new BufferedReader(new InputStreamReader(inputStream));
String fileLine = buffReader.readLine();
while(fileLine != null) {
ArrayList<String> line = new ArrayList<String>();
for(String value: fileLine.split("\t")) {
line.add(value);
}
lineConverter(line, dbDate);
fileLine = buffReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(buffReader != null) try { buffReader.close(); } catch (IOException e) { }
if(inputStream != null) try { inputStream.close(); } catch (IOException e) { }
}
}
}
}
I'm completely at a lose for why this isn't working. I've had similar loops before and they've worked fine.
try{
text = new BufferedReader(new FileReader(fileName));
while ((lineOfText = text.readLine()) != null){
StringTokenizer tokens = new StringTokenizer(lineOfText," , .;:\"&!?-_\n\t12345678910[]{}()##$%^*/+-");
while (tokens.hasMoreTokens()){
countTotalWordsInDocument++;
String word = tokens.nextToken();
countTotalCharactersInAllWords = countTotalCharactersInAllWords + word.length();
}
}
text.close();
} catch (IOException e) {
System.out.println("file not found");
}