繁体   English   中英

通过代码编辑Linux网络配置文件(“ / etc / network / interfaces”)

[英]Edit Linux Network Configuration File(“/etc/network/interfaces”) through code

我想在App中添加一个功能,用户可以永久更改机器IP方案(IP,SubnetMask,DefaultGateway),因此我想对Linux网络配置文件(“ / etc / network / interfaces”)执行读/写操作)使用以下代码。

 File file = new File("/etc/network/interfaces");
 boolean exists = file.exists();
 String line = "";

 FileWriter fw = new FileWriter(file.getAbsoluteFile());
 BufferedWriter bw = new BufferedWriter(fw);

 try
 {
    FileReader fr = new FileReader(file.getAbsoluteFile());
    //BufferedReader br = new BufferedReader(fr); 
    Scanner scan = new Scanner(new FileInputStream(file));

    if(exists)
    {
        while(scan.hasNext())     //while((line = br.readLine()) != null)
        {
            // Any Write operation                  
        }
        scan.close();             // br.close
    }
 }
bw.close();

问题是while()循环上的检查始终返回false。 我对任何替代方法进行了研究,包括使用BufferedReader或Scanner读取文件,但没有用。 以下所有检查仅会返回false。

while(scan.hasNext())
while(scan.hasNextLine())
while((line = br.readLine()) != null)

虽然文件确实存在,但它包含其内容,但是每次我尝试使用上述代码读取它时,所有文件内容都将被删除,文件将为空。

我想念什么吗? 还有更好的选择吗? 我还尝试读取同一目录中的另一个文件,该文件对所有用户具有完全读/写/执行权限,但结果仍然相同

当我试图打开文件进行读写时,是导致问题的原因,并且循环在开始时就终止了。 因此事实证明,对于同一文件,您不应该在FileReader之前使用FileWriter 这样做会同时导致文件读取器读取空文件,并且循环在开始时正确获取EndOfFile时终止。 之后,它将关闭文件为空,因此其所有内容均丢失。

更好的方法是

  • 首先打开“只读”文件。
  • 逐行扫描文件并保持解析的每一行的缓冲区(在我的情况下为清单)。
  • 到达“标记”行时,将要更新的内容添加到文件中,同时也更新缓冲区。
  • 现在打开文件以“写入”更新列表

注意:如果文件大小相当小以适应文件处理时间,则此方法适用。

File file = new File("/etc/network/interfaces");
boolean exists = file.exists();
Scanner scanner = null;
PrintWriter wirtter = null;
String line = "";
List<String> fileLines = new ArrayList<String>();

if(exists)
{
try {
    scanner = new Scanner(new FileInputStream(file));

    while(scanner.hasNextLine())
    {
        line = scanner.nextLine();
        fileLines.add(line);
        if(line.trim().startsWith("iface eth0 inet static"))
        {
            while(scanner.hasNextLine())
            {
                line = scanner.nextLine();
                fileLines.add(line);

                if(line.trim().startsWith("address"))
                {
                    String updateStr = "\taddress "+ipAddress+"\t";
                    fileLines.remove(fileLines.size()-1);
                    fileLines.add(updateStr);
                    System.out.println("IP add updated");
                }
                else if(line.trim().startsWith("netmask"))
                {
                    String updateStr = "\tnetmask "+subnetMask+"\t";
                    fileLines.remove(fileLines.size()-1);
                    fileLines.add(updateStr);
                    System.out.println("subnet add updated");
                }
                else if(line.trim().startsWith("gateway"))
                {
                    String updateStr = "\tgateway "+defaultGateway+"\t";
                    fileLines.remove(fileLines.size()-1);
                    fileLines.add(updateStr);
                    System.out.println("Gatway add updated");
                }

            }
        }   
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
finally{
    if(scanner != null)
        scanner.close();
}

现在分别进行写作。 而且您还想重新启动网络服务

try {
     wirtter = new PrintWriter(file);
     for (String lineW : fileLines) 
        wirtter.println(lineW);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
finally{
    if(wirtter != null)
        wirtter.close();
}
}

synchronized (p) {
    String cmd = "sudo /etc/init.d/networking restart ";
    p.exec(cmd);
    p.wait(10000);
    System.out.println("finishing restart 'Networking:' service");

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM