java实现动态双卡手机怎么切换上网网IP

语法:&rasdial&&连接名称&username&password &&
实例:&rasdial&我的宽带&hzhz&dfdfdfdfdf&&
语法:rasdial&&连接名称&/disconnect&& &&
实例:&rasdial&宽带&&/disconnect&&&&
java程序调用rasdial命令:(其中读取CMD返回消息时可能会乱码,注意红色字体那一句的编码设置)
package com.sesame.import java.io.BufferedRimport java.io.InputStreamRpublic class ConnectNetWork {
* 执行CMD命令,并返回String字符串
public static String executeCmd(String strCmd) throws Exception {
Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
StringBuilder sbCmd = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream(),"GB2312"));  //这里很重要,设置GB2312解决乱码!!!                             //如果程序默认编码就是GB2312,可以不写                             //我NetBeans默认用UTF8
while ((line = br.readLine()) != null) {
sbCmd.append(line + "\n");
return sbCmd.toString();    //如果整个过程换成这样,就更清楚了。getInputStream是获取最原始的字节流,    //cmd返回的是以GB2312双字节编码的字节流    InputStream in = p.getInputStream();    byte[] b = new byte[1000];    in.read(b);    String msg = new String(b,"GB2312");  //用GB2312解释这堆字节,就可以组装成一个正常的String了                         //如果上边不写GB2312,等于这里用UTF8组装,结果一样
* 连接ADSL
public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {
System.out.println("正在建立连接.");
String adslCmd = "rasdial " + adslTitle + " " + adslName + " "
String tempCmd = executeCmd(adslCmd);
// 判断是否连接成功
if (tempCmd.indexOf("已连接") & 0) {
System.out.println("已成功建立连接.");
return true;
System.err.println(tempCmd);
System.err.println("建立连接失败");
return false;
* 断开ADSL
public static boolean cutAdsl(String adslTitle) throws Exception {
String cutAdsl = "rasdial " + adslTitle + " /disconnect";
String result = executeCmd(cutAdsl);
if (result.indexOf("没有连接")!=-1){
System.err.println(adslTitle + "连接不存在!");
return false;
System.out.println("连接已断开");
return true;
public static void main(String[] args) throws Exception {
connAdsl("宽带","hzhz**********","******");
Thread.sleep(1000);
cutAdsl("宽带");
Thread.sleep(1000);
//再连,分配一个新的IP
connAdsl("宽带","hzhz**********","******");
执行结果:
正在建立连接. &&
已成功建立连接. &&
连接已断开 &&
正在建立连接. &&
已成功建立连接.&&
小结:实现这个功能的最主要在于bat命令能支持这个功能,和以前写过的自动设置ip功能类似,这些功能实现java其实是很不方便的,看来要优雅的实现和windows操作系统相关的行为,学习windows编程才行。
重拨机制:
while(!connAdsl("宽带","hzhz**********","******")){ &&
&&&&&&&&&&&&&&Thread.sleep(1000); &&
阅读(...) 评论()(转) java实现动态切换上网IP (ADSL拨号上网)
- open mind - ITeye技术网站
java实现动态切换上网IP (ADSL拨号上网)
转自:/blog/434088
动态切换IP的实现主是也由Windows的rasdial命令提供的,其实不是java的功劳,java只是调用一下bat脚本而已:
rasdial命令:
语法: rasdial
连接名称 username password
实例: rasdial 我的宽带 hzhz dfdfdfdfdf
语法:rasdial
连接名称 /disconnect
实例: rasdial 宽带
/disconnect
java程序调用rasdial命令:
package com.sesame.
import java.io.BufferedR
import java.io.InputStreamR
public class ConnectNetWork {
* 执行CMD命令,并返回String字符串
public static String executeCmd(String strCmd) throws Exception {
Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
StringBuilder sbCmd = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = br.readLine()) != null) {
sbCmd.append(line + "\n");
return sbCmd.toString();
* 连接ADSL
public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {
System.out.println("正在建立连接.");
String adslCmd = "rasdial " + adslTitle + " " + adslName + " "
String tempCmd = executeCmd(adslCmd);
// 判断是否连接成功
if (tempCmd.indexOf("已连接") & 0) {
System.out.println("已成功建立连接.");
System.err.println(tempCmd);
System.err.println("建立连接失败");
* 断开ADSL
public static boolean cutAdsl(String adslTitle) throws Exception {
String cutAdsl = "rasdial " + adslTitle + " /disconnect";
String result = executeCmd(cutAdsl);
if (result.indexOf("没有连接")!=-1){
System.err.println(adslTitle + "连接不存在!");
System.out.println("连接已断开");
public static void main(String[] args) throws Exception {
connAdsl("宽带","hzhz**********","******");
Thread.sleep(1000);
cutAdsl("宽带");
Thread.sleep(1000);
//再连,分配一个新的IP
connAdsl("宽带","hzhz**********","******");
package com.sesame.
import java.io.BufferedR
import java.io.InputStreamR
public class ConnectNetWork {
* 执行CMD命令,并返回String字符串
public static String executeCmd(String strCmd) throws Exception {
Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
StringBuilder sbCmd = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = br.readLine()) != null) {
sbCmd.append(line + "\n");
return sbCmd.toString();
* 连接ADSL
public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {
System.out.println("正在建立连接.");
String adslCmd = "rasdial " + adslTitle + " " + adslName + " "
String tempCmd = executeCmd(adslCmd);
// 判断是否连接成功
if (tempCmd.indexOf("已连接") & 0) {
System.out.println("已成功建立连接.");
System.err.println(tempCmd);
System.err.println("建立连接失败");
* 断开ADSL
public static boolean cutAdsl(String adslTitle) throws Exception {
String cutAdsl = "rasdial " + adslTitle + " /disconnect";
String result = executeCmd(cutAdsl);
if (result.indexOf("没有连接")!=-1){
System.err.println(adslTitle + "连接不存在!");
System.out.println("连接已断开");
public static void main(String[] args) throws Exception {
connAdsl("宽带","hzhz**********","******");
Thread.sleep(1000);
cutAdsl("宽带");
Thread.sleep(1000);
//再连,分配一个新的IP
connAdsl("宽带","hzhz**********","******");
执行结果:
正在建立连接.&&
已成功建立连接.&&
连接已断开&&
正在建立连接.&&
已成功建立连接.&
如果你要重连功能的话,这样就可以了:
while(!connAdsl("宽带","hzhz**********","******")){
Thread.sleep(1000);
gezhicheng
浏览: 14912 次
来自: 北京JAVA编写的IP地址动态切换软件-android100学习网
JAVA编写的IP地址动态切换软件
一共有4个类  MainApplet.java   ConfigFile.java   ExecCmd.java  SystemVars.java下面是源代码:1. MainApplet.ja
一共有4个类& MainApplet.java&& ConfigFile.java&& ExecCmd.java& SystemVars.java下面是源代码:1. MainApplet.java类package com.zhangzhen.&import java.awt.BorderLimport java.awt.Cimport java.awt.FlowLimport java.awt.GridLimport java.awt.event.ActionEimport java.awt.event.ActionLimport java.ponentEimport java.ponentLimport java.awt.event.KeyEimport java.io.Fimport java.io.IOEimport java.util.ArrayLimport javax.swing.DefaultListMimport javax.swing.JAimport javax.swing.JLimport javax.swing.JLimport javax.swing.JPimport javax.swing.JBimport javax.swing.JScrollPimport javax.swing.JTextFimport javax.swing.ListSelectionMimport javax.swing.event.ListSelectionEimport javax.swing.event.ListSelectionLpublic class MainApplet extends JApplet{&&& private JButton jbtSave = new JButton("Save");&& && && private JButton jbtDelete = new JButton("Delete");&& && private JButton jbtOK = new JButton("OK");&& && private JButton jbtAuto = new JButton("Auto");&& && private JButton jbtExit = new JButton("Exit");&& && && private JLabel jlblIPAddress = new JLabel("IP地址:");&& && private JTextField jtfIPAddress = new JTextField();&& && private JLabel jlblSubnetMask = new JLabel("子网掩码: ");&& && private JTextField jtfSubnetMask = new JTextField();&& && private JLabel jlblGateway = new JLabel("默认网关:");&& && private JTextField jtfGateway = new JTextField();&& && private JLabel jlblDNSfirst = new JLabel("首选DNS服务器:");&& && private JTextField jtfDNSfirst = new JTextField();&& && private JLabel jlblDNSremark = new JLabel("备用DNS服务器:");&&& private JTextField jtfDNSremark = new JTextField();&& && && private JLabel jlblConfigFileName = new JLabel("配置项名称:");&& && private JTextField jtfConfgFileName = new JTextField();&& && && private JList jlConfigItem = new JList();&& && && DefaultListModel dlmConfigItem = new DefaultListModel();&& && && SystemVars configInfo = new SystemVars();&& && && &&& public void init()&&& {&&&& super.setSize();&&&&&&& setLayout(new BorderLayout());&&&&&& &&&&&&& //添加控制按钮面板&&&& JPanel jpControl = new JPanel();&&&&&&& jpControl.setLayout(new FlowLayout());&&&&&&& // 设置按钮属性&&&&&&& jbtSave.setMnemonic(KeyEvent.VK_S);&&&&&&& jbtDelete.setMnemonic(KeyEvent.VK_DELETE);&&&&&&& jbtOK.setMnemonic(KeyEvent.VK_O);&&&&&&& jbtAuto.setMnemonic(KeyEvent.VK_A);&&&&&&& jbtExit.setMnemonic(KeyEvent.VK_E);&&&&&&& //添加控制按钮&&&&&&& jpControl.add(jbtSave);&&&&&&& jpControl.add(jbtDelete);&&&&&&& jpControl.add(jbtOK);&&&&&&& jpControl.add(jbtAuto);&&&&&&& jpControl.add(jbtExit);&&&&&& &&&&&&& add(jpControl,BorderLayout.SOUTH);&&&&&& &&&&&&& //添加输入配置信息面板&&&&&&& JPanel jpEnterConfigInfo = new JPanel();&&&&&&& jpEnterConfigInfo.setLayout(new GridLayout(6,2,0,40));&&&&&& &&&&&&& jpEnterConfigInfo.add(this.jlblIPAddress);&&&&&&& jpEnterConfigInfo.add(this.jtfIPAddress);&&&&&&& jpEnterConfigInfo.add(this.jlblSubnetMask);&&&&&&& jpEnterConfigInfo.add(this.jtfSubnetMask);&&&&&&& jpEnterConfigInfo.add(this.jlblGateway);&&&&&&& jpEnterConfigInfo.add(this.jtfGateway);&&&&&&& jpEnterConfigInfo.add(this.jlblDNSfirst);&&&&&&& jpEnterConfigInfo.add(this.jtfDNSfirst);&&&&&&& jpEnterConfigInfo.add(this.jlblDNSremark);&&&&&&& jpEnterConfigInfo.add(this.jtfDNSremark);&&&&&& &&&&&&& jpEnterConfigInfo.add(this.jlblConfigFileName);&&&&&&& jpEnterConfigInfo.add(this.jtfConfgFileName);&&&&&& &&&&&&& add(jpEnterConfigInfo,BorderLayout.EAST);&&&&&& &&&&&&& //添加配置项列表框面板&&&&&&& JPanel jpConfigItem = new JPanel();&&&&&&& jpConfigItem.setLayout(new BorderLayout());&&&&&& &&&&&&& //设置列表框属性&&&&&&& jlConfigItem.setSelectionBackground(Color.gray);&&&&&&& jlConfigItem.setSelectionForeground(Color.red);&&&&&&& jlConfigItem.setVisibleRowCount(10);&&&&&&& jlConfigItem.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);&&&&&& &&&&&&& jlConfigItem.setFixedCellHeight(30);&&&&&&& jlConfigItem.setFixedCellWidth(200);&&&&&& &&&&&&& jlConfigItem.setVisible(true);&&&&&& &&&&&& &&&&&& &&&&&& &&&&&&& //添加配置项列表框&&&&&&& jpConfigItem.add(jlConfigItem,BorderLayout.CENTER);&&&&&& &&&&&&& add(new JScrollPane(jlConfigItem),BorderLayout.WEST);&&&&&& &&&&&& &&&&&& &&&&&&& File configPath = new File("D:\IP_CONFIG");&&&&&&& configPath.mkdir();&&&&&& &&&&&& &&&&&&& this.jbtSave.addActionListener(new ActionListener()&&&&&&& {&&&&&&&& public void actionPerformed(ActionEvent e)&&&&&&&& {&&&&&&&&& if(MainApplet.this.jtfConfgFileName.getText().isEmpty() ||&&&&&&&&&&&& MainApplet.this.jtfDNSfirst.getText().isEmpty() ||&&&&&&&&&&&& MainApplet.this.jtfDNSremark.getText().isEmpty() ||&&&&&&&&&&&& MainApplet.this.jtfGateway.getText().isEmpty() ||&&&&&&&&&&&& MainApplet.this.jtfIPAddress.getText().isEmpty() ||&&&&&&&&&&&& MainApplet.this.jtfSubnetMask.getText().isEmpty()& )&&&&&&&&& {&&&&&&&&&& //添加配置信息为空异常代码&&&&&&&&&& System.out.println("+------&请填入配置信息后再点保存按钮!");&&&&&&&&& }&&&&&&&&& else&&&&&&&&& {&&&&&&&&&& &&&&&&&&&& configInfo.setIp_address(MainApplet.this.jtfIPAddress.getText());&&&&&&&&&& configInfo.setMask(MainApplet.this.jtfSubnetMask.getText());&&&&&&&&&& configInfo.setGateway(MainApplet.this.jtfGateway.getText());&&&&&&&&&& configInfo.setDns_first(MainApplet.this.jtfDNSfirst.getText());&&&&&&&&&& configInfo.setDns_remark(MainApplet.this.jtfDNSremark.getText());&&&&&&&&&& configInfo.setNetwork_name("u672Cu5730u8FDEu63A5");&&&&&&&&&& configInfo.setFile_url("D:\IP_CONFIG");&&&&&&&&&& configInfo.setWeb_count("5");&&&&&&&&&& configInfo.setWeb_url("/");&&&&&&&&&& configInfo.setGwmetric("1");&&&&&&&&&& configInfo.setConfigFileName(MainApplet.this.jtfConfgFileName.getText());&&&&& &&&&&&&&&& //生成IP配置文件&&&&&&&&&& &&&&&&&&&& ConfigFile.WriteConfigFile(configInfo);&&&&&&&&&& &&&&&&&&&& //在配置项列表框中添加所保存的配置项名&&&&&&&&&& showConfigFileNameToList();&&&&&&&&& }&&&&&&&& }&&&&&&& });&&&&&& &&&&&& &&&&&&& this.jbtOK.addActionListener(new ActionListener()&&&&&&& {&&&&&&&& public void actionPerformed(ActionEvent e)&&&&&&&& {&&&&&&&&& //选择配置项后才能修改IP地址信息&&&&&&&&& if(jlConfigItem.isSelectionEmpty())&&&&&&&&& {&&&&&&&&&& //添加为选择配置项提示信息代码&&&&&&&&&& System.out.println("+-------&没有选择配置项,请选择一个配置项后再点OK");&&&&&&&&& }&&&&&&&&& else&&&&&&&&& {&&&&&&&&&& //读取配置文件&&&&&&&&&& configInfo = ConfigFile.ReadConfigFile((String)jlConfigItem.getSelectedValue());&&&&&&&&&& //创建p文件&&&&&&&&&& ConfigFile.createPFile(configInfo);&&&&&&&&&& &&&&&&&&&& //修改配置信息&&&&&&&&&& ExecCmd.exec(configInfo);&&&&&&&&& }&&&&&&&& }&&&&&&& });&&&&&& &&& }//end init()&& && && && && &&& public void start()&&& {&&&& &&&& showConfigFileNameToList();&&&& &&&& &&&&&&& this.jlConfigItem.addListSelectionListener(new ListSelectionListener()&&&&&&& {&&&&&&&& public void valueChanged(ListSelectionEvent e)&&&&&&&& {&&&&&&&&& if(jlConfigItem.isSelectionEmpty())&&&&&&&&& {&&&&&&&&&& //添加为选择配置项提示信息代码&&&&&&&&&& System.out.println("+-------&没有选择配置项,请选择一个配置项");&&&&&&&&& }&&&&&&&&& else&&&&&&&&& {&&&&&&&&&& //读取配置文件&&&&&&&&&& configInfo = ConfigFile.ReadConfigFile((String)jlConfigItem.getSelectedValue());&&&&&&&&&& &&&&&&&&&& //显示所选配置信息&&&&&&&&&& MainApplet.this.jtfIPAddress.setText(configInfo.getIp_address());&&&&&&&&&& MainApplet.this.jtfSubnetMask.setText(configInfo.getMask());&&&&&&&&&& MainApplet.this.jtfGateway.setText(configInfo.getGateway());&&&&&&&&&& MainApplet.this.jtfDNSfirst.setText(configInfo.getDns_first());&&&&&&&&&& MainApplet.this.jtfDNSremark.setText(configInfo.getDns_remark());&&&&&&&&&& MainApplet.this.jtfConfgFileName.setText(configInfo.getConfigFileName());&&&&&&&&& }&&&&&&&& }&&&&&&& });&&&&&& &&&&&& &&&&&&& this.jbtDelete.addActionListener(new ActionListener()&&&&&&& {&&&&&&&& public void actionPerformed(ActionEvent e)&&&&&&&& {&&&&&&&&& if(jlConfigItem.isSelectionEmpty())&&&&&&&&& {&&&&&&&&&& //添加为选择配置项提示信息代码&&&&&&&&&& System.out.println("+-------&没有选择配置项,请选择一个配置项删除");&&&&&&&&& }&&&&&&&&& else&&&&&&&&& {&&&&&&&&&& String filename = (String)jlConfigItem.getSelectedValue();&&&&&&&&&& //删除所选配置文件&&&&&&&&&& ConfigFile.delete("D:\IP_CONFIG"+filename+".properties");&&&&&&&&&& //删除对应P文件&&&&&&&&&& ConfigFile.delete("D:\IP_CONFIG"+filename+".txt");&&&&&&&&&& //更新列表框&&&&&&&&&& showConfigFileNameToList();&&&&&&&&&& //清除文本域&&&&&&&&&& jtfIPAddress.setText(null);&&&&&&&&&& jtfSubnetMask.setText(null);&&&&&&&&&& jtfGateway.setText(null);&&&&&&&&&& jtfDNSfirst.setText(null);&&&&&&&&&& jtfDNSremark.setText(null);&&&&&&&&&&&&& jtfConfgFileName.setText(null);&&&&&&&&& }&&&&&&&& }&&&&&&& });&&&&&& &&&&&& &&&&&&& this.jbtExit.addActionListener(new ActionListener()&&&&&&& {&&&&&&&& public void actionPerformed(ActionEvent e)&&&&&&&& {&&&&&&&&& System.exit(1);&&&&&&&& }&&&&&&& });&&&&&& &&&&&& &&&&&&& this.jbtAuto.addActionListener(new ActionListener()&&&&&&& {&&&&&&&& public void actionPerformed(ActionEvent e)&&&&&&&& {&&&&&&&&& ExecCmd.dhcp();&&&&&&&& }&&&&&&& });&&& }&& && &&& public void showConfigFileNameToList()&&& {&&&& //清除原来列表框内容&&&& this.dlmConfigItem.clear();&&&& this.jlConfigItem.setModel(dlmConfigItem);&&&& //获得配置文件名&&&& ArrayList&String& al = ConfigFile.getConfigFileName();&&&& //显示到列表框中&&&& for(int i=0 ; i & al.size(); i++)&&&& {&&&&&&&& this.dlmConfigItem.addElement((String)al.get(i));&&&& }&&&& this.jlConfigItem.setModel(dlmConfigItem);&&& }
}&2.ConfigFile.java类&package com.zhangzhen.&
import java.io.*;import java.util.ArrayLimport java.util.Ppublic class ConfigFile{&&public static void WriteConfigFile(SystemVars config)&{& //创建配置文件目录& ConfigFile.mkdir(config.getFile_url());& & java.util.Properties p = new java.util.Properties();& & & String comments = new String();& try& {&& File f& = new File(config.getFile_url()+config.getConfigFileName()+".properties");&& f.createNewFile();
&& PrintWriter pw = new PrintWriter(new FileWriter(f));& &&&&& p.setProperty("FILE_URL", config.getFile_url());&&&&& p.setProperty("NETWORK_NAME", config.getNetwork_name());&&&&& p.setProperty("IP_ADDRESS", config.getIp_address());&&&&& p.setProperty("MASK",config.getMask());&&&&& p.setProperty("GATEWAY",config.getGateway());&&&&& p.setProperty("GWMETRIC",config.getGwmetric());&&&&& p.setProperty("DNS_FIRST",config.getDns_first());&&&&& p.setProperty("DNS_REMARK",config.getDns_remark());&&&&& p.setProperty("WEB_COUNT", config.getWeb_count());&&&&& p.setProperty("WEB_URL",config.getWeb_url());&&&&& p.setProperty("FILE_NAME",config.getConfigFileName());&&&&& p.store(pw, comments);& }& catch(IOException e)& {&& System.out.println("+-----&写配置文件异常!");&& e.printStackTrace();& }&}&&&&public static SystemVars ReadConfigFile(String fileName)&{& String fileURL = "D:\IP_CONFIG"+fileName+".properties";& SystemVars config = new SystemVars();& & Properties p = new Properties();& & try& {&& FileReader fr = new FileReader(fileURL);&& p.load(fr);&& config.setConfigFileName(p.getProperty("FILE_NAME"));&& config.setFile_url(p.getProperty("FILE_URL"));&& config.setNetwork_name(p.getProperty("NETWORK_NAME"));&& config.setIp_address(p.getProperty("IP_ADDRESS"));&& config.setMask(p.getProperty("MASK"));&& config.setGateway(p.getProperty("GATEWAY"));&& config.setGwmetric(p.getProperty("GWMETRIC"));&& config.setDns_first(p.getProperty("DNS_FIRST"));&& config.setDns_remark(p.getProperty("DNS_REMARK"));&& config.setNetwork_name(p.getProperty("NETWORK_NAME"));&& config.setWeb_count(p.getProperty("WEB_COUNT"));&& config.setWeb_url(p.getProperty("WEB_URL"));&& && //FileReader必须和文件断开&& fr.close();& }& catch (FileNotFoundException e)& {&& e.printStackTrace();& }& catch(IOException ioe)& {&& ioe.printStackTrace();& }&&}&&&public static void createPFile(SystemVars config)&{& //生成新的目录& ConfigFile.mkdir(config.getFile_url());& & String [] str = new String[7];& //生成P处理命令& str[0]="pushd interface ip ";& str[1]="set address name="+config.getNetwork_name()+" source=static addr="+config.getIp_address()+" mask="+config.getMask();& str[2]="set address name="+config.getNetwork_name()+" gateway="+config.getGateway()+" gwmetric="+config.getGwmetric();& str[3]="set dns name="+config.getNetwork_name()+" source=static addr="+config.getDns_first();& str[4]="add dns name="+config.getNetwork_name()+" addr="+config.getDns_remark();& str[5]="set wins name="+config.getNetwork_name()+" source=static addr=none ";& str[6]="popd ";& //生成P文件& ConfigFile.createFile(config.getFile_url()+config.getConfigFileName()+".txt" , str );&}&&& &public static void mkdir(String path)&{& String mkdirName =& try& {&& File dirFile = new File(mkdirName);&& boolean bFile = dirFile.exists();&& if(!bFile)//如果文件目录不存在&& {&&& bFile = dirFile.mkdir();&&& if(!bFile)//如果文件目录创建失败&&& {&&&&&&& //添加文件目录创建失败弹出信息代码&&&& &&&& System.exit(1);&&& }&& }& } catch(Exception err)& {&& System.err.println("+--------&文件夹创建发生异常");&& System.out.println("+--------&"+err.getMessage());& }&}&&&public static void createFile(String fileUrl,String []content)&{& PrintW& try& {&& pw = new PrintWriter(new FileWriter(fileUrl));&& && for (int i = 0; i & content. i++)&& {&&& pw.println(content[i]);&& }&& //PrintWriter与文件断开&& pw.close();& }& catch(IOException e)& {&& System.out.println("+--------&创建文件失败!!!");&& System.out.println("+--------&"+e.getMessage());& }&}&&&public static ArrayList&String& getConfigFileName()&{& ArrayList&String& al = new ArrayList&String&(20);& String path = "D:\IP_CONFIG";& File f = new File(path);& & String [] fileName=new String[100];& & & if(f.exists())//如果配置文件路径存在& {&& fileName = f.list(new FilenameFilter()&& {&&& public boolean accept(File dir,String name)&&& {&&&& if(name.endsWith(".properties"))&&&& {&&&&&&&&& }&&&& else&&&& {&&&&&&&&& }&&& }&& });& }& else& {&& //添加配置文件目录不存在异常&& System.out.println("+---------&配置文件目录不存在!");& }& for(int i = 0 ; i & fileName. i++)& {&& fileName[i] = fileName[i].substring( 0 , fileName[i].indexOf(.) );&& al.add(fileName[i]);& }&&}&&&&public static boolean delete(String fileName)&{& File file = new File(fileName);& if(!file.exists())& {&&& }& else if(file.isFile())& {&& return deleteFile(fileName);& }& else& {&& return deleteDirectory(fileName);& }&}&&&public static boolean deleteFile(String fileName)&{& File file = new File(fileName);& if(file.isFile()&& file.exists())& {&& file.delete();&& System.out.println("+--------&删除单个文件" + fileName + "成功!");&&& }& else& {&& System.out.println("+--------&删除单个文件" + fileName + "失败!");&&& }&}&&&public static boolean deleteDirectory(String dir)&{& // 如果dir不以文件分隔符结尾,自动添加文件分隔符& if (!dir.endsWith(File.separator))& {&& dir = dir + File.& }& File dirFile = new File(dir);& // 如果dir对应的文件不存在,或者不是一个目录,则退出& if (!dirFile.exists() || !dirFile.isDirectory())& {&& System.out.println("+--------&删除目录失败" + dir + "目录不存在!");&&& }& boolean flag =& // 删除文件夹下的所有文件(包括子目录)& File[] files = dirFile.listFiles();& for (int i = 0; i & files. i++)& {&& // 删除子文件&& if (files[i].isFile())&& {&&& flag = deleteFile(files[i].getAbsolutePath());&&& if (!flag)&&& {&&&&&&& }&& }&& // 删除子目录&& else&& {&&& flag = deleteDirectory(files[i].getAbsolutePath());&&& if (!flag)&&& {&&&&&&& }&& }& }&& if (!flag)& {&&&&&&& System.out.println("+--------&删除目录失败");&&&&&&& }// 删除当前目录& if (dirFile.delete())& {&& System.out.println("+--------&删除目录" + dir + "成功!");&&& }& else& {&& System.out.println("+--------&删除目录" + dir + "失败!");&&& }&}}&&&&3.SystemVars.java类&&package com.zhangzhen.
public class SystemVars{&&private String file_&&private String network_&&private String ip_&&private S&&private S&&private S&&private String dns_&&private String dns_&&private String web_&&private String web_&&&&&private String configFileN&&&public String getNetwork_name()&{& return network_&}&public void setNetwork_name(String networkName)&{& network_name = networkN&}&public String getIp_address()&{& return ip_&}&public void setIp_address(String ipAddress)&{& ip_address = ipA&}&public String getMask()&{&&}&public void setMask(String mask)&{& this.mask =&}&public String getGateway()&{&&}&public void setGateway(String gateway)&{& this.gateway =&}&public String getGwmetric()&{&&}&public void setGwmetric(String gwmetric)&{& this.gwmetric =&}&public String getDns_first()&{& return dns_&}&public void setDns_first(String dnsFirst)&{& dns_first = dnsF&}&public String getDns_remark()&{& return dns_&}&public void setDns_remark(String dnsRemark)&{& dns_remark = dnsR&}&public String getFile_url()&{& return file_&}&public void setFile_url(String fileUrl)&{& file_url = fileU&}&public String getWeb_count()&{& return web_&}&public void setWeb_count(String webCount)&{& web_count = webC&}&public String getWeb_url()&{& return web_&}&public void setWeb_url(String webUrl)&{& web_url = webU&}&&public String getConfigFileName()&{& return this.configFileN&}&public void setConfigFileName(String cfgFileName)&{& this.configFileName = cfgFileN&}}&&4. ExecCmd.java 类&&package com.zhangzhen.&import java.io.Fimport java.io.IOE
public class ExecCmd{&//R&& &public static void exec(SystemVars configInfo)&{& String cmd = "netsh -f "+configInfo.getFile_url()+configInfo.getConfigFileName()+".txt";& try& {&&&&&&&& Runtime runtime = Runtime.getRuntime();&&&&&&&& Process process = runtime.exec(cmd);& }& catch(IOException e)& {&& System.out.println("+------&"+e.getMessage());& }&}&&&public static void dhcp()&{& File dhcp = new File("D:\IP_CONFIG\dhcp.txt");& String [] str = new String[5];& str[0]="pushd interface ip";& str[1]="set address name="+"本地连接 "+"source=dhcp";& str[2]="set dns name="+"本地连接 "+"source=dhcp register=PRIMARY ";& str[3]="set wins name="+"本地连接 "+"source=dhcp";& str[4]="popd";& & try& {&&&&& dhcp.createNewFile();& }& catch(IOException ie)& {&& ie.printStackTrace();& }& & ConfigFile.createFile("D:\IP_CONFIG\dhcp.txt", str);& & String cmd = "netsh -f "+"D:\IP_CONFIG\dhcp.txt";& try& {&&&&&&&& Runtime runtime = Runtime.getRuntime();&&&&&&&& Process process = runtime.exec(cmd);&&&& }& catch(IOException e)& {&& System.out.println("+------&"+e.getMessage());& }&}}

我要回帖

更多关于 双卡手机怎么切换上网 的文章

 

随机推荐