import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;public class OprationTXT { public OprationTXT(String name){
File filename = new File(name);
if(!filename.exists()){
try {
filename.createNewFile();
} catch (IOException e) {

e.printStackTrace();
}
}
}
/**
 * 
 * @param filename
 * @param context
 */
public static void writeTxtFile(String filename,String context){
RandomAccessFile raf = null;
 try {
raf = new RandomAccessFile(filename,"rw");
// raf.writeBytes(context);
//raf.writeUTF(context);
raf.write(context.getBytes());
//raf.writeChars(context);
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}finally{
if(raf != null){
try {
raf.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}

public static void main(String args[]){

for(int i = 5 ; i <=62 ; i++){
StringBuffer sb = new StringBuffer();
String filename = "D:/安装软件/"+ i +".bat";
OprationTXT txt = new OprationTXT(filename);
sb.append("@echo off  ")
.append("\n")
.append("cls  ")
.append("\n")
.append("color 0A   ")
.append("\n")
.append("Echo  \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
.append("\n")
.append("Echo           正在修改IP地址和DNS服务器地址,请耐心等待…………    ")
.append("\n")
.append("Echo \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
.append("\n")
.append("cmd /c netsh interface ip set address name=\"本地连接\" source=static addr=172.16.1.")
.append(i)
.append(" mask=255.255.255.192 gateway=172.16.1.1 gwmetric=1     ")
.append("\n")
.append("ipconfig /all        ")
.append("\n")
.append("Echo \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
.append("\n")
.append("Echo          OK!!已修改成功!请按任意键继续………… Echo          谢谢您的使用!太极制作       ")
.append("\n")
.append("Echo \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
.append("\n")
.append("Pause");

txt.writeTxtFile(filename, sb.toString());
}
}
}

解决方案 »

  1.   

    打开bat文件都没有问题的.没有出现乱码的
      

  2.   

    运行的时候出现这个问题:
    'IP地址和DNS服务器地址' 不是内部或外部命令,也不是可运行的程序
    或批处理文件。
    'face' 不是内部或外部命令,也不是可运行的程序
    或批处理文件。
    '*******' 不是内部或外部命令,也不是可运行的程序
    或批处理文件。
    '氚慈我饧绦?Echo' 不是内部或外部命令,也不是可运行的程序
    或批处理文件。
    **********
      

  3.   

    raf.write(context.getBytes("ISO8859-1"));你这样看下吧。你的中文与u002A都不是一个编码。你得把中文也转成ascii码。这样就OK了。看我的代码。:package csdn;import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;public class OprationTxt {    public OprationTxt(String name){
            File filename = new File(name);
            if(!filename.exists()){
                try {
                    filename.createNewFile();
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }
            }
        }
        /**
         * 
         * @param filename
         * @param context
         */
        public static void writeTxtFile(String filename,String context){
            RandomAccessFile raf = null;
             try {
                raf = new RandomAccessFile(filename,"rw");
                // raf.writeBytes(context);
                //raf.writeUTF(context);
                raf.write(context.getBytes("ISO8859-1"));
                //raf.writeChars(context);
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            } catch (IOException e) {
                
                e.printStackTrace();
            }finally{
                if(raf != null){
                    try {
                        raf.close();
                    } catch (IOException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        } 
        public static void main(String args[]){
            
            for(int i = 0 ; i <1 ; i++){
                StringBuffer sb = new StringBuffer();
                String filename = "D:/"+ i +".bat";
                OprationTxt txt = new OprationTxt(filename);
                sb.append("@echo off  ")
                .append("\n")
                .append("cls  ")
                .append("\n")
                .append("color 0A   ")
                .append("\n")
                .append("Echo ******************")
                .append("Echo  \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
                .append("\n")
                .append("Echo \u6b63\u5728\u4fee\u6539IP\u5730\u5740\u548cDNS\u670d\u52a1\u5668\u5730\u5740,\u8bf7\u8010\u5fc3\u7b49\u5f85\u2026\u2026\u2026\u2026")
                .append("\n")
                .append("Echo \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
                .append("\n")
                .append("cmd /c netsh interface ip set address name=\"本地连接\" source=static addr=172.16.1.")
                .append(i)
                .append(" mask=255.255.255.192 gateway=172.16.1.1 gwmetric=1     ")
                .append("\n")
                .append("ipconfig /all        ")
                .append("\n")
                .append("Echo \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
                .append("\n")
                .append("Echo OK\uff01\uff01\u5df2\u4fee\u6539\u6210\u529f\uff01\u8bf7\u6309\u4efb\u610f\u952e\u7ee7\u7eed\u2026\u2026\u2026\u2026 Echo          \u8c22\u8c22\u60a8\u7684\u4f7f\u7528\uff01\u592a\u6781\u5236\u4f5c       ")
                .append("\n")
                .append("Echo \u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A\u002A ")
                .append("\n")            .append("Pause &");
                txt.writeTxtFile(filename, sb.toString());
            }
        }
    }
      

  4.   

    代码问题是因为有中文编码的问题。我只能提示你一下。自已修改修改,就能OK。还有啦。批处理一般用
    english
      

  5.   

    我给你正版的。package csdn;import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;public class OprationTxt {    public OprationTxt(String name){
            File filename = new File(name);
            if(!filename.exists()){
                try {
                    filename.createNewFile();
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }
            }
        }
        /**
         * 
         * @param filename
         * @param context
         */
        public static void writeTxtFile(String filename,String context){
          FileOutputStream raf = null;
             try {
              raf = new FileOutputStream(filename);
                // raf.writeBytes(context);
                //raf.writeUTF(context);
                 raf.write(context.getBytes());
                //raf.writeChars(context);
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            } catch (IOException e) {
                
                e.printStackTrace();
            }finally{
                if(raf != null){
                    try {
                        raf.close();
                    } catch (IOException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        } 
        public static void main(String args[]){
            for(int i = 0 ; i <1 ; i++){
                StringBuffer sb = new StringBuffer();
                String filename = "D:/"+ i +".bat";
                OprationTxt txt = new OprationTxt(filename);
                sb.append("@echo off  ")
                .append(System.getProperty("line.separator"))
                .append("cls")
                .append(System.getProperty("line.separator"))
                .append("color 0A")
                .append(System.getProperty("line.separator"))
                .append("Echo ******************")
                .append(System.getProperty("line.separator"))
                .append("echo \u6b63\u5728\u4fee\u6539IP\u5730\u5740\u548cDNS\u670d\u52a1\u5668\u5730\u5740,\u8bf7\u8010\u5fc3\u7b49\u5f85...")
                .append(System.getProperty("line.separator"))
                .append("echo ******************")
                .append(System.getProperty("line.separator"))
                .append("cmd /c netsh interface ip set address name=\"本地连接\" source=static addr=172.16.1.22." + (i+100))
                .append(" mask=255.255.255.192 gateway=172.16.1.1 gwmetric=1")
                .append(System.getProperty("line.separator"))
                .append("ipconfig/all")
                .append(System.getProperty("line.separator"))
                .append("echo *******************")
                .append(System.getProperty("line.separator"))
                .append("echo ok 已经修改完成,请按任意键继续...")
                .append(System.getProperty("line.separator"))
                .append("echo *******************")
                .append(System.getProperty("line.separator"))
                .append("Pause &");
                txt.writeTxtFile(filename, sb.toString());
            }
        }
    }
      

  6.   

    刚我错了,中文乱码是一个问题。不过回车与空格也是一个问题。我暂时测了一下回车符。发现回车符在bat下有问题。所以改成了系统的。这样没有错。
      

  7.   

    win下的回车不是\n啊
    你先看看你输出的那个文件对不对,用记事本打开。
      

  8.   

    zhrseghloaghesoigjoo0it bw4 mnyw 34kn4w5moknyhnrs90 w45yw4mnue57uy4m,su7au7y3427m 34