Change the model in build.prop - android

I would change the name of the model programmatically and I have root permissions, but my code does not work well and do not understand the problem.
The second toast give me this message "Error2:File/system/build.prop/ro.product.model open failed: ENOTDIR (Not a directory)"
code:
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount rw /system/\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error1: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
File file=new File("/system/build.prop/ro.product.model");
try {
fis = new FileInputStream(file);
String content = "xx";
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {}
content += new String(input);
DataOutputStream outstream= new DataOutputStream(new FileOutputStream(file,false));
String body = content;
outstream.write(body.getBytes());
outstream.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error2: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

Replace the line FileInputStream fis = openFileInput("/system/build.prop/ro.product.model"); by:
FileInputStream fis = FileInputStream(file);
for security reasons openFileInput is not allowed to accept seperators in the file name. But you can create a File with seperators in the path and create a FileImputStream from the file.

There is another problem in your porgram, the model name is not located in a file called "/system/build.prop/ro.product.model", but is defined by the line "ro.product.model=" in the file "/system/build.prop".. Be careful when editing this file!!

Related

Android, Why Can't I Read the File I Just Wrote?

I am using the first snippet to write a file.
String fileName = "Test6.txt";
String outputString="Text for File";
try {
FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(outputString.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
And the second to read it.
try{
FileInputStream InputStream = openFileInput("Text6.txt");
InputStreamReader inputStreamReader = new InputStreamReader(InputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineData = bufferedReader.readLine();
}catch(FileNotFoundException ex)
{
Log.d(TAG, ex.getMessage());
}
catch(IOException ex) {
Log.d(TAG, ex.getMessage());
}
But I can't read it, I get:
java.io.FileNotFoundException: /data/user/0/com.example.android.buildingmarque2/files/Text6.txt (No such file or directory)
I can also get a list of the files and Test6.txt is in the list.
Also, Android Studio Device File Explorer shows it.
It could be a problem with the path.
Device Explorer, "Copy Path" gives me
/data/data/com.example.android.buildingmarque2/files/Test6.txt
But the Log says:
/data/user/0/com.example.android.buildingmarque2/files/Text6.txt
I'm confused?
Typo. One is "Text6" the other is "Test6". Use a constant for both names to avoid this in the future

Android write text in file Sd card

i try to write text in file.i wrote code ,witch can to write text ,but if i will use again my code again text is rewrite in file.for example if i first time write "Hello android" and then "Sir",result is only "Sir".i want "Hello android Sir"
your_file = new File("/sdcard/facebookUser");
try {
Writer writer = new OutputStreamWriter(new FileOutputStream(
your_file), "UTF-8");
writer.write(facebook_user_name + ",");
writer.write(facebook_id);
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
how i can write code to save another text in this file second time?
new FileOutputStream(your_file, true)
public FileOutputStream(String name, boolean append) throws FileNotFoundException
append - if true, then bytes will be written to the end of the file rather than the beginning
Instead of:
writer = new OutputStreamWriter(new FileOutputStream(
your_file), "UTF-8");
Use:
writer = new OutputStreamWriter(new FileOutputStream(
your_file, true), "UTF-8");
This sets the FileOutputStream in append mode.
java.io.FileOutputStream.FileOutputStream(File file, boolean append)
throws FileNotFoundException
Constructs a new FileOutputStream that writes to file. If append is
true and the file already exists, it will be appended to; otherwise it
will be truncated. The file will be created if it does not exist.
try {
int n = 0;
String Name = "file";
File myFile = new File("/sdcard/test/");
if (!myFile.exists()) {
boolean b = myFile.mkdirs(); }
myFile = new File("/sdcard/test/"+Name+".txt");
while (myFile.exists())
{
myFile = new File("/sdcard/test/"+Name+n+".txt");
n++;
}
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
String str = "Your text to write";
myOutWriter.append(str);
myOutWriter.close();
fOut.close();
} catch (Exception e) {}
And do not forget permision:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Vladimir Kulyk and user2450263 are correct! You have to write a file in append mode. try this link.

Reading logs with filters programmatically

I am trying to read the user entered logs using log.i() programmatically as follows,
try {
Log.i("logs", "inside log.java");
String filter = "logcat ";
filter += "-d ";
filter += "logs:I";
// String[] command = new String[] { "logcat", "-s" , "logs:"+filter
// };
Process process = Runtime.getRuntime().exec(filter);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(onCleared);
tv = (TextView) findViewById(R.id.logview);
tv.setText(log.toString());
} catch (IOException ex) {
Log.e("Logging", ex.toString());
}
// convert log to string
final String logString = new String(log.toString());
// create text file in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");
try {
// to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(logString);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
but it simply shows a black screen . but when i try to run the command in adb shell it works . my adb code is,
adb logcat -s logs:I
I was facing a similar problem while trying to log files to text programmatically. I think what's happening here is that the log reading process is blocking the UI (main) thread, resulting in the black screen.
Try putting the entire log reading process into an ASyncTask. That made it work for me!
Did you add this to your manifest file?
<uses-permission android:name="android.permission.READ_LOGS" />

How to append data to text files in the new line, ( \n does not work)

I am using this code as a part to write data to a text file, but I am not sure how I can append the data to the new line. "\n" does not seem to be working. Here, variable "data" has this format:
t=1, x=-3.1, y=19.0, z=-8.6
this part of the code iterates and the variable "data" is written each time; the current result is something like:
t=1, x=-6.9, y=-9.6, z=-6.9t=2, x=-1.4, y=6.2, z=7.0t=3, x=-1.4, y=6.1, z=6.9t=4, and so on, but what I would like is:
t=1, x=-6.9, y=-9.6, z=-6.9
t=2, x=-1.4, y=6.2, z=7.0
t=3, x=-1.4, y=6.1, z=6.9
Thanks in advance for your help.
String datatest = data.toString();
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/test");
directory.mkdirs();
String filename = "test.txt";
File file = new File(directory, filename);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(fOut);
try {
osw.write(datatest + "\n");
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
try "\r\n"
and to make this answer longer :)

getAbsolutePath() displays nothing?

I'm trying to display the path of the file by calling getAbsolutePath(), but the Application
displays nothing.
Java Code:
public void createExternalStorageDirectory() {
File file = new File(getExternalFilesDir(null), fileName);
try {
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
Toast.makeText(getBaseContext(), file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
is.close();
os.close();
} catch (IOException e) {
Log.w("ExternalStorage", " Error writing " + file, e);
}
}
Add the External File permission to the manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And try using getApplicationContext() intead of getBaseContext()
you can try to use Environment.getExternalStorageDirectory() instead of getExternalFilesDir(null)

Categories

Resources