编辑位于 /root/data/misc/wifi/p2p_supplicant.conf 的文件 p2p_supplicant.conf 中的值
editing a value in the file p2p_supplicant.conf which is located on /root/data/misc/wifi/p2p_supplicant.conf
我正在尝试开发一个功能来编辑根文件 p2p_supplicant.conf 中的值,该文件位于 /root/data/misc/wifi/p2p_supplicant.conf
toast 消息始终显示 "File Not Found"
我的代码是:
private static final String FILE_PATH = "/root/data/misc/wifi/p2p_supplicant.conf";
private static final String MARKER_LINE = "p2p_oper_channel=";
private static final String TEXT_TO_ADD = "11";
public void changeConfig() {
String message = String.format("Entering Config Class");
Toast.makeText(getApplicationContext(), message,Toast.LENGTH_LONG).show();
List<String> fileLines = new ArrayList<String>();
Scanner scanner = null;
try {
scanner = new Scanner(new File(FILE_PATH));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
fileLines.add(line);
if (line.trim().equalsIgnoreCase(MARKER_LINE)) {
fileLines.add(TEXT_TO_ADD);
String message2 = String.format("File Written");
Toast.makeText(getApplicationContext(), message2,Toast.LENGTH_LONG).show();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
String message1 = String.format("File Not found");
Toast.makeText(getApplicationContext(), message1,Toast.LENGTH_LONG).show();
} finally {
if (scanner != null) {
scanner.close();
}
}
PrintWriter pw = null;
try {
pw = new PrintWriter(new File(FILE_PATH));
for (String line : fileLines) {
pw.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}
}
但是代码没有找到文件的location/path。请建议。
N.B。我的 phone 已 root。
基本上,您正在读取文件、更改频道然后重写整个文件。我正在寻找一种解决方案,您可以先读取文件,对其进行编辑,然后将其写回。解决该问题后,我将在此处编辑我的答案。
请注意,在测试之前,请为您的 wpa_supplicant.conf 文件制作一个备份文件,以防发生错误。
第 1 步:读取文件 = 检查此解决方案:
第 2 步:遍历包含文件文本的字符串并更改操作频道并将整个文本保存在 String text
第 3 步:将 text
写入文件
Process p;
try {
// Preform su to get root privileges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \""+text+"\" > /data/misc/wifi/p2p_supplicant.conf\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
// TODO Code to run on success
}
else {
// TODO Code to run on unsuccessful
}
} catch (InterruptedException e) {
// TODO Code to run in interrupted exception
}
} catch (IOException e) {
// TODO Code to run in input/output exception
}
我正在尝试开发一个功能来编辑根文件 p2p_supplicant.conf 中的值,该文件位于 /root/data/misc/wifi/p2p_supplicant.conf
toast 消息始终显示 "File Not Found" 我的代码是:
private static final String FILE_PATH = "/root/data/misc/wifi/p2p_supplicant.conf";
private static final String MARKER_LINE = "p2p_oper_channel=";
private static final String TEXT_TO_ADD = "11";
public void changeConfig() {
String message = String.format("Entering Config Class");
Toast.makeText(getApplicationContext(), message,Toast.LENGTH_LONG).show();
List<String> fileLines = new ArrayList<String>();
Scanner scanner = null;
try {
scanner = new Scanner(new File(FILE_PATH));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
fileLines.add(line);
if (line.trim().equalsIgnoreCase(MARKER_LINE)) {
fileLines.add(TEXT_TO_ADD);
String message2 = String.format("File Written");
Toast.makeText(getApplicationContext(), message2,Toast.LENGTH_LONG).show();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
String message1 = String.format("File Not found");
Toast.makeText(getApplicationContext(), message1,Toast.LENGTH_LONG).show();
} finally {
if (scanner != null) {
scanner.close();
}
}
PrintWriter pw = null;
try {
pw = new PrintWriter(new File(FILE_PATH));
for (String line : fileLines) {
pw.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}
}
但是代码没有找到文件的location/path。请建议。 N.B。我的 phone 已 root。
基本上,您正在读取文件、更改频道然后重写整个文件。我正在寻找一种解决方案,您可以先读取文件,对其进行编辑,然后将其写回。解决该问题后,我将在此处编辑我的答案。 请注意,在测试之前,请为您的 wpa_supplicant.conf 文件制作一个备份文件,以防发生错误。
第 1 步:读取文件 = 检查此解决方案:
第 2 步:遍历包含文件文本的字符串并更改操作频道并将整个文本保存在 String text
第 3 步:将 text
写入文件
Process p;
try {
// Preform su to get root privileges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \""+text+"\" > /data/misc/wifi/p2p_supplicant.conf\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
// TODO Code to run on success
}
else {
// TODO Code to run on unsuccessful
}
} catch (InterruptedException e) {
// TODO Code to run in interrupted exception
}
} catch (IOException e) {
// TODO Code to run in input/output exception
}