0

Can anyone help me how to change a line in a file using java IO function. The file is located at SDCard. I have tried some solution from different blog, but failed.

I need to change one attribute wpa_oper_channel=1 to 2,3,4..... as user demand in the file sdcard/sample.txt.

I have also tried using SED command, but still not working. Please suggest if there any solution using java IO function.

The Command I have used using SED :

sed -i 's/wpa_oper_channel=[0-9]\\+/wpa_oper_channel=7/' sample.txt
6
  • 1
    please don't just describe what you have tried. Post the code, instead, and why it didn't work Commented Jun 11, 2015 at 7:20
  • So what's the issue? Commented Jun 11, 2015 at 7:21
  • I have updated the questions, please get the issue. I want to change the value after wpa_oper_channel= using java IO function, as sed is not working for Android. Commented Jun 11, 2015 at 7:30
  • possible duplicate of http://stackoverflow.com/questions/3147615/replace-string-in-file Commented Jun 11, 2015 at 7:49
  • @NayeemMorshedTushar Is there any information regarding file format? It seems like a properties file. If its a properties file you can read it and then change the property and then save it again. Commented Jun 11, 2015 at 7:49

3 Answers 3

0

If your configuration file is in the form of a Properties file. This means each line is in a format of key=value. You can easily read, modify and write it. Here is an example, I assume you have no problem with reading and writing a file through streams.

File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"sample.txt");
InputStream is = new FileInputStream(file); // Read your existing file
Properties props = new Properties();
props.load(is);
props.setProperty("wpa_oper_channel", "4");
OutputStream out = new FileOutputStream(file); // Your output file stream
prop.store(output, null); // Save your properties file without header

By doing this, you may lose if there is another type of line like comments or else.

Update

I updated to source code. But still you need to get read and write permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I haven't test the code, It will work with some exception handling. If it is not work please specify your error.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, Can you plz provide a working demo code? I think i am facing some consistency problem...
Wow... It works !!! But I have lost all other key=value properties like update_config=1 ap_scan=1 model_name=SAMSUNG_MOBILE etc. I need to change only wpa_oper_channel=4 keeping rest unchanged.
@NayeemMorshedTushar Sorry i forget to add load statement.
0

You just have to

  • read the file's content into memory (there are millions of examples on how to do that with java),
  • change the content (finding the position could be done using e.g. indexOf("wpa_oper_channel="))
  • write the content back to the file (there are millions of examples for that too)

Comments

0

What android do you use?

From android 4.4+ user haven't access to edit and remove files from SD Card. You must copy them to external storage and use there. See this link - http://code.google.com/p/android/issues/detail?id=67570

1 Comment

My Android version is jelly Bean 4.2.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.