java修改指定行,替换改行某内容

大家都遇到要查找行、查找有关键字的该行,替换改行中特定的位置的某内容,我这边是根据识别终端的型号、安卓版本,替换脚本中的需求,脚本内容大概如下:

# -*- coding:UTF-8 -*-
# -*- encoding:utf-8 -*-
from appium import webdriver
import os
import time
import configparser
import codecs
desired_caps = {}
desired_caps['platformName'] = 'android'
desired_caps['platformVersion'] = '5.0'
desired_caps['deviceName'] = 'GH500'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = 'com.android.settings.Settings'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(5)
driver.find_element_by_name("WLAN").click()
print ("点击进入WLAN界面")
time.sleep(2)
wifi_icon=driver.find_element_by_id("com.android.settings:id/switch_widget")
for n in range(0,200):
    print ("................................................................")

我们要根据机型去替换以下的5.0和GH500型号,这样需要到文件的读写,我们就要替换红色字体部分

desired_caps['platformVersion'] = '5.0'
desired_caps['deviceName'] = 'GH500'

我们点击连接终端可以自动读取终端的型号和安卓版本,效果截图和方法如下

 

 

// 按钮实现
		btnNewButton_connect.addActionListener(new ActionListener() {
			@SuppressWarnings("resource")
			public void actionPerformed(ActionEvent e) {
				Process process1 = null;
				Process process2 = null;
				try {

					process1 = Runtime.getRuntime().exec("adb devices");
					process2 = Runtime.getRuntime().exec(
							"adb shell getprop ro.build.version.release");// 这个直接出5.1.1

					try {
						process1.waitFor();
						process2.waitFor();
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				InputStreamReader isr1 = new InputStreamReader(process1.getInputStream());
				InputStreamReader isr2 = new InputStreamReader(process2.getInputStream());

				Scanner sc1 = new Scanner(isr1);
				Scanner sc2 = new Scanner(isr2);
				// while(sc.hasNext()){
				// 获取name
				String name1 = sc1.next();// 得到的结果List,不需要
				System.out.println(name1);
				String name2 = sc1.next();// 得到的结果of,不需要
				System.out.println(name2);
				String name3 = sc1.next();// 得到的结果devices,不需要
				System.out.println(name3);
				String name4 = sc1.next();// 得到的结果attached,不需要
				System.out.println(name4);
				String name = sc1.next();// 获取第5行终端name,如GH880
				System.out.println(name);
				// 获取textField_version
				String version = sc2.next();// 获取version。如:5.1.1,若是过长取5.1.1即可
				System.out.println(version);

				// }
				// 把获取终端的name放入到文本框中显示,GH880
				// textField_name.setText("");
				textField_name.setText(name);
				String strPath1 = name;
				System.out.println(strPath1);
				// 把获取终端的version放入到文本框中显示,5.1.1
				// textField_version.setText("");
				// textField_version.setText(version.substring(25));//version.substring(25)是获取第26行开始(前面25位),如:5.1.1
				textField_version.setText(version);
				String strPath2 = version;
				System.out.println(strPath2);

			}
		});

 

然后文件替换改行特定的字符串内容如下(其实我也是找到改行的位置,全部替换):

 

// 按钮实现
		
		btnNewButton_modifiy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String path = getScriptFilePath();
				System.out.println("path日:" + path);
				String version = getScriptVersion();
				System.out.println("version日:" + version);
				String name = getScriptName();
				System.out.println("name日:" + name);
				
				try {
					
				if(version.equals("") || name.equals("")){		 
					JOptionPane.showConfirmDialog(p,"请先连接终端","配置参数为空",0);
				}else{
					  if(path.equals("")){
						 JOptionPane.showConfirmDialog(p,"请先选择可执行脚本","脚本为空",0);
				      }else{
					File file = new File(path);// 建立一个file对象,参数就是你想访问文件的路径,这里我就不验证文件是否存在了
					BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));// 查下api即可
					StringBuffer outBuf = new StringBuffer();
					String line = null;
					 while ((line =br.readLine()) != null) {
						
						System.out.println("测试");
//						System.out.println("read line:"+line);	//控制台输出脚本内容显示						
						
						int index = line.indexOf("platformVersion");// 查找关键字,看此行的是否包含platformVersion
						if (index != -1) {
							line = "desired_caps['platformVersion'] = " + "'" + version + "'";//直接修改该行,把version替换即可
						} 	
						
						int index1 = line.indexOf("deviceName");// 看此行的是否包含deviceName
						if (index1 != -1) {
							line = "desired_caps['deviceName'] = " + "'"+ name + "'";//直接修改该行,把name替换即可
						}	
						
						outBuf.append(line+"\n");//
					}
					br.close();
					PrintWriter pw = new PrintWriter(file);// 建立一个输出流,把东西写入文件
					pw.write(outBuf.toString());
					
					pw.close();
				   }
				  }
					
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});

 

你可能感兴趣的:(Java)